Home > Article > Backend Development > Allocation of PHP object types in memory, php object type allocation_PHP tutorial
Object types, like integers, strings and other types, are also a data type in PHP. They are used to store different types of data in programs. When the program is running, each part of its content must be loaded into memory before being used. So how is object type data allocated in memory? Let’s first understand the memory structure. Logically, memory is roughly divided into four segments, namely stack space segment, heap space segment, initialization data segment and code segment. The declarations of different types of data in the program will be stored in different memory segments. The characteristics of each segment of memory are as follows.
①. Stack space segment
The stack is characterized by small space but fast access by the CPU. It is used by users to store variables temporarily created in the program. Due to the last-in-first-out nature of the stack, the stack is particularly convenient for saving and restoring call scenes. In this sense, we can think of the stack as a memory area for temporary data storage and exchange. Memory segments used to store data types that occupy a constant length and small space. For example, integers 1, 100, 10000, etc. occupy the same length of space in the memory, and the space occupied is 32-bit and 4 bytes. Double, boolean, etc. can also be stored in the stack space segment.
②. Heap space segment
The heap is used to store the memory segment that is dynamically allocated during the running of the process. Its size is not fixed and can be dynamically expanded or reduced. Used to store data with variable data length or large memory usage. For example, strings, arrays, and objects are stored in this memory.
③. Data segment
The data segment is used to store initialized global variables in the executable file. In other words, it stores variables statically allocated by the program.
④. Code segment
The code segment is used to store the operation instructions of the executable file, which means that it is the image of the executable program in the memory. The code segment needs to be prevented from being illegally modified at runtime, so only read operations are allowed, but write (modification) operations are not allowed. For example, the functions in the program are stored in this memory.
Object type data is a data type that takes up a relatively large space, and it is a data type that occupies a variable length of space. Therefore, after the object is created, it is stored in the memory, but the reference to the object is still stored in the stack. of. When the program is running, the data in the memory can be directly accessed, while the heap memory is memory that cannot be directly accessed, but the members in the object can be accessed through the reference name of the object.
>> Fixed link to this article: http://php.ncong.com/php_course/oop/duixiangfenpei.html
>> Please indicate when reprinting: Encongphp Published in Encong PHP Learning Tutorial on July 23, 2014
PHP allocates explicit memory, there is no way to allocate memory implicitly. For PHP, when a variable is defined, a space should be opened up in the memory. There is no way in C that you can use malloc, alloc, realloc to allocate memory.
Damn, how can you learn Java if you don’t understand this problem?
The memory space in the heap is allocated after new is stored in the heap.
If you declare only one object, It does not allocate heap memory for it
The heap stores objects that have been instantiated;