Delphi variable memory allocation and release

Source: Internet
Author: User

Global non-pointer type. The memory is automatically allocated and the value is initialized.

The local non-pointer type. The memory is automatically allocated after the Declaration. The value is not initialized and the value is uncertain.ProgramOperations on this memory)

Global pointer type. memory is not automatically allocated after declaration. The value is nil.

The local pointer type. After the declaration, the memory is not automatically allocated, but it will randomly point to an address, so the address is not nil

There are three types of memory available for applications: global variable zone (storing global variables), stack, and heap ). At the beginning of the application, the memory of all global variables is allocated to the global variable area, and local variables are allocated to the Application Stack, when the application ends, the variable memory allocated to the stack will be automatically released by the stack manager, and the variable memory on the stack must be manually released.

Heap (memory)
Heap indicates the memory zone available for the program, also known as the dynamic memory zone. The heap memory allocation and release order are random. That is to say, if you allocate three pieces of memory in order, the memory will not be released in the allocation order. The heap manager is responsible for all operations. You only need to use the getmem function to request new memory or call constructor to create an object, delphi returns a new memory block (the discarded memory block is reused at will ).

Heap is one of three memory zones available for applications. The other two are the global memory zone (storing full-process variables) and stack. In contrast to the heap, the full-process variables are allocated when the program is started, and are retained until the program is terminated. For details about the stack content, see the glossary.

Delphi uses heap to allocate memory for objects, strings, dynamic arrays, and special dynamic memory requests (getmem.

Windows applications have a maximum address space of 2 gigabyte, most of which can be used by the heap.

Stack (memory)
Stack indicates the memory zone available for the program. The stack memory is dynamically allocated and released in a specific order. Stack memory is allocated in the following order (LIFO), indicating that the last allocated memory zone is released first. Stack memory is generally used in routines (process, function, and method call ). When you call a routine, the parameters and returned values of the routine are placed in the stack (unless you use Delphi's default call method to optimize the call process ). In addition, the variables declared in the routine (in the VaR block before the begin statement) are also stored in the stack, so when the routine ends, these variables are automatically cleared (released in the LIFO order before the return call point ).

Stack is one of three memory zones available for applications, and the other two are the global memory zone and heap. For more information about the heap, see the glossary.

Delphi uses the stack to store routine parameters and their return values (unless you use Delphi's default register call protocol), local routine variables, Windows API function calls, and so on.

Windows applications can reserve a large amount of stack memory. in Delphi, you can use the linker page settings of the project options, but you can use the default settings. If you receive a stack overflow error message, it may be because your function is in an endless loop, rather than the stack space is too small.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.