In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the C++ design of a simple memory pool example analysis, the article is very detailed, has a certain reference value, interested friends must read it!
What is a memory pool?
Usually if we use new or malloc to allocate memory, due to the uncertain size of the application, memory fragmentation and efficiency will be reduced when used frequently. In order to overcome this problem, we put forward the concept of memory pool. Memory pool is a way of allocating memory. The advantage of memory pool is that it can effectively reduce memory fragmentation, allocate memory more quickly, and reduce memory leaks.
A memory pool is to apply for the allocation of a large block of memory to be used as a spare before actually using memory. When you really need to use memory, allocate a piece of memory from the memory pool and return it to the memory pool when it is used up. If there is not enough memory block, apply for another large memory block from memory.
You can see that this has two benefits:
1. Because the memory blocks applied to memory are relatively large, the problem of external fragmentation can be reduced.
2. Apply for a large piece of memory for slow use at one time, which avoids frequently requesting memory operations from memory and improves the efficiency of memory allocation.
Memory fragmentation:
One of the main reasons for low heap utilization is memory fragmentation. If there is unused memory, but this memory cannot be used to meet the allocation request, the problem of memory fragmentation will occur. Memory fragmentation is divided into internal fragments and external fragments.
Internal fragments:
Internal fragmentation occurs when an allocated block is larger than the payload. (take a chestnut: suppose 10 bytes were previously allocated, but now only 5 bytes are used, the remaining 5 bytes will be fragmented). The size of the internal fragments is the sum of the difference between the size of the allocated blocks and their payloads. So the internal fragmentation depends on the pattern that previously requested memory and the pattern implemented by the allocator.
External fragmentation: external fragmentation is when the sum of free memory is sufficient to satisfy an allocation request, but no single free block is large enough to handle the request. External fragmentation depends on the previous memory request pattern and the implementation pattern of the allocator, as well as on the future memory request pattern. So external fragments are difficult to quantify.
Here is a simple memory pool, which is implemented for a certain object. We can implement this memory pool with a linked list, and each node on the linked list is an object pool. if we need to apply for space, we can directly apply for space in the memory pool and return it to the memory pool when we run out of it.
The design of the memory pool mainly consists of three steps:
1. Initialize
A large piece of memory is allocated to the memory pool when it is created, which is convenient for future use.
2. Allocate memory
Allocate memory in the memory pool when memory is needed.
3. Reclaim memory
When the memory allocated from the memory pool has been used, the memory needs to be returned to the memory pool.
The most important problem in designing the above memory pool is how to reuse the released memory to maximize the utilization.
However, if the size of the object is smaller than the object pointer, that is, the space of an object cannot hold the size of a pointer, internal fragmentation will inevitably occur. For example, if you open up an object pool for T-type objects, sizeof (T) = _ last- > _ objNum) / / greater than or equal to no more, it is time to allocate memory {size_t size = 2*_countIn. The maximum size of an if (size > _ maxNum) / / block cannot exceed maxNum. If it does not exceed, double the growth size = _ maxNum; _ last- > _ next = new BlockNode (size); _ last = _ last- > _ next; _ countIn = 0 } / / there is also allocated unused memory T * object = (T*) ((char*) _ last- > _ memory + _ countIn*_itemSize); _ countIn++; return new (object) T () / / initialize this space with relocation new} void Destory () {BlockNode* cur = _ frist; while (cur) {BlockNode* del = cur; cur = cur- > _ next; delete del / / will automatically call ~ BlockNode ()} _ frist = _ last = NULL;} void Delete (T* object) / / release memory {if (object) {object- > ~ T (); * ((object- *) object) = _ lastDelete / / Save the address saved in _ lastDelete in the space before T* of the tmp pointing space _ lastDelete = object;}} protected: static size_t GetItemSize () {if (sizeof (T) > sizeof (T*)) {return sizeof (T) } else {return sizeof (T*);}; templatesize_t ObjectPool::_itemSize = ObjectPool::GetItemSize (); / / out-of-class initialization static variable _ itemSize is all the contents of the article "sample Analysis of C++ designing a simple memory Pool". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.