Memory Management [3]

Source: Internet
Author: User
The memory allocated by virtualalloc is 4 K as the smallest unit and continuous memory address (but it is not necessarily continuous when mapped to the real memory). As mentioned above, it is not suitable for allocating small memory (for example, variables with only a few bytes); local variables are available in the "stack" Program Automatic Management. What about global small variables? This requires "Heap ".

In this case, the memory allocated by virtualalloc is neither "stack" nor "Heap"; the memory address allocated by virtualalloc is continuous, and the content in the "Heap" is generally discontinuous, therefore, it is troublesome to manage the "Heap". It is managed by the structure of the double-line linked list. The program can have several "Heap" and each "Heap" has a handle, when accessing the content in "Heap", you must first find this "Heap" and then traverse the table. This may be the root cause of "Heap" being slower than "stack.

Before allocating memory (heapalloc) in "Heap", you must create a "heapcreate", just as the program has a default "stack, each program has a default "Heap" (you can use getprocessheap to obtain the "Default Heap" handle). When we use "Heap" in Delphi, this "Default Heap" is used ". if you want your program to have multiple "heaps" more flexibly, you must use API functions.

The real memory will be submitted at the same time when the "Heap" is created, which will be slow when large memory is applied, so the default heap is only 1 MB, but the "Default Heap" does not limit the size, it will increase dynamically as needed.

Is it necessary to apply for another "Heap" with "Default Heap? This can only be reflected in multiple threads. Unlike the stack, the program assigns a "Stack zone" to each thread, while the default heap is shared by all threads in the process, when a thread uses the "Default Heap", the other thread that needs to use the "Heap" must first suspend and wait, that is, they cannot be used at the same time; only private stacks created through API functions are mutually independent and most efficient.

First, let's take a look at the functions related to "Heap.

// Create a heap. Note that the size specified during creation is also aligned by page size (pagesize). For example, if 15 K is specified, 16 K is actually allocated. heapcreate (floptions: DWORD; {heap attribute options, see the table below} dwinitialsize: DWORD; {initial size, in bytes; this size will be directly submitted to the actual memory} dwmaximumsize: DWORD {maximum size, if the maximum value is not limited, it is set to 0}): thandle; {heap handle is returned; 0 is returned for failure, but if the floptions parameter allows an exception, if a failure occurs, the system returns the exception identifier} // floptions parameter. Optional values: heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access the heap at the same time} heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return the exception identifier} heap_zero_memory = 8; {initialize the allocated memory to 0} // when the floptions parameter is specified with heap_generate_exceptions, possible returned exceptions: status_access_violation = DWORD ($ c0000005); {parameter error} status_no_memory = DWORD ($ c0000017); {insufficient memory}
  
   
 // Destroy heapdestroy (hheap: thandle {heap handle}): bool ;{}
 
   
 // Apply for heapalloc (hheap: thandle; {heap handle} dwflags: DWORD; {Memory attribute options from the heap, as shown in the following table} dwbytes: DWORD {size of the applied memory, the Unit is byte}): pointer; {the memory pointer is returned; 0 or an exception is returned if the heap is created.} // optional value of the dwflags parameter: heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access this heap at the same time} heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return the exception identifier} heap_zero_memory = 8; {initialize the allocated memory to 0} {it can be seen that this is the same as the heap attribute options; if the dwflags parameter is set to 0, the heap attribute will be used; if you re-specify the attribute that overwrites the heap} {if the heap is the default heap, that is, the heap handle comes from getprocessheap, the dwflags parameter is ignored}
  
   
 // Change the heap memory size, that is, re-allocate heaprealloc (hheap: thandle; {handle} dwflags: DWORD; {Memory attribute option; this parameter has one more option than heapalloc, see the table below} lpmem: pointer; {original memory pointer} dwbytes: DWORD {New Size}): pointer; {same as heapalloc} // dwflags parameter value options: heap_no_serialize = 1; {non-mutex. This flag allows multiple threads to access this heap at the same time} heap_generate_exceptions = 4; {when a heap creation error occurs, this flag can trigger an exception and return the exception identifier} heap_zero_memory = 8; {initialize the allocated memory to 0} heap_realloc_in_place_only = 16; {this flag cannot change the original memory location}
  
   
 // Obtain the heapsize (hheap: thandle; {heap handle} dwflags: DWORD; {Memory attribute; optional values: 0 or heap_no_serialize, the latter can ensure synchronous access} lpmem: pointer {memory pointer}): DWORD; {the size of the successful returned bytes; $ ffffffff} is returned if the failure is returned}
  
   
 // Release heapfree (hheap: thandle; {heap handle} dwflags: DWORD; {Memory attribute; optional values: 0 or heap_no_serialize} lpmem: pointer {memory pointer}): bool ;{}
  
   
 // Verify heapvalidate (hheap: thandle; {} dwflags: DWORD; {} lpmem: pointer {}): bool ;{}
  
   
 // Heapcompact (hheap: thandle ;{} dwflags: DWORD {}): uint ;{}
  
   
 // Lock heaplock (hheap: thandle {}): bool ;{}
  
   
 // Unlock heapunlock (hheap: thandle {}) after locking: bool ;{}
  
   
 // List heapwalk (hheap: thandle ;{} var lpentry: tprocessheapentry {}): bool ;{}
  
   
 

Let's take the following example.

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.