[go: up one dir, main page]

CN107515788A - Method and device for allocating memory - Google Patents

Method and device for allocating memory Download PDF

Info

Publication number
CN107515788A
CN107515788A CN201710797453.5A CN201710797453A CN107515788A CN 107515788 A CN107515788 A CN 107515788A CN 201710797453 A CN201710797453 A CN 201710797453A CN 107515788 A CN107515788 A CN 107515788A
Authority
CN
China
Prior art keywords
memory
value
chained list
thread
memory value
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.)
Pending
Application number
CN201710797453.5A
Other languages
Chinese (zh)
Inventor
刘如意
孙京本
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhengzhou Yunhai Information Technology Co Ltd
Original Assignee
Zhengzhou Yunhai Information Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Zhengzhou Yunhai Information Technology Co Ltd filed Critical Zhengzhou Yunhai Information Technology Co Ltd
Priority to CN201710797453.5A priority Critical patent/CN107515788A/en
Publication of CN107515788A publication Critical patent/CN107515788A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System (AREA)

Abstract

本发明公开了一种内存分配的方法,将总内存分为了本地池和上层内存池,本地池中又有多个与线程对应的链表,线程与链表对应,解决了内存分配慢的问题;而每个线程的内存大小根据占用内存最小的线程确定,当分配内存时,本地池中链表不会有浪费;如果链表的内存不够时,可以按照需要总内存和链表中已有内存的差值,在上层内存池中拆分出差值大小的内存,合并到这个链表中再进行分配,因此,在实现内存分配时,如果线程对内存的需求超过了其对应链表的内存,那么则在上层内存池中拆分出超过的内存的大小,可见,按需分配内存,有效地解决了资源浪费的问题。本发明还公开一种内存分配的装置,同样可以实现上述技术效果。

The invention discloses a memory allocation method, which divides the total memory into a local pool and an upper-level memory pool, and in the local pool there are multiple linked lists corresponding to threads, and the threads correspond to the linked lists, which solves the problem of slow memory allocation; and The memory size of each thread is determined according to the thread that occupies the smallest memory. When allocating memory, the linked list in the local pool will not be wasted; The memory of the difference value is split in the upper memory pool, merged into this linked list and then allocated. Therefore, when implementing memory allocation, if the thread's demand for memory exceeds the memory of its corresponding linked list, then in the upper memory The size of the excess memory is split from the pool. It can be seen that memory is allocated on demand, which effectively solves the problem of resource waste. The invention also discloses a device for allocating memory, which can also achieve the above-mentioned technical effect.

Description

一种内存分配的方法及装置Method and device for allocating memory

技术领域technical field

本发明涉及计算机技术领域,更具体地说,涉及一种内存分配的方法及装置。The present invention relates to the technical field of computers, and more specifically, to a memory allocation method and device.

背景技术Background technique

在计算机中,经常会涉及到为线程分配内存以及分配后释放内存。一些系统可能会频繁地分配和释放各种大小的内存,然而,内存的分配和释放过程十分的缓慢,尤其是在系统多线程的情况下,每个线程彼此之间会相互竞争内存,因此要进行加锁,从而使内存分配效率更低。In computers, it often involves allocating memory for threads and freeing memory after allocation. Some systems may frequently allocate and release memory of various sizes. However, the process of memory allocation and release is very slow, especially in the case of multi-threaded systems. Each thread will compete with each other for memory, so it is necessary to Locking is performed to make memory allocation less efficient.

为解决这个问题,在内存中针对各个线程分别建立了对应线程的内存单元,在分配内存时,只需要将对应内存单元分配给对应的线程即可。但是,为每个线程都分配一个内存单元,会造成内存的资源浪费。To solve this problem, memory units corresponding to threads are respectively established for each thread in the memory. When allocating memory, it is only necessary to assign the corresponding memory unit to the corresponding thread. However, allocating a memory unit for each thread will cause waste of memory resources.

因此,如何在提高内存分配效率的同时,减少内存资源的浪费,是本领域技术人员需要解决的问题。Therefore, how to reduce waste of memory resources while improving memory allocation efficiency is a problem to be solved by those skilled in the art.

发明内容Contents of the invention

本发明的目的在于提供一种内存分配的方法及装置,以在提高内存分配效率的同时,减少内存资源的浪费。The object of the present invention is to provide a memory allocation method and device, so as to reduce waste of memory resources while improving memory allocation efficiency.

为实现上述目的,本发明实施例提供了如下技术方案:In order to achieve the above object, the embodiment of the present invention provides the following technical solutions:

一种内存分配的方法,包括:A method of memory allocation, comprising:

获取待分配内存的线程的所需内存值;Obtain the required memory value of the thread to be allocated memory;

判断所述所需内存值是否小于等于本地池中与所述线程对应的第一链表的第一内存值;其中,所述本地池中的一个链表对应一个线程,所述链表的内存值根据占用内存最小的线程确定;Judging whether the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool; wherein, a linked list in the local pool corresponds to a thread, and the memory value of the linked list is based on the occupied The thread with the smallest memory is determined;

若是,则利用所述第一链表为所述线程分配内存;If so, use the first linked list to allocate memory for the thread;

若否,则获取第一剩余内存值;所述第一剩余内存值为所述所需内存值与所述第一内存值的差值;If not, then obtain the first remaining memory value; the first remaining memory value is the difference between the required memory value and the first memory value;

在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。Split the memory with the size of the first remaining memory value in the upper memory pool, merge the memory into the first linked list, and use the merged first linked list to allocate memory for the thread.

其中,所述在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存,包括:Wherein, said splitting the memory of the size of the first remaining memory value in the upper memory pool, merging the memory into the first linked list, and using the merged first linked list to allocate memory for the thread, include:

在公共池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。Splitting the memory with the size of the first remaining memory value in the common pool, merging the memory into the first linked list, and using the combined first linked list to allocate memory for the thread.

其中,所述在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存,包括:Wherein, said splitting the memory of the size of the first remaining memory value in the upper memory pool, merging the memory into the first linked list, and using the merged first linked list to allocate memory for the thread, include:

判断所述第一剩余内存值是否小于等于公共池的第二内存值;judging whether the first remaining memory value is less than or equal to the second memory value of the common pool;

若是,则在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存;If so, then split the memory of the first remaining memory value size in the public pool, merge into the first linked list, and use the merged first linked list to allocate memory for the thread;

若否,则获取第二剩余内存值;所述第二内存值为所述第一剩余内存值与所述第二内存值的差值;If not, then acquire a second remaining memory value; the second memory value is the difference between the first remaining memory value and the second memory value;

在内存页堆中拆分与所述第二剩余内存值对应的内存页,并将所述内存页合并到所述公共池中;Splitting the memory page corresponding to the second remaining memory value in the memory page heap, and merging the memory page into the common pool;

继续执行所述在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存的步骤。Continue to execute the step of splitting the memory with the size of the first remaining memory value in the public pool, merging it into the first linked list, and using the merged first linked list to allocate memory for the thread.

其中,所述在内存页堆中拆分与所述第二剩余内存值对应的内存页,并将所述内存页合并到所述公共池中,包括:Wherein, the splitting the memory page corresponding to the second remaining memory value in the memory page heap, and merging the memory page into the public pool includes:

利用所述第二剩余内存值确定内存页数;determining the number of memory pages by using the second remaining memory value;

利用所述内存页数在内存页堆中确定第二链表;其中所述内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同;Using the number of memory pages to determine a second linked list in the memory page heap; wherein the memory page heap includes a plurality of linked lists, each linked list includes a plurality of intervals, and each interval is a preset number of continuous memory pages, The number of memory pages in each interval in the same linked list is the same;

在所述第二链表中拆分一个区间的所有内存页,并将拆分的内存页合并到所述公共池中。All the memory pages of a range are split in the second linked list, and the split memory pages are merged into the common pool.

其中,所述第二链表中拆分一个区间的内存页,并将拆分的内存页合并到所述公共池中,包括:Wherein, splitting a range of memory pages in the second linked list, and merging the split memory pages into the common pool includes:

根据所述线程确定pageID,利用基数树确定所述pageID对应的区间,在所述第二链表中拆分所述区间的内存页,并将拆分的内存页合并到所述公共池中。Determine the pageID according to the thread, use the radix tree to determine the section corresponding to the pageID, split the memory pages of the section in the second linked list, and merge the split memory pages into the common pool.

其中,所述第一链表包括至少一个内存块。Wherein, the first linked list includes at least one memory block.

一种内存分配的装置,包括:A device for memory allocation, comprising:

所需内存值获取模块,用于获取待分配内存的线程的所需内存值;The required memory value acquisition module is used to obtain the required memory value of the thread to be allocated memory;

判断模块,用于判断所述所需内存值是否小于等于本地池中与所述线程对应的第一链表的第一内存值;其中,所述本地池中的一个链表对应一个线程,所述链表的内存值根据占用内存最小的线程确定;A judging module, configured to judge whether the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool; wherein, a linked list in the local pool corresponds to a thread, and the linked list The memory value of is determined according to the thread that occupies the smallest memory;

分配模块,用于所述所需内存值小于等于本地池中与所述线程对应的第一链表的第一内存值时,利用所述第一链表为所述线程分配内存;An allocation module, configured to use the first linked list to allocate memory for the thread when the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool;

第一剩余内存值获取模块,用于所述所需内存值大于本地池中与所述线程对应的第一链表的第一内存值时,获取第一剩余内存值;所述第一剩余内存值为所述所需内存值与所述第一内存值的差值;The first remaining memory value acquisition module is used to obtain the first remaining memory value when the required memory value is greater than the first memory value of the first linked list corresponding to the thread in the local pool; the first remaining memory value is the difference between the required memory value and the first memory value;

第二分配模块,用于在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。The second allocating module is configured to split the memory of the first remaining memory value size in the upper memory pool, merge the memory into the first linked list, and use the combined first linked list for the thread Allocate memory.

其中,所述第二分配模块,具体用于,在公共池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。Wherein, the second allocation module is specifically configured to split the memory with the size of the first remaining memory value in the common pool, merge the memory into the first linked list, and use the merged first The linked list allocates memory for said thread.

其中,所述第二分配模块,包括:Wherein, the second distribution module includes:

判断单元,用于判断所述第一剩余内存值是否小于等于公共池的第二内存值;A judging unit, configured to judge whether the first remaining memory value is less than or equal to the second memory value of the common pool;

第一合并单元,用于所述第一剩余内存值小于等于公共池的第二内存值时,在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存;The first merging unit is used for splitting the memory with the size of the first remaining memory value in the common pool when the first remaining memory value is less than or equal to the second memory value of the public pool, and merging it into the first linked list , and utilize the combined first linked list to allocate memory for the thread;

第二剩余内存值获取单元,用于在所述第一剩余内存值大于公共池的第二内存值时,获取第二剩余内存值;所述第二内存值为所述第一剩余内存值与所述第二内存值的差值;A second remaining memory value obtaining unit, configured to obtain a second remaining memory value when the first remaining memory value is greater than a second memory value of the public pool; the second remaining memory value is equal to the first remaining memory value a difference between said second memory value;

第二合并单元,用于在内存页堆中拆分与所述第二剩余内存值对应的内存页,并将所述内存页合并到所述公共池中;继续调用所述第一合并单元。The second merging unit is configured to split the memory page corresponding to the second remaining memory value in the memory page heap, and merge the memory page into the common pool; continue to call the first merging unit.

其中,所述第二合并单元,包括:Wherein, the second merging unit includes:

内存页数确定子单元,用于利用所述第二剩余内存值确定内存页数;a subunit for determining the number of memory pages, configured to determine the number of memory pages by using the second remaining memory value;

第二链表确定子单元,用于利用所述内存页数在内存页堆中确定第二链表;其中所述内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同;The second linked list determination subunit is used to determine the second linked list in the memory page heap by using the number of memory pages; wherein the memory page heap includes a plurality of linked lists, and each linked list includes a plurality of intervals, and each interval is The preset number of continuous memory pages, the number of memory pages in each interval in the same linked list is the same;

合并子单元,用于在所述第二链表中拆分一个区间的所有内存页,并将拆分的内存页合并到所述公共池中。The merging subunit is used to split all the memory pages of a range in the second linked list, and merge the split memory pages into the common pool.

通过以上方案可知,本发明实施例提供的一种内存分配的方法,将总内存分为了本地池和上层内存池,本地池中又有多个与线程对应的链表,线程与链表对应,解决了内存分配慢的问题;而每个线程的内存大小根据占用内存最小的线程确定,当分配内存时,本地池中链表不会有浪费;如果链表的内存不够时,可以按照需要总内存和链表中已有内存的差值,在上层内存池中拆分出差值大小的内存,合并到这个链表中再进行分配,因此,在实现内存分配时,如果线程对内存的需求超过了其对应链表的内存,那么则在上层内存池中拆分出超过的内存的大小,可见,按需分配内存,有效地解决了资源浪费的问题。本发明实施例还提供一种内存分配的装置,同样可以实现上述技术效果。It can be seen from the above scheme that the method of memory allocation provided by the embodiment of the present invention divides the total memory into a local pool and an upper memory pool, and there are multiple linked lists corresponding to threads in the local pool, and threads correspond to linked lists, which solves the problem of The problem of slow memory allocation; the memory size of each thread is determined according to the thread that occupies the smallest memory. When allocating memory, there will be no waste in the linked list in the local pool; The difference of the existing memory, split the memory of the size of the difference in the upper memory pool, merge it into this linked list and then allocate it. Therefore, when implementing memory allocation, if the thread's demand for memory exceeds its corresponding linked list Memory, then the size of the excess memory is split in the upper memory pool. It can be seen that allocating memory on demand effectively solves the problem of resource waste. The embodiment of the present invention also provides a device for allocating memory, which can also achieve the above-mentioned technical effect.

附图说明Description of drawings

为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings that need to be used in the description of the embodiments or the prior art. Obviously, the accompanying drawings in the following description are only These are some embodiments of the present invention. Those skilled in the art can also obtain other drawings based on these drawings without creative work.

图1为本发明实施例公开的一种内存分配的方法;Fig. 1 is a method for memory allocation disclosed by an embodiment of the present invention;

图2为本发明实施例公开的一种具体的内存分配的方法;FIG. 2 is a specific memory allocation method disclosed by an embodiment of the present invention;

图3为本发明实施例公开的一种具体的内存分配的方法;FIG. 3 is a specific memory allocation method disclosed by an embodiment of the present invention;

图4为本发明实施例公开的内存页堆结构示意图;FIG. 4 is a schematic diagram of a memory page heap structure disclosed by an embodiment of the present invention;

图5为本发明实施例公开的一种池的结构示意图;Fig. 5 is a schematic structural view of a pool disclosed in an embodiment of the present invention;

图6为本发明实施例公开的一种内存分配的装置结构示意图。FIG. 6 is a schematic structural diagram of a device for memory allocation disclosed by an embodiment of the present invention.

具体实施方式detailed description

下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only some, not all, embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by persons of ordinary skill in the art without creative efforts fall within the protection scope of the present invention.

本发明实施例公开了一种内存分配的方法及装置,以在提高内存分配效率的同时,减少内存资源的浪费。The embodiment of the invention discloses a memory allocation method and device, so as to reduce waste of memory resources while improving memory allocation efficiency.

参见图1,本发明实施例提供的一种内存分配的方法,具体包括:Referring to FIG. 1, a memory allocation method provided by an embodiment of the present invention specifically includes:

S101,获取待分配内存的线程的所需内存值;S101, acquiring the required memory value of the thread to be allocated memory;

具体地,当线程需要分配内存时,首先获取这个线程所需要的内存的大小。Specifically, when a thread needs to allocate memory, first obtain the size of the memory required by the thread.

S102,判断所述所需内存值是否小于等于本地池中与所述线程对应的第一链表的第一内存值;其中,所述本地池中的一个链表对应一个线程,所述链表的内存值根据占用内存最小的线程确定;S102, judging whether the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool; wherein, a linked list in the local pool corresponds to a thread, and the memory value of the linked list Determined according to the thread that occupies the smallest memory;

需要说明的是,对于高并发的系统,如果需要比较频繁地分配和释放内存,需要考虑县城建锁的竞争以及内存分配效率,因此在分配内存时,优先考虑从本地池为其分配。本地池是一个有多个链表的内存池,其中,一个链表对应一个线程,当需要给一个线程分配内存时,优先从本地池中对应这个线程的链表中分配,这样线程之间就不会存在竞争,也就不需要加锁,有效的降低了对锁的竞争,提高了内存分配的效率。It should be noted that for a system with high concurrency, if you need to allocate and release memory more frequently, you need to consider the competition of county lock building and memory allocation efficiency. Therefore, when allocating memory, give priority to allocating it from the local pool. The local pool is a memory pool with multiple linked lists. One linked list corresponds to one thread. When memory needs to be allocated to a thread, it is first allocated from the linked list corresponding to the thread in the local pool, so that there will be no memory between threads. Competition means there is no need to add locks, which effectively reduces the competition for locks and improves the efficiency of memory allocation.

S103,若是,则利用所述第一链表为所述线程分配内存;S103, if so, using the first linked list to allocate memory for the thread;

具体地,如果一个线程所需要的内存小于等于它所对应的链表可以提供的内存,那么直接利用这个链表为线程分配内存。Specifically, if the memory required by a thread is less than or equal to the memory that its corresponding linked list can provide, then directly use this linked list to allocate memory for the thread.

S104,若否,则获取第一剩余内存值;所述第一剩余内存值为所述所需内存值与所述第一内存值的差值;S104. If not, acquire a first remaining memory value; the first remaining memory value is a difference between the required memory value and the first memory value;

具体地,如果这个线程对应的链表的内存不足以给这个线程,那么则计算出还需要多少内存,也就是说,计算出本地池中链表还需要多少内存才可以为对应的线程分配内存。Specifically, if the memory of the linked list corresponding to the thread is not enough for the thread, then calculate how much memory is needed, that is, calculate how much memory the linked list in the local pool still needs to allocate memory for the corresponding thread.

S105,在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。S105. Split the memory with the size of the first remaining memory value in the upper memory pool, merge the memory into the first linked list, and use the merged first linked list to allocate memory for the thread.

具体地,计算出第一剩余内存值后,向上层内存池中“索要”还需要的内存,上层的内存池拆分出第一剩余内存之大小的内存,然后将这部分内存合并到需要分配内存的线程对应的链表中,然后再利用这个合并了上层内存池中部分内存的链表为其分配内存。Specifically, after calculating the value of the first remaining memory, "ask" for the memory that is still needed from the upper-level memory pool, and the upper-level memory pool splits out the memory of the size of the first remaining memory, and then merges this part of memory into the memory that needs to be allocated. In the linked list corresponding to the thread of the memory, and then use this linked list that merges part of the memory in the upper memory pool to allocate memory for it.

通过以上方案可知,本发明实施例提供的内存分配的方法,将总内存分为了本地池和上层内存池,本地池中又有多个与线程对应的链表,线程与链表对应,解决了内存分配慢的问题;而每个线程的内存大小根据占用内存最小的线程确定,当分配内存时,本地池中链表不会有浪费;如果链表的内存不够时,可以按照需要总内存和链表中已有内存的差值,在上层内存池中拆分出差值大小的内存,合并到这个链表中再进行分配,因此,在实现内存分配时,如果线程对内存的需求超过了其对应链表的内存,那么则在上层内存池中拆分出超过的内存的大小,可见,按需分配内存,有效地解决了资源浪费的问题。It can be seen from the above scheme that the memory allocation method provided by the embodiment of the present invention divides the total memory into a local pool and an upper memory pool, and there are multiple linked lists corresponding to threads in the local pool, and threads correspond to linked lists, which solves the problem of memory allocation. The problem of slowness; while the memory size of each thread is determined according to the thread with the smallest memory usage. When allocating memory, there will be no waste in the linked list in the local pool; For the memory difference, the memory with the size of the difference is split in the upper memory pool, merged into this linked list and then allocated. Therefore, when implementing memory allocation, if the thread's demand for memory exceeds the memory of its corresponding linked list, Then the size of the excess memory is split in the upper memory pool. It can be seen that allocating memory on demand effectively solves the problem of resource waste.

本发明实施例提供一种具体的内存分配的方法,本发明实施例对上述实施例中的S105做了进一步的限定和说明,其他步骤内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,S105包括:The embodiment of the present invention provides a specific memory allocation method. The embodiment of the present invention further defines and explains S105 in the above-mentioned embodiment. The content of other steps is roughly the same as that of the above-mentioned embodiment. For details, please refer to the above-mentioned embodiment. The corresponding content will not be repeated here. Specifically, S105 includes:

在公共池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。Splitting the memory with the size of the first remaining memory value in the common pool, merging the memory into the first linked list, and using the combined first linked list to allocate memory for the thread.

需要说明的是,放本地池不足以为线程分配内存时,会从公共池进行分配,在公共池中拆分相应大小的内存合并到本地池对应的链表中,进而为线程分配内存。It should be noted that when the local pool is not enough to allocate memory for the thread, it will be allocated from the public pool, and the memory of the corresponding size will be split in the public pool and merged into the corresponding linked list of the local pool, and then memory will be allocated for the thread.

本发明实施例提供一种具体的内存分配的方法,本发明实施例对上述实施例中的S105做了进一步的限定和说明,其他步骤内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,参考图2,S105包括:The embodiment of the present invention provides a specific memory allocation method. The embodiment of the present invention further defines and explains S105 in the above-mentioned embodiment. The content of other steps is roughly the same as that of the above-mentioned embodiment. For details, please refer to the above-mentioned embodiment. The corresponding content will not be repeated here. Specifically, referring to FIG. 2, S105 includes:

S201,判断所述第一剩余内存值是否小于等于公共池的第二内存值;S201, judging whether the first remaining memory value is less than or equal to the second memory value of the common pool;

具体的,判断本地池不足以给线程分配内存的第一剩余内存值是不是小于等于公共池的内存值,也就是要判断公共池的内存是否可以满足本地池的需要。Specifically, it is judged whether the first remaining memory value that the local pool is not enough to allocate memory to the thread is less than or equal to the memory value of the public pool, that is, it is necessary to judge whether the memory of the public pool can meet the needs of the local pool.

S202,若是,则在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存;S202, if yes, split the memory with the size of the first remaining memory value in the public pool, merge it into the first linked list, and use the merged first linked list to allocate memory for the thread;

具体地,如果公共池中的内存可以满足本地池中不够为线程分配的内存大小,那么直接将公共池中的内存拆分到本地池中对应的链表中,以利用链表为线程分配内存。Specifically, if the memory in the common pool can meet the insufficient memory size allocated for the thread in the local pool, then directly split the memory in the public pool into the corresponding linked list in the local pool, so as to use the linked list to allocate memory for the thread.

S203,若否,则获取第二剩余内存值;所述第二内存值为所述第一剩余内存值与所述第二内存值的差值;S203. If not, acquire a second remaining memory value; the second memory value is a difference between the first remaining memory value and the second memory value;

如果本地池需要的内存公共池也不足以提供,则计算出公共池不足以提供的部分,作为第二剩余内存值,以便像内存页堆进行“索要”。If the memory required by the local pool is not enough for the public pool, calculate the insufficient part of the public pool as the second remaining memory value, so as to "request" like the memory page heap.

S204,在内存页堆中拆分与所述第二剩余内存值对应的内存页,并将所述内存页合并到所述公共池中;S204. Split the memory page corresponding to the second remaining memory value in the memory page heap, and merge the memory page into the common pool;

具体地,内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同。在内存页堆中拆分出公共池需要的内存,然后将这部分内存合并到公共池中。Specifically, the memory page heap includes multiple linked lists, each linked list includes multiple intervals, each interval is a preset number of continuous memory pages, and the number of memory pages in each interval in the same linked list is the same. Split the memory required by the public pool in the memory page heap, and then merge this part of memory into the public pool.

S205,继续执行所述在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存的步骤。S205. Continue to execute the step of splitting the memory with the size of the first remaining memory value in the public pool, merging it into the first linked list, and using the merged first linked list to allocate memory for the thread.

合并了内存页堆中部分内存的公共池的内存加本地池中对应链表的内存足以为线程分配。需要说明的是,为线程分配内存的最终还是本地池中与线程对应的链表,只是将内存页堆中的内存合并到公共池中,进一步将公共池的内存合并到本地池对应的链表中,从而在利用链表分配内存。The memory of the public pool combined with part of the memory in the memory page heap plus the memory of the corresponding linked list in the local pool are sufficient for thread allocation. It should be noted that the link list corresponding to the thread in the local pool is the one that allocates memory for the thread. It just merges the memory in the memory page heap into the common pool, and further merges the memory of the common pool into the linked list corresponding to the local pool. Thus, memory is allocated using a linked list.

需要说明的是,当释放内存时会在本地池比较当前空闲块个数是否已经大于最大值,若是已大于最大值会释放到公共池,若公共池也大于最大值,会释放回页堆。当某个页的区间全部释放后,会释放回系统。It should be noted that when the memory is released, it will compare whether the current number of free blocks is greater than the maximum value in the local pool. If it is greater than the maximum value, it will be released to the public pool. If the public pool is also greater than the maximum value, it will be released back to the page heap. When all the range of a page is released, it will be released back to the system.

由此可见,既可以解决高并发情况下内存分配对锁的竞争,同时也能做到按量的分配内存,避免不必要的浪费。It can be seen that it can not only solve the competition of memory allocation for locks in the case of high concurrency, but also allocate memory according to the amount to avoid unnecessary waste.

下面对本发明实施例提供的一种具体的内存分配的方法,本发明实施例对上述实施例中的S204做了进一步的限定和说明,其他步骤内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,参考图3,S204包括:The following is a specific memory allocation method provided by the embodiment of the present invention. The embodiment of the present invention further defines and explains S204 in the above embodiment. The content of other steps is roughly the same as that of the above embodiment. For details, please refer to The content corresponding to the embodiment will not be repeated here. Specifically, referring to FIG. 3, S204 includes:

S301,利用所述第二剩余内存值确定内存页数;S301. Determine the number of memory pages by using the second remaining memory value;

具体地,根据公共池还需要的内存值,确定这个内存值是几页的内存。Specifically, according to the memory value still required by the public pool, it is determined how many pages of memory this memory value is.

S302,利用所述内存页数在内存页堆中确定第二链表;其中所述内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同;S302, using the number of memory pages to determine a second linked list in the memory page heap; wherein the memory page heap includes multiple linked lists, each linked list includes multiple intervals, and each interval is a preset number of continuous memory page, the number of memory pages in each interval in the same linked list is the same;

具体地,参考图4,图为内存页堆的结构示意图,分别有1页、2页等等的链表,1页的链表中,每个区间只有1页,2页的链表中每个区间都是连续的2页,以此类推。Specifically, refer to Figure 4, which is a schematic diagram of the structure of the memory page heap. There are linked lists of 1 page, 2 pages, etc., in the linked list of 1 page, each interval has only 1 page, and in the linked list of 2 pages, each interval is are 2 consecutive pages, and so on.

公共池需要几页的内存,就在对应的链表中取出相应页数的内存页;例如公共池需要2页内存,那么就在内存页堆中确定应在2pages的链表中拆分区间。If the public pool needs several pages of memory, take out the memory pages of the corresponding number of pages from the corresponding linked list; for example, if the public pool needs 2 pages of memory, then determine in the memory page heap that the interval should be split in the linked list of 2pages.

S303,在所述第二链表中拆分一个区间的所有内存页,并将拆分的内存页合并到所述公共池中。S303. Split all memory pages of a section in the second linked list, and merge the split memory pages into the common pool.

具体地,确定了第二链表后,则在第二链表中拆分出对应的区间,将区间内所有的内存页合并至公共池,以便公共池进一步拆分给本地池的链表,从而给对应的线程分配内存。Specifically, after the second linked list is determined, the corresponding section is split in the second linked list, and all memory pages in the section are merged into the public pool, so that the public pool can be further split into the linked list of the local pool, so that the corresponding The thread allocates memory.

由于区间中的内存页为连续的,因此当需要一定量的连续内存时,从最满足条件的页区间去分配,减少了内存碎片数量。Since the memory pages in the range are continuous, when a certain amount of continuous memory is needed, it is allocated from the most satisfying page range, reducing the number of memory fragments.

本发明实施例提供一种具体的内存分配的方法,本发明实施例对上述实施例中的S303做了进一步的限定和说明,其他步骤内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,S303包括:The embodiment of the present invention provides a specific memory allocation method. The embodiment of the present invention further defines and explains S303 in the above-mentioned embodiment. The content of other steps is roughly the same as that of the above-mentioned embodiment. For details, please refer to the above-mentioned embodiment. The corresponding content will not be repeated here. Specifically, S303 includes:

根据所述线程确定pageID,利用基数树确定所述pageID对应的区间,在所述第二链表中拆分所述区间的内存页,并将拆分的内存页合并到所述公共池中。Determine the pageID according to the thread, use the radix tree to determine the section corresponding to the pageID, split the memory pages of the section in the second linked list, and merge the split memory pages into the common pool.

具体地,为了快速按照pageID找到公共池所需要的内存所在的区间,使用一颗二层的基数树来维护这些映射关系。Specifically, in order to quickly find the range of the memory required by the public pool according to the pageID, a two-layer radix tree is used to maintain these mapping relationships.

例如x86-64系统只使用了48位内存,我们使用的页大小是Linux内存页大小的两倍即8K,所以我们需要维护的页号为48-13=35位。我们将它分成两层,一层为18,一层为17,具体实现方式如下:For example, the x86-64 system only uses 48-bit memory, and the page size we use is twice the size of the Linux memory page, that is, 8K, so the page number we need to maintain is 48-13=35 bits. We divide it into two layers, one layer is 18, and the other is 17. The specific implementation is as follows:

这样我们就可以维护起所有的页,并且只有当已分配到某些页时,才建立叶子节点,不会花费太多内存用于管理这些映射关系。In this way, we can maintain all the pages, and only when some pages have been allocated, the leaf nodes will be established, without spending too much memory for managing these mapping relationships.

当给定一个pageID,例如k,我们可以按照下述方式查找内存页的区间:When given a pageID, such as k, we can look up the range of the memory page as follows:

i1=k>>LEAF_BITS;i1=k>>LEAF_BITS;

i2=k&(LEAF_LENGTH–1);i2=k&(LEAF_LENGTH–1);

range=root[i1]->values[i2];range=root[i1]->values[i2];

本发明实施例提供一种具体的内存分配的方法,本发明实施例对上述实施例中的第一链表做了进一步的限定和说明,其他步骤内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,第一链表包括第一链表。The embodiment of the present invention provides a specific memory allocation method. The embodiment of the present invention further defines and explains the first linked list in the above embodiment. The content of other steps is roughly the same as that of the above embodiment. For details, please refer to the above The content corresponding to the embodiment will not be repeated here. Specifically, the first linked list includes a first linked list.

具体的,参考图5,池是按内存块大小维护的空闲链表。按8字节16,24,32…,128,…,1024维护的一系列大小的内存块的空闲链表。当需要分配特定大小的内存时,会从对应的大小的链表寻找一个块进行分配。当释放时,会放回到该表中去。当某个大小的块不够用时,会从更大的块的链表上进行分配,并将剩余的拆分到较小的内存块空闲链表中去。当更大的块链表依旧不能满足时,会从公共池分配若干个该大小的块,并补充到本地池中去。Specifically, referring to FIG. 5 , the pool is a free linked list maintained according to the size of the memory block. A free list of memory blocks of a series of sizes maintained by 8 bytes 16, 24, 32..., 128,..., 1024. When it is necessary to allocate memory of a specific size, it will find a block from the linked list of the corresponding size for allocation. When released, it will be put back into the table. When a block of a certain size is not enough, it will be allocated from a linked list of a larger block, and the rest will be split into a free list of smaller memory blocks. When the larger block list is still not enough, several blocks of this size will be allocated from the public pool and added to the local pool.

下面对本发明实施例提供的一种内存分配的装置进行介绍,下文描述的一种内存分配的装置与上文描述的一种内存分配的方法可以相互参考。A memory allocation device provided by an embodiment of the present invention is introduced below, and the memory allocation device described below and the memory allocation method described above may refer to each other.

参考图6,一种内存分配的装置,具体包括:Referring to Figure 6, a memory allocation device specifically includes:

所需内存值获取模块401,用于获取待分配内存的线程的所需内存值;The required memory value obtaining module 401 is used to obtain the required memory value of the thread to be allocated memory;

具体地,当线程需要分配内存时,所需内存值获取模块401获取这个线程所需要的内存的大小。Specifically, when a thread needs to allocate memory, the required memory value obtaining module 401 obtains the size of the memory required by the thread.

判断模块402,用于判断所述所需内存值是否小于等于本地池中与所述线程对应的第一链表的第一内存值;其中,所述本地池中的一个链表对应一个线程,所述链表的内存值根据占用内存最小的线程确定;A judging module 402, configured to judge whether the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool; wherein, a linked list in the local pool corresponds to a thread, and the The memory value of the linked list is determined according to the thread that occupies the smallest memory;

需要说明的是,对于高并发的系统,如果需要比较频繁地分配和释放内存,需要考虑县城建锁的竞争以及内存分配效率,因此在分配内存时,优先考虑从本地池为其分配。本地池是一个有多个链表的内存池,其中,一个链表对应一个线程,当需要给一个线程分配内存时,优先从本地池中对应这个线程的链表中分配,这样线程之间就不会存在竞争,也就不需要加锁,有效的降低了对锁的竞争,提高了内存分配的效率。It should be noted that for a system with high concurrency, if you need to allocate and release memory more frequently, you need to consider the competition of county locks and memory allocation efficiency. Therefore, when allocating memory, give priority to allocating it from the local pool. The local pool is a memory pool with multiple linked lists. One linked list corresponds to one thread. When memory needs to be allocated to a thread, it is first allocated from the linked list corresponding to the thread in the local pool, so that there will be no memory between threads. Competition means there is no need to add locks, which effectively reduces the competition for locks and improves the efficiency of memory allocation.

分配模块403,用于所述所需内存值小于等于本地池中与所述线程对应的第一链表的第一内存值时,利用所述第一链表为所述线程分配内存;An allocation module 403, configured to use the first linked list to allocate memory for the thread when the required memory value is less than or equal to the first memory value of the first linked list corresponding to the thread in the local pool;

具体地,如果一个线程所需要的内存小于等于它所对应的链表可以提供的内存,那么分配模块403直接利用这个链表为线程分配内存。Specifically, if the memory required by a thread is less than or equal to the memory that its corresponding linked list can provide, then the allocation module 403 directly uses this linked list to allocate memory for the thread.

第一剩余内存值获取模块404,用于所述所需内存值大于本地池中与所述线程对应的第一链表的第一内存值时,获取第一剩余内存值;所述第一剩余内存值为所述所需内存值与所述第一内存值的差值;The first remaining memory value acquisition module 404 is used to obtain the first remaining memory value when the required memory value is greater than the first memory value of the first linked list corresponding to the thread in the local pool; the first remaining memory value The value is the difference between the required memory value and the first memory value;

具体地,如果这个线程对应的链表的内存不足以给这个线程,那么第一剩余内存值获取模块404计算出还需要多少内存,也就是说,计算出本地池中链表还需要多少内存才可以为对应的线程分配内存。Specifically, if the memory of the linked list corresponding to this thread is not enough for this thread, then the first remaining memory value acquisition module 404 calculates how much memory is still needed, that is to say, calculates how much memory is still needed for the linked list in the local pool. The corresponding thread allocates memory.

第二分配模块405,用于在上层内存池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。The second allocating module 405 is configured to split the memory with the size of the first remaining memory value in the upper memory pool, merge the memory into the first linked list, and use the merged first linked list for the Threads allocate memory.

具体地,计算出第一剩余内存值后,第二分配模块405向上层内存池中“索要”还需要的内存,上层的内存池拆分出第一剩余内存之大小的内存,然后将这部分内存合并到需要分配内存的线程对应的链表中,然后再利用这个合并了上层内存池中部分内存的链表为其分配内存。Specifically, after calculating the value of the first remaining memory, the second allocating module 405 "asks" for memory that is still needed from the memory pool of the upper layer, and the memory pool of the upper layer splits out the memory with the size of the first remaining memory, and then allocates this part The memory is merged into the linked list corresponding to the thread that needs to allocate memory, and then the linked list that merges part of the memory in the upper memory pool is used to allocate memory for it.

通过以上方案可知,本发明实施例提供的内存分配的方法,将总内存分为了本地池和上层内存池,本地池中又有多个与线程对应的链表,线程与链表对应,解决了内存分配慢的问题;而每个线程的内存大小根据占用内存最小的线程确定,当分配内存时,本地池中链表不会有浪费;如果链表的内存不够时,可以利用第二分配模块405按照需要总内存和链表中已有内存的差值,在上层内存池中拆分出差值大小的内存,合并到这个链表中再进行分配,因此,在实现内存分配时,如果线程对内存的需求超过了其对应链表的内存,那么则在上层内存池中拆分出超过的内存的大小,可见,按需分配内存,有效地解决了资源浪费的问题。It can be seen from the above scheme that the memory allocation method provided by the embodiment of the present invention divides the total memory into a local pool and an upper memory pool, and there are multiple linked lists corresponding to threads in the local pool, and threads correspond to linked lists, which solves the problem of memory allocation. slow problem; and the memory size of each thread is determined according to the thread that occupies the smallest memory. When allocating memory, the linked list in the local pool will not be wasted; if the memory of the linked list is not enough, the second distribution module 405 can be utilized to total The difference between the memory and the existing memory in the linked list, the memory of the size of the difference is split in the upper memory pool, merged into this linked list and then allocated. Therefore, when implementing memory allocation, if the thread's demand for memory exceeds It corresponds to the memory of the linked list, then the size of the excess memory is split in the upper memory pool. It can be seen that allocating memory on demand effectively solves the problem of resource waste.

本发明实施例提供一种具体的内存分配的装置,本发明实施例对上述实施例中的第二分配模块405做了进一步的限定和说明,其他模块内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,第二分配模块405具体用于,在公共池中拆分所述第一剩余内存值大小的内存,将所述内存合并到所述第一链表中,并利用合并后的第一链表为所述线程分配内存。The embodiment of the present invention provides a specific memory allocation device. The embodiment of the present invention further defines and explains the second allocation module 405 in the above embodiment. The content of other modules is roughly the same as that of the above embodiment. The specific content can be Refer to the content corresponding to the foregoing embodiments, and details are not repeated here. Specifically, the second allocating module 405 is specifically configured to split the memory with the size of the first remaining memory value in the public pool, merge the memory into the first linked list, and use the merged first linked list Allocate memory for said thread.

需要说明的是,本地池不足以为线程分配内存时,第二分配模块405会从公共池进行分配,在公共池中拆分相应大小的内存合并到本地池对应的链表中,进而为线程分配内存。It should be noted that when the local pool is not enough to allocate memory for the thread, the second allocation module 405 will allocate from the public pool, split the memory of the corresponding size in the public pool and merge it into the linked list corresponding to the local pool, and then allocate memory for the thread .

本发明实施例提供一种具体的内存分配的装置,本发明实施例对上述实施例中的第二分配模块405做了进一步的限定和说明,其他模块内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,第二分配模块405包括:The embodiment of the present invention provides a specific memory allocation device. The embodiment of the present invention further defines and explains the second allocation module 405 in the above embodiment. The content of other modules is roughly the same as that of the above embodiment. The specific content can be Refer to the content corresponding to the foregoing embodiments, and details are not repeated here. Specifically, the second allocation module 405 includes:

判断单元,用于判断所述第一剩余内存值是否小于等于公共池的第二内存值;A judging unit, configured to judge whether the first remaining memory value is less than or equal to the second memory value of the common pool;

具体的,判断单元判断本地池不足以给线程分配内存的第一剩余内存值是不是小于等于公共池的内存值,也就是要判断公共池的内存是否可以满足本地池的需要。Specifically, the judging unit judges whether the first remaining memory value that the local pool is not enough to allocate memory to the thread is less than or equal to the memory value of the public pool, that is, judges whether the memory of the public pool can meet the needs of the local pool.

第一合并单元,用于所述第一剩余内存值小于等于公共池的第二内存值时,在公共池中拆分所述第一剩余内存值大小的内存,合并到所述第一链表中,并利用利用合并后的第一链表为所述线程分配内存;The first merging unit is used for splitting the memory with the size of the first remaining memory value in the common pool when the first remaining memory value is less than or equal to the second memory value of the public pool, and merging it into the first linked list , and utilize the combined first linked list to allocate memory for the thread;

具体地,如果公共池中的内存可以满足本地池中不够为线程分配的内存大小,那么第一合并单元直接将公共池中的内存拆分到本地池中对应的链表中,以利用链表为线程分配内存。Specifically, if the memory in the public pool can satisfy the memory size that is not enough for the thread allocation in the local pool, then the first merging unit directly splits the memory in the public pool into the corresponding linked list in the local pool, so as to use the linked list as a link for the thread. Allocate memory.

第二剩余内存值获取单元,用于在所述第一剩余内存值大于公共池的第二内存值时,获取第二剩余内存值;所述第二内存值为所述第一剩余内存值与所述第二内存值的差值;A second remaining memory value obtaining unit, configured to obtain a second remaining memory value when the first remaining memory value is greater than a second memory value of the public pool; the second remaining memory value is equal to the first remaining memory value a difference between said second memory value;

如果本地池需要的内存公共池也不足以提供,则第二剩余内存值获取单元计算出公共池不足以提供的部分,作为第二剩余内存值,以便像内存页堆进行“索要”。If the public pool of memory required by the local pool is not enough to provide, the second remaining memory value obtaining unit calculates the insufficient part of the public pool as the second remaining memory value, so as to "request" like the memory page heap.

第二合并单元,用于在内存页堆中拆分与所述第二剩余内存值对应的内存页,并将所述内存页合并到所述公共池中;继续调用所述第一合并单元;The second merging unit is configured to split the memory page corresponding to the second remaining memory value in the memory page heap, and merge the memory page into the common pool; continue to call the first merging unit;

具体地,内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同。第二合并单元在内存页堆中拆分出公共池需要的内存,然后将这部分内存合并到公共池中。合并了内存页堆中部分内存的公共池的内存加本地池中对应链表的内存足以为线程分配。Specifically, the memory page heap includes multiple linked lists, each linked list includes multiple intervals, each interval is a preset number of continuous memory pages, and the number of memory pages in each interval in the same linked list is the same. The second merging unit splits memory required by the common pool in the memory page heap, and then merges this part of memory into the common pool. The memory of the public pool combined with part of the memory in the memory page heap plus the memory of the corresponding linked list in the local pool are sufficient for thread allocation.

需要说明的是,为线程分配内存的最终还是本地池中与线程对应的链表,只是将内存页堆中的内存合并到公共池中,进一步将公共池的内存合并到本地池对应的链表中,从而在利用链表分配内存。It should be noted that the link list corresponding to the thread in the local pool is the one that allocates memory for the thread. It just merges the memory in the memory page heap into the common pool, and further merges the memory of the common pool into the linked list corresponding to the local pool. Thus, memory is allocated using a linked list.

当释放内存时会在本地池比较当前空闲块个数是否已经大于最大值,若是已大于最大值会释放到公共池,若公共池也大于最大值,会释放回页堆。当某个页的区间全部释放后,会释放回系统。When releasing memory, it will compare whether the current number of free blocks is greater than the maximum value in the local pool. If it is greater than the maximum value, it will be released to the public pool. If the public pool is also greater than the maximum value, it will be released back to the page heap. When all the range of a page is released, it will be released back to the system.

由此可见,既可以解决高并发情况下内存分配对锁的竞争,同时也能做到按量的分配内存,避免不必要的浪费。It can be seen that it can not only solve the competition of memory allocation for locks in the case of high concurrency, but also allocate memory according to the amount to avoid unnecessary waste.

下面对本发明实施例提供的一种具体的内存分配的装置,本发明实施例对上述实施例中的第二合并单元做了进一步的限定和说明,其他内容与上述实施例大致相同,具体内容可以参考上述实施例对应的内容,此处不再赘述。具体地,第二合并单元包括:The following is a specific memory allocation device provided by the embodiment of the present invention. The embodiment of the present invention further defines and explains the second merging unit in the above embodiment. The other contents are roughly the same as the above embodiment, and the specific content can be Refer to the content corresponding to the foregoing embodiments, and details are not repeated here. Specifically, the second merging unit includes:

内存页数确定子单元,用于利用所述第二剩余内存值确定内存页数;a subunit for determining the number of memory pages, configured to determine the number of memory pages by using the second remaining memory value;

具体地,内存页数确定子单元根据公共池还需要的内存值,确定这个内存值是几页的内存。Specifically, the subunit for determining the number of memory pages determines how many pages of memory this memory value is according to the memory value still required by the public pool.

第二链表确定子单元,用于利用所述内存页数在内存页堆中确定第二链表;其中所述内存页堆中包括多个链表,每个链表中包括多个区间,每个区间为预设个数的连续内存页,同一链表中每个区间的内存页数相同;The second linked list determination subunit is used to determine the second linked list in the memory page heap by using the number of memory pages; wherein the memory page heap includes a plurality of linked lists, and each linked list includes a plurality of intervals, and each interval is The preset number of continuous memory pages, the number of memory pages in each interval in the same linked list is the same;

具体地,公共池需要几页的内存,第二链表确定子单元就确定对应的链表,以便取出相应页数的内存页;例如公共池需要2页内存,那么就在内存页堆中确定应在2pages的链表中拆分区间。Specifically, the public pool needs several pages of memory, and the second linked list determination subunit determines the corresponding linked list so as to take out the memory pages of the corresponding number of pages; for example, if the public pool needs 2 pages of memory, then it is determined in the memory page heap that the Split the interval in the linked list of 2pages.

合并子单元,用于在所述第二链表中拆分一个区间的所有内存页,并将拆分的内存页合并到所述公共池中。The merging subunit is used to split all the memory pages of a range in the second linked list, and merge the split memory pages into the public pool.

具体地,确定了第二链表后,则合并子单元在第二链表中拆分出对应的区间,将区间内所有的内存页合并至公共池,以便公共池进一步拆分给本地池的链表,从而给对应的线程分配内存。Specifically, after the second linked list is determined, the merging subunit splits the corresponding interval in the second linked list, and merges all the memory pages in the interval into the public pool, so that the public pool can be further split into the linked list of the local pool, So as to allocate memory to the corresponding thread.

由于区间中的内存页为连续的,因此当需要一定量的连续内存时,从最满足条件的页区间去分配,减少了内存碎片数量。Since the memory pages in the range are continuous, when a certain amount of continuous memory is needed, it is allocated from the most satisfying page range, reducing the number of memory fragments.

本说明书中各个实施例采用递进的方式描述,每个实施例重点说明的都是与其他实施例的不同之处,各个实施例之间相同相似部分互相参见即可。Each embodiment in this specification is described in a progressive manner, each embodiment focuses on the difference from other embodiments, and the same and similar parts of each embodiment can be referred to each other.

对所公开的实施例的上述说明,使本领域专业技术人员能够实现或使用本发明。对这些实施例的多种修改对本领域的专业技术人员来说将是显而易见的,本文中所定义的一般原理可以在不脱离本发明的精神或范围的情况下,在其它实施例中实现。因此,本发明将不会被限制于本文所示的这些实施例,而是要符合与本文所公开的原理和新颖特点相一致的最宽的范围。The above description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the present invention will not be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (10)

  1. A kind of 1. method of Memory Allocation, it is characterised in that including:
    Obtain the required memory value of the thread of internal memory to be allocated;
    Judge whether the required memory value is less than or equal to the first internal memory of the first chained list corresponding with the thread in local pond Value;Wherein, the corresponding thread of a chained list in the local pond, the memory value of the chained list is according to committed memory minimum Thread determines;
    If so, it is then the thread storage allocation using first chained list;
    If it is not, then obtain the first free memory value;The first free memory value is in the required memory value and described first Deposit the difference of value;
    The internal memory of the first free memory value size is split in the memory pool of upper strata, the internal memory is merged into first chain In table, and it is the thread storage allocation using the first chained list after merging.
  2. 2. according to the method for claim 1, it is characterised in that described to be split in the memory pool of upper strata in first residue The internal memory of value size is deposited, the internal memory is merged into first chained list, and is the line using the first chained list after merging Journey storage allocation, including:
    The internal memory of the first free memory value size is split in common pool, the internal memory is merged into first chained list In, and be the thread storage allocation using the first chained list after merging.
  3. 3. according to the method for claim 2, it is characterised in that described to be split in the memory pool of upper strata in first residue The internal memory of value size is deposited, the internal memory is merged into first chained list, and is the line using the first chained list after merging Journey storage allocation, including:
    Judge whether the first free memory value is less than or equal to the second memory value of common pool;
    If so, then splitting the internal memory of the first free memory value size in common pool, it is merged into first chained list, and It is the thread storage allocation using using the first chained list after merging;
    If it is not, then obtain the second free memory value;Second memory value is in the first free memory value and described second Deposit the difference of value;
    Split corresponding with the second free memory value page in page heap, and described in the page is merged into In common pool;
    The internal memory that the first free memory value size is split in common pool is continued executing with, is merged into first chained list In, and using using merge after the first chained list be the thread storage allocation the step of.
  4. 4. according to the method for claim 3, it is characterised in that it is described in page heap split with second residue in Page corresponding to value is deposited, and the page is merged into the common pool, including:
    Internal memory number of pages is determined using the second free memory value;
    Deposited inside using the internal memory number of pages and the second chained list is determined in page heap;Wherein described page heap includes multiple chained lists, Each chained list includes multiple sections, and each section is the contiguous memory page of predetermined number, and each section is interior in same chained list It is identical to deposit number of pages;
    All pages in a section are split in second chained list, and the page of fractionation is merged into the common pool In.
  5. 5. according to the method for claim 4, it is characterised in that the page in a section is split in second chained list, And the page of fractionation is merged into the common pool, including:
    PageID is determined according to the thread, section corresponding to the pageID is determined using radix tree, in second chained list It is middle to split the page in the section, and the page of fractionation is merged into the common pool.
  6. 6. method as claimed in any of claims 1 to 5, it is characterised in that first chained list includes at least one Individual memory block.
  7. A kind of 7. device of Memory Allocation, it is characterised in that including:
    Required memory value acquisition module, the required memory value of the thread for obtaining internal memory to be allocated;
    Judge module, for judging whether the required memory value is less than or equal to the first chain corresponding with the thread in local pond First memory value of table;Wherein, the corresponding thread of a chained list in the local pond, the memory value of the chained list is according to accounting for Determined with the minimum thread of internal memory;
    Distribute module, it is less than or equal to first of the first chained list corresponding with the thread in local pond for the required memory value It is the thread storage allocation using first chained list during memory value;
    First free memory value acquisition module, it is more than for the required memory value corresponding with the thread first in local pond During the first memory value of chained list, the first free memory value is obtained;The first free memory value is the required memory value and institute State the difference of the first memory value;
    Second distribute module, will be described interior for splitting the internal memory of the first free memory value size in the memory pool of upper strata Deposit and be merged into first chained list, and be the thread storage allocation using the first chained list after merging.
  8. 8. device according to claim 7, it is characterised in that second distribute module, be specifically used for, in common pool The internal memory of the first free memory value size is split, the internal memory is merged into first chained list, and after utilization merging The first chained list be the thread storage allocation.
  9. 9. device according to claim 8, it is characterised in that second distribute module, including:
    Judging unit, for judging whether the first free memory value is less than or equal to the second memory value of common pool;
    First combining unit, when being less than or equal to the second memory value of common pool for the first free memory value, in common pool The middle internal memory for splitting the first free memory value size, is merged into first chained list, and using utilizing the after merging One chained list is the thread storage allocation;
    Second free memory value acquiring unit, for the first free memory value be more than common pool the second memory value when, Obtain the second free memory value;Second memory value is the first free memory value and the difference of second memory value;
    Second combining unit, for splitting corresponding with the second free memory value page in page heap, and by institute Page is stated to be merged into the common pool;Continue to call first combining unit.
  10. 10. device according to claim 9, it is characterised in that second combining unit, including:
    Internal memory number of pages determination subelement, for determining internal memory number of pages using the second free memory value;
    Second chained list determination subelement, the second chained list is determined in page heap for being deposited inside using the internal memory number of pages;It is wherein described Page heap includes multiple chained lists, and each chained list includes multiple sections, and each section is the contiguous memory page of predetermined number, The internal memory number of pages in each section is identical in same chained list;
    Merge subelement, for splitting all pages in a section in second chained list, and by the page of fractionation It is merged into the common pool.
CN201710797453.5A 2017-08-31 2017-08-31 Method and device for allocating memory Pending CN107515788A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201710797453.5A CN107515788A (en) 2017-08-31 2017-08-31 Method and device for allocating memory

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201710797453.5A CN107515788A (en) 2017-08-31 2017-08-31 Method and device for allocating memory

Publications (1)

Publication Number Publication Date
CN107515788A true CN107515788A (en) 2017-12-26

Family

ID=60725135

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201710797453.5A Pending CN107515788A (en) 2017-08-31 2017-08-31 Method and device for allocating memory

Country Status (1)

Country Link
CN (1) CN107515788A (en)

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109375985A (en) * 2018-09-06 2019-02-22 新华三技术有限公司成都分公司 Dynamic memory management method and device
CN110209489A (en) * 2018-02-28 2019-09-06 贵州白山云科技股份有限公司 A kind of EMS memory management process and device suitable for memory page structure
CN110727517A (en) * 2019-10-12 2020-01-24 福建顶点软件股份有限公司 Memory allocation method and device based on partition design
CN112052079A (en) * 2020-07-30 2020-12-08 视联动力信息技术股份有限公司 Service execution method, apparatus, terminal device and storage medium
CN113051081A (en) * 2021-06-01 2021-06-29 广东省新一代通信与网络创新研究院 Event state management method, system and storage medium based on memory pool
CN113268349A (en) * 2021-06-04 2021-08-17 科东(广州)软件科技有限公司 Computer memory management method, device, equipment and storage medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6058460A (en) * 1996-06-28 2000-05-02 Sun Microsystems, Inc. Memory allocation in a multithreaded environment
CN102455974A (en) * 2010-10-21 2012-05-16 上海宝信软件股份有限公司 High-speed internal memory application and release management system with controllable internal memory consumption and high-speed internal memory application release management method
CN102567107A (en) * 2011-10-31 2012-07-11 广东电网公司电力科学研究院 Highly-concurrent real-time memory resource management and scheduling method
CN102915276A (en) * 2012-09-25 2013-02-06 武汉邮电科学研究院 Memory control method for embedded systems
CN103914265A (en) * 2014-04-09 2014-07-09 江苏物联网研究发展中心 Cluster fine-grained memory management method
CN104881324A (en) * 2014-09-28 2015-09-02 北京匡恩网络科技有限责任公司 Memory management method in multi-thread environment
CN105988876A (en) * 2015-03-27 2016-10-05 杭州迪普科技有限公司 Memory allocation method and apparatus

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6058460A (en) * 1996-06-28 2000-05-02 Sun Microsystems, Inc. Memory allocation in a multithreaded environment
CN102455974A (en) * 2010-10-21 2012-05-16 上海宝信软件股份有限公司 High-speed internal memory application and release management system with controllable internal memory consumption and high-speed internal memory application release management method
CN102567107A (en) * 2011-10-31 2012-07-11 广东电网公司电力科学研究院 Highly-concurrent real-time memory resource management and scheduling method
CN102915276A (en) * 2012-09-25 2013-02-06 武汉邮电科学研究院 Memory control method for embedded systems
CN103914265A (en) * 2014-04-09 2014-07-09 江苏物联网研究发展中心 Cluster fine-grained memory management method
CN104881324A (en) * 2014-09-28 2015-09-02 北京匡恩网络科技有限责任公司 Memory management method in multi-thread environment
CN105988876A (en) * 2015-03-27 2016-10-05 杭州迪普科技有限公司 Memory allocation method and apparatus

Cited By (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110209489A (en) * 2018-02-28 2019-09-06 贵州白山云科技股份有限公司 A kind of EMS memory management process and device suitable for memory page structure
CN109375985A (en) * 2018-09-06 2019-02-22 新华三技术有限公司成都分公司 Dynamic memory management method and device
CN110727517A (en) * 2019-10-12 2020-01-24 福建顶点软件股份有限公司 Memory allocation method and device based on partition design
CN112052079A (en) * 2020-07-30 2020-12-08 视联动力信息技术股份有限公司 Service execution method, apparatus, terminal device and storage medium
CN113051081A (en) * 2021-06-01 2021-06-29 广东省新一代通信与网络创新研究院 Event state management method, system and storage medium based on memory pool
CN113051081B (en) * 2021-06-01 2021-10-29 广东省新一代通信与网络创新研究院 Event state management method, system and storage medium based on memory pool
CN113268349A (en) * 2021-06-04 2021-08-17 科东(广州)软件科技有限公司 Computer memory management method, device, equipment and storage medium

Similar Documents

Publication Publication Date Title
CN107515788A (en) Method and device for allocating memory
CN104182502B (en) A kind of data pick-up method and device
US11099982B2 (en) NUMA-aware garbage collection
US7908454B2 (en) Application-specific heap management
TWI690851B (en) Computer system resource configuration method and device
CN106095590B (en) A kind of method for allocating tasks and device based on thread pool
CN108038002A (en) A kind of embedded software EMS memory management process
CN105975398A (en) Method for memory fragmentation management
EP0817044A3 (en) Memory allocation in a multithreaded environment
CN107844372B (en) Memory allocation method and system
CN106844050A (en) A kind of memory allocation method and device
JP2017538194A5 (en)
CN107533435A (en) The distribution method and storage device of memory space
CN106406762A (en) A repeated data deleting method and device
CN108829523A (en) Memory source distribution method, device, electronic equipment and readable storage medium storing program for executing
CN109766171A (en) Task processing method, apparatus, device and storage medium
CN103455433A (en) Memory management method and system
CN108920105B (en) Community structure-based distributed storage method and device for graph data
CN103488577A (en) Method and device of memory allocation and batch recovery for user applications based on use numbering
CN112685333B (en) Heap memory management method and device
CN113590332A (en) Memory management method and device and memory distributor
CN107291371B (en) Method and device for implementing a read-write lock
CN108429704A (en) A node resource allocation method and device
CN108595259A (en) A kind of internal memory pool managing method based on global administration
CN110825732A (en) Data query method and device, computer equipment and readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication

Application publication date: 20171226

RJ01 Rejection of invention patent application after publication