Efficient memory management is an essential feature of superior software products. If memory allocation and release involve costly search, sorting, and re-combination operations, memory Processing may become a bottleneck of system performance. The design objectives of the AMPs memory management subsystem are as follows:
- Memory Management must be fast and the system overhead is small.
- Minimize Memory leakage without introducing the garbage collector.
- After memory allocation, it takes a relatively long time to reduce the system overhead caused by the release.
Amps uses the following methods to achieve the above goal: Application Protocol servers usually use memory in a sudden outbreak, which completes data processing by using a large data structure and then discards the data structure. For example, when a protocol message from the network is processed and the message enters the system, it is usually parsed and processed, and may be modified to add new data or delete or update some data, in each processing phase, the memory is dynamically allocated. The memory may be released after the message leaves the system, or, when each independent memory object is switched to another stage after one stage of processing is completed, the memory may be released. However, if all stages of processing are completed and then released, this approach is better than the former, the reason is as follows:
- If we accumulate all allocated small pieces of memory and then release it at one time, it will reduce memory leakage in essence because we do not need to release it in multiple places, thus avoiding omissions.
- A large block of memory is pre-allocated at the beginning. After a message enters the system, a simple and efficient allocator is established to allocate small memory objects to the buffer, in this way, you do not have to worry about managing the linked list for memory release and system overhead caused by small object allocation.
Lists an example of a protocol message. When a message arrives at the system, it can pass Programming Language The provided memory allocation function allocates a large buffer and defines a pointer pointing to the beginning of the buffer. This forms a memory management object for a specific message. When the message starts and goes through different processing stages, the application Program Use a special API to allocate memory management objects as parameters. The allocation function simply moves the pointer to allocate the required bytes and returns the address of the pointer before the allocation, in the future, the application can use this pointer to write the buffer, and the subsequent allocation needs to move the Pointer Forward. It is possible that the pre-allocated large block of memory has been used up before the message leaves the system. If this happens, we must allocate another large buffer, and splice it into the previous buffer, and then allocate it from the new buffer. In addition, we need to mount the memory block objects allocated on the buffer to a linked list. When the system no longer needs this message, release the linked list. Because all the allocations are distributed from the pre-allocated buffer, you only need to add a required memory size to the pointer, And the allocation is quite efficient. Amps provides an API to create memory management objects in the upper layer. This API returns an object as a parameter and transmits another allocation API. The allocation and destruction details are shown above, the buffer linked list established within the management object is released through another API provided by the memory management object.