Memory partition
Memory we artificially divided it into five areas, stack area, heap area, global area, constant area, code area, address from high to low
The stack area
- The stack area holds function parameter values, local object values, and object Pointers. Allocated and released by the system.
- Linear structure, continuous memory
The figure below defines two integers, and an objectThe storage structure of a, B, and object in the stack
Management and optimization solutions: Do not create a large number of local variables in the stack area, especially in a loop. Use the Autorealsepool auto release pool to release and optimize in time
The heap area
- Objects created by alloc, managed by the developer,
- Dynamic allocation and release cannot be determined in advance at compile time
- Relatively slow speed, flexible operation, memory discontinuous
For example, the following figure defines four objects
Object storage structure in the heap:
Global static region
- The global area is the memory space allocated at compile time, where data is kept throughout the program’s run
- The global area is divided into uninitialized BBS segment and initialized data segment.
- Store global and static variables (static modification)
The constant area
That’s where the constant string goes. The program is released by the system after completion.
Code section
The binary code that holds the function body.
There are also things like the kernel area, which is the memory occupied by the kernel system, but also retains a memory area
Memory related interview questions
There is a person class implemented as follows:
Call the following method:
Question one, can I call it the second time?
The answer is yes. The oc instance method calls essentially find the class address through ISA and then find the method through the class’s methodsList. Kc actually points to CLS.
Question two, what is the second print?
View the print result:
I’m printing a Person object, so why?
- So the first time you call self, it’s the person object. The Person object gets the name property by offsetting 8 bytes.
Self.name = person address +8
- So print it again
We analyze stack storage
Again self.name = address of CLS +8. 0x7ffEE335f7d0 +8 = 0x7FFEE335f7d8. That happens to be the stack address of Person. So the last thing you print is person.