“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

The ownership of

  • Ownership is at the heart of Rust
  • All programs must manage how they use computer memory at runtime
    • Js has a garbage collection mechanism
    • There are other languages that must show the allocation and freeing of memory
  • Let’s talk about Rust
    • Memory is managed through an ownership system in which the compiler checks rules at compile time
    • The ownership feature does not slow down the application

Stack vs. Heap

Whether a value is on the stack or on the heap in a normal language is not really sensitive to the developer, but in a system-level programming language like Rust, whether a value is on the stack or on the heap matters a lot

Stack memory

  • Stacks are stored in the order they deserve to be received and taken out in the order of ideas (last in first out of LIFO)
    • Adding data is called pushing
    • Removing data is called popping the stack
    • All data on the stack must be of known fixed size

Heap memory

The Heap organization is poor

  • When data goes into the heap, a certain amount of space is requested
  • The operating system finds a large enough space in the heap, marks it for reuse, and returns the space address
  • This process is called allocation

Storage Data Comparison

  • Because Pointers are always fixed in size, you can put Pointers on the stack. But if you use real data, you have to use Pointers to locate it
  • Data is pushed onto the stack much faster than it is allocated on the heap, because with the stack, the operating system doesn’t need storage space and the values are at the top of the stack
  • Allocating space on the heap is a lot of trouble
    • The operating system must find a large enough space according to the data, and then make a good record for the next access

Access data comparison

It is definitely slower on the heap than on the stack because it is found through a pointer, and the memory needs one more conversion to get the data

Why ownership exists

  • Where does the trace code use, and what heap data is used
  • Minimize heap duplicate data
  • Clean up unused data on the heap to avoid running out of space

Ownership exists to manage HEAP data, and understanding heap helps us understand ownership