CN1327348C - Method for resolving frequently distributing and releasing equal size internal memory - Google Patents
Method for resolving frequently distributing and releasing equal size internal memory Download PDFInfo
- Publication number
- CN1327348C CN1327348C CNB2005100607956A CN200510060795A CN1327348C CN 1327348 C CN1327348 C CN 1327348C CN B2005100607956 A CNB2005100607956 A CN B2005100607956A CN 200510060795 A CN200510060795 A CN 200510060795A CN 1327348 C CN1327348 C CN 1327348C
- Authority
- CN
- China
- Prior art keywords
- memory
- memory pool
- pool
- free
- freen
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Expired - Fee Related
Links
Images
Landscapes
- Memory System (AREA)
Abstract
Description
技术领域technical field
本发明涉及应用程序内存管理技术,特别是涉及一种解决频繁分配释放同等大小内存的方法。The invention relates to application program memory management technology, in particular to a method for solving the problem of frequently allocating and releasing memory of the same size.
背景技术Background technique
内存是计算机类产品的重要硬件设施,应用程序的内存分配释放都是建立在操作系统对内存的管理之上的。操作系统提供接口比如malloc,free等函数供应用程序进行内存的相关操作,操作系统本身则对应用程序的内存使用情况进行管理。Memory is an important hardware facility of computer products, and the memory allocation and release of application programs are all based on the management of memory by the operating system. The operating system provides interfaces such as malloc, free and other functions for the application program to perform memory-related operations, and the operating system itself manages the memory usage of the application program.
操作系统的内存管理机制会产生几种内存浪费的情况:内部碎片和外部碎片。The memory management mechanism of the operating system can produce several situations of memory waste: internal fragmentation and external fragmentation.
操作系统的内存管理机制遵循一些基本的内存分配规则。例如,所有内存的分配必须起始于可被4、8或16整除的地址。内存管理机制把仅仅预定大小的内存块分配给应用程序。当一个应用程序请求一个43字节的内存块的时候,内存管理机制可能会给它分配44个字节、或者48个字节甚至更多满足内存分配规则的内存。由所需大小四舍五入而产生的多余空间叫内部碎片。The memory management mechanism of the operating system follows some basic memory allocation rules. For example, all memory allocations must start at addresses divisible by 4, 8, or 16. The memory management mechanism allocates only blocks of memory of a predetermined size to applications. When an application requests a 43-byte memory block, the memory management mechanism may allocate 44 bytes, or 48 bytes or even more memory that meets the memory allocation rules. The extra space created by rounding off the desired size is called internal fragmentation.
当已分配内存块之间出现未被使用的差额时,就会产生外部碎片。例如,一个应用程序分配三个连续的内存块,然后释放中间的内存块。操作系统的内存管理机制可以重新使用中间的内存块供将来的内存块进行分配。但不太可能将来分配的内存块的大小跟操作系统的所有空闲内存块一样大。那么内存管理机制只能从大的空闲内存块中间分割出小的满足应用程序要求的内存块,这样一直继续下来,最后会出现很多小的内存之间的碎片,它们不能满足应用程序的任何一个内存请求,这些就是外部碎片。External fragmentation occurs when there is an unused gap between allocated memory blocks. For example, an application allocates three contiguous blocks of memory and then frees the middle block of memory. The operating system's memory management mechanisms can reuse intermediate memory blocks for future memory block allocations. But it is unlikely that the size of the memory block allocated in the future will be as large as all free memory blocks of the operating system. Then the memory management mechanism can only divide small memory blocks that meet the requirements of the application program from the middle of the large free memory block, and this continues, and eventually there will be fragments between many small memories, which cannot meet any of the application programs. Memory requests, these are external fragments.
在频繁分配释放同等大小内存的时候,操作系统会产生大量的外部内存碎片,并影响操作系统的效率。传统的解决方法是采用内存池。但是单一的内存池存在着定制性差、内存浪费、在扩展内存池的时候有可能产生内存碎片等缺陷,因此,对传统的频繁分配释放同等大小内存的方法进行改造,可以增加应用程序内存的可定制性,提高系统内存的使用效率,改善系统性能。When frequently allocating and releasing memory of the same size, the operating system will generate a large number of external memory fragments and affect the efficiency of the operating system. The traditional solution is to use memory pools. However, a single memory pool has defects such as poor customization, memory waste, and possible memory fragmentation when expanding the memory pool. Therefore, transforming the traditional method of frequently allocating and releasing memory of the same size can increase the availability of application memory. Customization, improve the efficiency of system memory usage, and improve system performance.
一个内存池是应用程序从操作系统中预先申请得到的一块连续内存,内存池的属性包括内存池的大小、起始地址、结束地址、内存块的大小、内存块的个数、内存块的标志、内存块的其它信息等。内存池的大小为内存块的个数乘以内存块的大小、内存块的标志大小与内存块的其它信息大小之和。A memory pool is a piece of continuous memory pre-applied by the application program from the operating system. The attributes of the memory pool include the size of the memory pool, the start address, the end address, the size of the memory block, the number of memory blocks, and the symbol of the memory block , other information about the memory block, etc. The size of the memory pool is the sum of the number of memory blocks multiplied by the size of the memory block, the size of the flag of the memory block, and the size of other information of the memory block.
应用程序根据需要分配内存的大小,从对应的内存池取空闲内存块的地址进行使用,使用完后返回给内存池。The application program allocates the memory size according to the need, takes the address of the free memory block from the corresponding memory pool for use, and returns it to the memory pool after use.
内存池没有空闲内存块的时候,应用程序扩展内存池,操作系统负责根据操作系统的内存管理机制扩展内存池或者重新分配更大的内存池,并且把原有的内存池信息复制到新的内存池。When the memory pool has no free memory blocks, the application program expands the memory pool, and the operating system is responsible for expanding the memory pool or reallocating a larger memory pool according to the memory management mechanism of the operating system, and copying the original memory pool information to the new memory pool. pool.
现有的内存池管理方法,在应用程序刚开始的时候就限制了内存池的大小,可定制性较差,尽管当内存池不够大的时候可以扩展内存池,但同样可能会产生外部内存碎片,而且在内存池一直扩大的情况下,不停扩展内存池就相当于频繁分配内存,对系统效率有明显的影响。The existing memory pool management method limits the size of the memory pool at the beginning of the application, and the customizability is poor. Although the memory pool can be expanded when the memory pool is not large enough, it may also generate external memory fragments , and when the memory pool has been expanding, continuous expansion of the memory pool is equivalent to frequent allocation of memory, which has a significant impact on system efficiency.
因此,如果能提出更有效的解决频繁分配同等大小的方法,可以提高整个应用程序内存的可定制性,更有效的利用内存和节约内存,提高应用程序的运行速度,减少响应时间。Therefore, if a more effective solution to frequent allocation of the same size can be proposed, the customizability of the entire application memory can be improved, the memory can be used and saved more effectively, the running speed of the application can be improved, and the response time can be reduced.
发明内容Contents of the invention
本发明的目的在于提供一种解决频繁分配释放同等大小内存的方法。The purpose of the present invention is to provide a method for solving the problem of frequently allocating and freeing memory of the same size.
本发明采用的技术方案如下:The technical scheme that the present invention adopts is as follows:
1)记频繁分配释放同等大小内存的大小为msize,内存池链表的指针为plist,第n个内存池为mpn,内存池的起始地址为psn,结束地址为pen,内存池的第一个free标志的内存在该内存池的位置为freen;1) Note that the size of frequently allocated and released memory of the same size is msize, the pointer of the memory pool list is plist, the nth memory pool is mpn, the start address of the memory pool is psn, the end address is pen, the first memory pool is The memory of the free flag is free at the location of the memory pool;
2)定义一个函数f(n)用来决定第n个内存池中内存块的个数,f(n)值等于0或者小于0,则表示应用程序设定了大小为msize的内存块的个数上限,大小为前面n-1次分配的内存块的个数的和;2) Define a function f(n) to determine the number of memory blocks in the nth memory pool. If the value of f(n) is equal to 0 or less than 0, it means that the application program has set the number of memory blocks whose size is msize. The upper limit of the number, the size is the sum of the number of memory blocks allocated in the previous n-1 times;
3)当应用程序需要大小为msize的内存时,则从内存池链表的第一个内存池开始,逐个判断内存池的freen是否大于f(n);如果freen小于等于f(n),表示该内存池有空闲内存可以分配,返回该内存池;如果freen大于f(n),表示该内存池没有空闲内存可以使用,判断下一个内存池;如果没有下一个内存池,并且f(n)>0,那么新建一个大小为F(n)的内存池,其中F(n)=(msize+标志位大小)*f(n);同时初始化所有的标志位为free,初始化freen为1,然后返回该内存池;如果f(n)<=0,则通知应用程序已达内存上限;3) When the application program needs memory with a size of msize, start from the first memory pool in the memory pool linked list, and judge whether the freen of the memory pool is greater than f(n); if freen is less than or equal to f(n), it means that the The memory pool has free memory that can be allocated, and returns the memory pool; if freen is greater than f(n), it means that the memory pool has no free memory to use, and judges the next memory pool; if there is no next memory pool, and f(n) > 0, then create a new memory pool with a size of F(n), where F(n)=(msize+flag size)*f(n); at the same time, initialize all flags to free, initialize free to 1, and then return the Memory pool; if f(n)<=0, notify the application that the memory limit has been reached;
4)得到有空闲内存可以使用的内存池以后,把该内存池的位置为freen的内存给应用程序使用,同时把位置为freen的内存的标志设为used,然后从位置freen+1开始,逐个判断内存池的内存的标志是否为free,如果在内存池结束前找到标志为free的内存,则令该内存池的freen等于找到的标志为free的内存所在的位置,如果没找到标志为free的内存,则令该内存池的freen等于f(n)+1;4) After obtaining a memory pool with free memory available for use, use the memory whose location is free for the application program, and at the same time set the flag of the memory whose location is free to used, and then start from the location free+1, one by one Determine whether the memory flag of the memory pool is free. If the memory marked as free is found before the end of the memory pool, the free of the memory pool is equal to the location of the found memory marked as free. If no memory marked as free is found memory, then make the freen of the memory pool equal to f(n)+1;
5)当应用程序释放大小为msize的内存时,则把该内存的标志设为free,同时判断该内存在所在内存池的位置是否大于所在内存池的freen,如果小于freen,则令freen等于该内存在所在内存池的位置;5) When the application releases the memory with the size of msize, set the flag of the memory to free, and at the same time judge whether the location of the memory in the memory pool is greater than the free of the memory pool, if it is smaller than free, then make free equal to the The location of the memory in the memory pool;
6)应用程序退出的时候,销毁内存池链表中的所有内存池。6) When the application exits, all memory pools in the memory pool linked list are destroyed.
本发明和已有技术相比,具有的有益的效果是:用内存池链表替代单一的内存池、用函数来控制内存池的数量和大小,从而提高了整个应用程序内存的可定制性,更有效的利用内存和节约内存,提高应用程序的运行速度,减少响应时间。Compared with the prior art, the present invention has the beneficial effects of: replacing a single memory pool with a memory pool linked list, and using functions to control the number and size of the memory pool, thereby improving the customizability of the entire application program memory and making it easier Effective use of memory and save memory, improve the running speed of applications, reduce response time.
附图说明Description of drawings
图1是内存池结构图;Figure 1 is a memory pool structure diagram;
图2是内存池链表的总结构图;Fig. 2 is a general structure diagram of the memory pool linked list;
图3是有1个新建内存池的内存池链表;Figure 3 is a memory pool linked list with 1 new memory pool;
图4是应用程序分配了1块大小为32的内存后的内存池链表;Figure 4 is the memory pool linked list after the application allocates a block of memory with a size of 32;
图5是在图4状态上,应用程序连续分配了5块大小为32的内存后的内存池链表;Figure 5 is the memory pool linked list after the application has continuously allocated 5 blocks of memory with a size of 32 in the state of Figure 4;
图6是在图5状态上,应用程序释放了位于内存池链表中内存池mp1第2个位置的内存块后的内存池链表状态。FIG. 6 is the state of the memory pool list after the application program releases the memory block at the second position of the memory pool mp1 in the memory pool list in the state of FIG. 5 .
具体实施方式Detailed ways
下面结合附图,用一个实施例来说明如何用本发明解决频繁分配释放同等大小内存的情况。In the following, an embodiment is used to illustrate how to use the present invention to solve the situation of frequently allocating and releasing memory of the same size in conjunction with the accompanying drawings.
记频繁分配释放同等大小内存的大小为msize,内存池链表的指针为plist,第n个内存池为mpn,内存池的起始地址为psn,结束地址为pen,内存池的第一个free标志的内存在该内存池的位置为freen;定义一个函数f(n)用来决定第n个内存池中内存块的个数,f(n)值等于0或者小于0,则表示应用程序设定了大小为msize的内存块的个数上限,大小为前面n-1次分配的内存块的个数的和,见图1和图2所示。Note that the size of frequently allocated and released memory of the same size is msize, the pointer of the memory pool list is plist, the nth memory pool is mpn, the start address of the memory pool is psn, the end address is pen, and the first free flag of the memory pool The location of the memory in the memory pool is free; define a function f(n) to determine the number of memory blocks in the nth memory pool, and the value of f(n) is equal to 0 or less than 0, which means that the application program The upper limit of the number of memory blocks whose size is msize is set, and the size is the sum of the number of memory blocks allocated for the previous n-1 times, as shown in Figure 1 and Figure 2.
步骤1:采用技术方案1)初始化频繁分配释放同等大小的内存的大小msize=32,内存池链表的指针为plist为空;Step 1: adopt technical scheme 1) initialization frequent allocation releases the size msize=32 of memory of the same size, and the pointer of the memory pool linked list is empty for plist;
步骤2:采用技术方案2)初始化函数f,令f(1)=5,f(2)=7,f(3)=0,表示应用程序设定了内存大小为32的内存块的个数上限为5+7=12个;Step 2: adopt technical solution 2) to initialize function f, make f(1)=5, f(2)=7, f(3)=0, it means that the application program has set the number of memory blocks whose memory size is 32 The upper limit is 5+7=12;
步骤3:采用技术方案3)取得有空闲内存的内存池mp1,内存池mp1的5个内存块的标志全部为free(用F简写),内存池的第一个空闲内存块的位置free1=1。(见图3所示)Step 3: adopt technical scheme 3) to obtain memory pool mp1 with free memory, the signs of 5 memory blocks of memory pool mp1 are all free (abbreviated with F), the position free1=1 of the first free memory block of memory pool . (See Figure 3)
步骤4:得到有空闲内存块的内存池后,采用技术方案4)把位置为1的内存块给应用程序使用,同时把位置1的内存块的标志改成used(用U简写),内存池mp1的第一个空闲内存块的位置free1=2。(见图4所示)Step 4: After obtaining the memory pool with free memory blocks, adopt technical solution 4) to use the memory block at
步骤5:采用技术方案3)和4)再分配5块大小为32的内存给应用程序使用,这时内存池mp1的第一个空闲内存块的位置free1=6,内存池mp2的第一个空闲内存块的位置free2=2。(见图5所示)Step 5: adopt technical scheme 3) and 4) to allocate 5 blocks of memory whose size is 32 to the application program, at this moment, the position free1=6 of the first free memory block of memory pool mp1, and the first free memory block of memory pool mp2 The location free2=2 of the free memory block. (See Figure 5)
步骤6:采用技术方案5)释放内存池mp1的位置为2的内存块,这时把该内存块的标志改为free,同时令内存池mp1的第一个空闲内存块的位置free1=2。Step 6: adopt technical solution 5) to release the memory block whose position of memory pool mp1 is 2, at this moment change the sign of this memory block to free, and make the position free1=2 of the first free memory block of memory pool mp1 simultaneously.
(见图6所示)(See Figure 6)
步骤7:采用技术方案6)销毁内存池链表plist中的内存池mp1和mp2。这时,plist为空。Step 7: adopt technical solution 6) to destroy the memory pools mp1 and mp2 in the memory pool linked list plist. At this time, the plist is empty.
Claims (1)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CNB2005100607956A CN1327348C (en) | 2005-09-16 | 2005-09-16 | Method for resolving frequently distributing and releasing equal size internal memory |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CNB2005100607956A CN1327348C (en) | 2005-09-16 | 2005-09-16 | Method for resolving frequently distributing and releasing equal size internal memory |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN1740975A CN1740975A (en) | 2006-03-01 |
| CN1327348C true CN1327348C (en) | 2007-07-18 |
Family
ID=36093377
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CNB2005100607956A Expired - Fee Related CN1327348C (en) | 2005-09-16 | 2005-09-16 | Method for resolving frequently distributing and releasing equal size internal memory |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN1327348C (en) |
Families Citing this family (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN100530140C (en) * | 2007-11-08 | 2009-08-19 | Ut斯达康通讯有限公司 | Memory management method for application program |
| CN101673246A (en) * | 2009-08-06 | 2010-03-17 | 深圳市融创天下科技发展有限公司 | High-efficient first-in first-out (FIFO) data pool reading and writing method |
| CN102253900B (en) * | 2011-06-13 | 2013-06-12 | 华中师范大学 | Memory optimization method under Windows CE operating system |
| WO2015180667A1 (en) * | 2014-05-28 | 2015-12-03 | Mediatek Inc. | Computing system with reduced data exchange overhead and related data exchange method thereof |
| CN114365096B (en) * | 2019-11-04 | 2024-11-29 | 深圳市欢太科技有限公司 | Memory allocation method, memory allocation device, terminal and computer readable storage medium |
| CN113419715B (en) * | 2021-06-17 | 2024-06-25 | 吕锦柏 | Dynamic memory management method and equipment based on linked list |
Citations (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US6574718B2 (en) * | 2001-02-28 | 2003-06-03 | International Business Machines Corporation | Excessive spin detection and avoidance for systems using a least recently used page replacement algorithm |
| CN1183453C (en) * | 2001-12-21 | 2005-01-05 | 上海贝尔有限公司 | Internal storage management system and its distribution method |
| CN1581064A (en) * | 2003-08-13 | 2005-02-16 | 华为技术有限公司 | Timer management method |
-
2005
- 2005-09-16 CN CNB2005100607956A patent/CN1327348C/en not_active Expired - Fee Related
Patent Citations (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US6574718B2 (en) * | 2001-02-28 | 2003-06-03 | International Business Machines Corporation | Excessive spin detection and avoidance for systems using a least recently used page replacement algorithm |
| CN1183453C (en) * | 2001-12-21 | 2005-01-05 | 上海贝尔有限公司 | Internal storage management system and its distribution method |
| CN1581064A (en) * | 2003-08-13 | 2005-02-16 | 华为技术有限公司 | Timer management method |
Non-Patent Citations (3)
| Title |
|---|
| 具有服务仲裁机制的内存数据库管理器的研究与设计 宋广华,杨长生,石教英,计算机辅助设计与图形学学报,第14卷第3期 2002 * |
| 具有服务仲裁机制的内存数据库管理器的研究与设计 宋广华,杨长生,石教英,计算机辅助设计与图形学学报,第14卷第3期 2002;有效实现内存管理的方法 潘立登,李婷,北京化工大学学报,第27卷第3期 2000 * |
| 有效实现内存管理的方法 潘立登,李婷,北京化工大学学报,第27卷第3期 2000 * |
Also Published As
| Publication number | Publication date |
|---|---|
| CN1740975A (en) | 2006-03-01 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US12541813B2 (en) | Dynamic kernel memory space allocation | |
| CN101266575B (en) | A method to improve memory pool utilization | |
| US7246195B2 (en) | Data storage management for flash memory devices | |
| US7653799B2 (en) | Method and apparatus for managing memory for dynamic promotion of virtual memory page sizes | |
| US7971027B2 (en) | Mark page-out pages as critical for cooperative memory over-commitment | |
| CN109725846B (en) | Memory system and control method | |
| KR100874702B1 (en) | Device Drivers and Methods for Efficiently Managing Flash Memory File Systems | |
| KR101385430B1 (en) | Cache coherence protocol for persistent memories | |
| US7689762B2 (en) | Storage device wear leveling | |
| JP4701321B2 (en) | Memory management | |
| CN101859279B (en) | Memory allocation and release method and device | |
| CN101042684A (en) | System and method for improving system DMA mapping while substantially reducing memory fragmentation | |
| US20080235306A1 (en) | Garbage collection in nonvolatile memories using data attributes, computer program products and methods of operating the same | |
| CN106844050A (en) | A kind of memory allocation method and device | |
| CN103793332B (en) | Memory-based data storage method, device, processor and electronic device | |
| CN109753361A (en) | A kind of EMS memory management process, electronic equipment and storage device | |
| CN105988875A (en) | Method and device for running process | |
| CN1327348C (en) | Method for resolving frequently distributing and releasing equal size internal memory | |
| CN102968380A (en) | Method and device for managing memory partitions of memory filesystem | |
| WO2022199560A1 (en) | Memory management method and device | |
| US8055876B2 (en) | Selectively mark free frames as unused for cooperative memory over-commitment | |
| CN110109677B (en) | A Dynamic Object Cache Pool Allocation Method | |
| CN106294198A (en) | The memory allocation method of a kind of (SuSE) Linux OS and method for releasing | |
| CN115168243A (en) | Memory space management method and device, electronic equipment and storage medium | |
| CN114741205B (en) | Anonymous page recovery method and electronic device |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| C06 | Publication | ||
| PB01 | Publication | ||
| C10 | Entry into substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| C14 | Grant of patent or utility model | ||
| GR01 | Patent grant | ||
| CF01 | Termination of patent right due to non-payment of annual fee | ||
| CF01 | Termination of patent right due to non-payment of annual fee |
Granted publication date: 20070718 Termination date: 20160916 |