CN113448720A - Memory allocation method, device, equipment and storage medium - Google Patents

Memory allocation method, device, equipment and storage medium Download PDF

Info

Publication number
CN113448720A
CN113448720A CN202010231780.6A CN202010231780A CN113448720A CN 113448720 A CN113448720 A CN 113448720A CN 202010231780 A CN202010231780 A CN 202010231780A CN 113448720 A CN113448720 A CN 113448720A
Authority
CN
China
Prior art keywords
memory
pool
application
address
identifier
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
CN202010231780.6A
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.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen 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 Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN202010231780.6A priority Critical patent/CN113448720A/en
Publication of CN113448720A publication Critical patent/CN113448720A/en
Pending legal-status Critical Current

Links

Images

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
    • 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/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating
    • G06F9/44505Configuring for program initiating, e.g. using registry, configuration files

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 embodiment of the application discloses a memory allocation method, a device, equipment and a storage medium, wherein the method comprises the following steps: when an application program starting request is received, a plurality of memory pools, a first mapping relation and a second mapping relation are constructed, each memory pool comprises a plurality of memory blocks, the first mapping relation records the mapping relation between an application size interval and a memory pool identifier, and the second mapping relation records the mapping relation between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. After a memory application request sent by an application program is received, acquiring the memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size and a first mapping relation; and acquiring a first memory address according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address, so that the application program can use the memory block corresponding to the first memory address.

Description

Memory allocation method, device, equipment and storage medium
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method, an apparatus, a device, and a storage medium for allocating memory.
Background
Memory allocation refers to allocating or recycling a storage space in the execution process of an application program. For example, during the running process of an application (e.g., a game, a music player, etc.), it is necessary to allocate memory to the application in order to ensure that the target application can run smoothly.
Currently, a default system memory allocation scheme is usually used for memory allocation, and the system memory allocation scheme responds to memory application requests of various sizes. However, because the efficiency of the memory application and release process of the system memory allocation scheme is low, when the system memory allocation scheme processes application tasks (for example, loading game level cards and the like) in which the memory application is released frequently, it takes a long time to respond to the memory allocation request of the application task, which results in a long time for completing the application task, and thus the running smoothness of the application is low.
Disclosure of Invention
The embodiment of the application provides a memory allocation method, a memory allocation device, memory allocation equipment and a memory medium, which can improve memory allocation efficiency, so that the operation smoothness of an application program is improved.
In view of the above, a first aspect of the present application provides a memory allocation method, including:
when an application program starting request is received, constructing N memory pools, a first mapping relation and a second mapping relation; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
after receiving a memory application request sent by the application program, obtaining a memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
A second aspect of the present application provides a memory allocation apparatus, including:
the device comprises a construction unit, a first mapping unit and a second mapping unit, wherein the construction unit is used for constructing N memory pools, a first mapping relation and a second mapping relation when an application program starting request is received; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
the first processing unit is used for acquiring the memory application size of the application program according to the memory application request after receiving the memory application request sent by the application program, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and the allocation unit is used for acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
A third aspect of the application provides an apparatus comprising a processor and a memory:
the memory is used for storing a computer program;
the processor is configured to execute the memory allocation method according to the first aspect.
A fourth aspect of the present application provides a computer-readable storage medium for storing a computer program for executing the memory allocation method of the first aspect.
A fifth aspect of the present application provides a computer program product comprising instructions which, when run on a computer, cause the computer to perform the memory allocation method of the first aspect described above.
According to the technical scheme, the embodiment of the application has the following advantages:
in the memory allocation method provided in the embodiment of the present application, when an application start request is received, a plurality of memory pools, a first mapping relationship and a second mapping relationship are first constructed, so that each memory pool includes at least one memory block, the first mapping relationship is used to record a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used to record a mapping relationship between a memory pool identifier and an address of a next memory block to be allocated in the memory pool. Then, after receiving a memory application request sent by an application program, obtaining the memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when the first memory address is determined to be an effective address, sending the first memory address to the application program, so that the subsequent application program can use the memory block corresponding to the first memory address.
Therefore, after a memory application request sent by the application program is received in the running process of the application program, the memory allocated to the application program can be determined only by inquiring the first mapping relation and the second mapping relation, so that the memory application flow in the running process of the application program is simplified, the memory allocation efficiency is improved, and the running smoothness of the application program is improved.
Drawings
Fig. 1 is a schematic view of an application scenario of a memory allocation method according to an embodiment of the present application;
fig. 2 is a schematic view of another application scenario of the memory allocation method according to the embodiment of the present application;
fig. 3 is a schematic flowchart of a memory allocation method according to an embodiment of the present application;
fig. 4 is a schematic diagram illustrating memory pool partitioning according to an embodiment of the present application;
fig. 5 is an address directing relationship between different memory blocks according to an embodiment of the present application;
fig. 6 is a schematic diagram illustrating an updating process of a second mapping relationship during a memory application according to an embodiment of the present application;
fig. 7 is an update diagram of address directing relationships between different memory blocks according to an embodiment of the present application;
fig. 8 is a flowchart illustrating a use of a memory pool during a memory application according to an embodiment of the present application;
fig. 9 is a flowchart of memory release provided in the present embodiment;
fig. 10 is a flowchart illustrating a use of a memory pool during a memory release period according to an embodiment of the present application;
fig. 11 is a flowchart of a memory allocation method applied to the application scenario shown in fig. 1 according to an embodiment of the present application;
FIG. 12 is a diagram illustrating a user triggering an application start request according to an embodiment of the present application;
fig. 13 is a schematic structural diagram of a memory allocation apparatus according to an embodiment of the present application;
fig. 14 is a schematic structural diagram of another memory allocation apparatus according to an embodiment of the present application;
fig. 15 is a schematic structural diagram of another memory allocation apparatus according to an embodiment of the present application;
fig. 16 is a schematic structural diagram of another memory allocation apparatus according to an embodiment of the present application;
fig. 17 is a schematic structural diagram of a terminal device according to an embodiment of the present application;
fig. 18 is a schematic structural diagram of a server according to an embodiment of the present application.
Detailed Description
In order to make the technical solutions of the present application better understood, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The terms "first," "second," "third," "fourth," and the like in the description and in the claims of the present application and in the drawings described above, if any, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the application described herein are capable of operation in sequences other than those illustrated or described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
The inventor finds that, in a research on a conventional memory allocation process (e.g., a system memory allocation scheme), because a memory application and release flow of the conventional memory allocation process is relatively complex, the efficiency of the memory application and release process of the conventional memory allocation process is relatively low, so that when the conventional memory allocation process processes application tasks (e.g., loading a game level and the like) with relatively frequent memory application release (especially small memory application release), it takes a relatively long time to respond to a memory allocation request of the application task, and thus the completion of the application task takes a relatively long time, and the running smoothness of the application is relatively low.
In view of the technical problems in the conventional memory allocation process, the embodiments of the present application provide a memory allocation method, which can effectively simplify a memory application flow in an application program running process, and is beneficial to improving memory allocation efficiency.
Specifically, in the memory allocation method provided in this embodiment of the present application, when an application start request is received, a plurality of memory pools, a first mapping relationship and a second mapping relationship are first constructed, so that each memory pool includes at least one memory block, the first mapping relationship is used to record a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used to record a mapping relationship between a memory pool identifier and an address of a next memory block to be allocated in a memory pool. Then, after receiving a memory application request sent by an application program, obtaining the memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when the first memory address is determined to be an effective address, sending the first memory address to the application program, so that the subsequent application program can use the memory block corresponding to the first memory address.
Therefore, each memory pool, the first mapping relation and the second mapping relation are constructed when the application program is started, so that after a memory application request sent by the application program is received in the running process of the application program, the memory allocated to the application program can be determined only by inquiring the first mapping relation and the second mapping relation, the memory application flow in the running process of the application program is simplified, and the memory allocation efficiency is improved.
It should be understood that the memory allocation method provided by the embodiment of the present application may be applied to data processing devices, such as terminal devices, servers, and the like; the terminal device may be a smart phone, a computer, a Personal Digital Assistant (PDA), a tablet computer, or the like; the server may specifically be an application server or a Web server, and in actual deployment, the server may be an independent server or a cluster server.
If the memory allocation method provided in the embodiment of the present application is executed by a terminal device, the terminal device may first construct a plurality of memory pools, a first mapping relationship, and a second mapping relationship when receiving an application program start request, and determine a first memory address from the plurality of memory pools through the first mapping relationship and the second mapping relationship directly after receiving a memory application request sent by the application program, so as to allocate a memory block corresponding to the first memory address to the application program. If the memory allocation method provided by the embodiment of the application is executed by a server, the server first constructs a plurality of memory pools, a first mapping relationship and a second mapping relationship according to an application program starting request uploaded by a terminal device, and after receiving a memory application request uploaded by the terminal device, determines a first memory address from the plurality of memory pools by directly using the first mapping relationship and the second mapping relationship, and then sends the first memory address to the terminal device, so that the terminal device allocates a memory block corresponding to the first memory address to the application program.
In order to facilitate understanding of the technical solutions provided in the embodiments of the present application, an application scenario in which the memory allocation method provided in the embodiments of the present application is applied to a terminal device is exemplarily described below.
Referring to fig. 1, the figure is a schematic view of an application scenario of the memory allocation method provided in the embodiment of the present application. As shown in fig. 1, the application scenario includes: terminal device 101 and user 102; the terminal device 101 is configured to perform the memory allocation method provided in the embodiment of the present application to perform memory allocation.
When the terminal device 101 receives an application program starting request triggered by the user 102, the terminal device 101 first constructs a plurality of memory pools, a first mapping relationship and a second mapping relationship, so that each memory pool includes at least one memory block, the first mapping relationship is used for recording a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used for recording a mapping relationship between the memory pool identifier and an address of a next memory block to be allocated in the memory pool. Then, after receiving a memory application request sent by an application program, the terminal device 101 first obtains a memory application size of the application program according to the memory application request, and determines a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when the first memory address is determined to be an effective address, allocating a memory block corresponding to the first memory address to the application program, so that a subsequent application program can use the memory block corresponding to the first memory address to execute a corresponding task (for example, loading a game prop and the like).
It should be understood that, in practical applications, the memory allocation method provided in the embodiment of the present application may also be applied to a server, see fig. 2, which is another application scenario diagram of the memory allocation method provided in the embodiment of the present application. As shown in fig. 2, the server 201 can first construct a plurality of memory pools, a first mapping relationship and a second mapping relationship according to an application program start request uploaded by the terminal device, so that each memory pool includes at least one memory block, the first mapping relationship is used to record a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used to record a mapping relationship between a memory pool identifier and an address of a next memory block to be allocated in the memory pool. Then, after receiving the memory application request uploaded by the terminal device, the server 201 first obtains the memory application size of the application program according to the memory application request, and determines a first identifier of the first memory pool according to the memory application size of the application program and the first mapping relationship; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when determining that the first memory address is an effective address, directly sending the first memory address to the terminal device, so that the terminal device allocates a memory block corresponding to the first memory address to the application program, so that a subsequent application program can execute a corresponding task (for example, loading a game prop and the like) by using the memory block corresponding to the first memory address.
It should be understood that the application scenarios shown in fig. 1 and fig. 2 are only examples, and in practical applications, the memory allocation method provided in the embodiment of the present application may also be applied to other application scenarios for performing memory allocation, and no limitation is made to the memory allocation method provided in the embodiment of the present application here.
The following describes a memory allocation method provided by the present application with embodiments.
Method embodiment one
Referring to fig. 3, fig. 3 is a schematic flowchart of a memory allocation method according to an embodiment of the present disclosure. For convenience of description, the following embodiments only take the terminal device as an execution subject to describe the memory allocation method. As shown in fig. 3, the memory allocation method includes S301 to S304:
s301: and when an application program starting request is received, constructing N memory pools, a first mapping relation and a second mapping relation.
The application program is computer software, and particularly can be computer software which needs to frequently apply for releasing a small memory in the running process. In addition, the application program is not limited in the embodiment of the application, for example, the application program may be game software, and the game software needs to apply for releasing a small memory frequently in an operation process (for example, loading a level with a complicated content).
The embodiment of the application program may not limit the obtaining manner of the application program starting request, for example, when the user clicks the application program icon on the terminal device, the application program starting request is triggered, and at this time, the terminal device may receive the application program starting request.
The N memory pools are used for providing memory blocks with various sizes for the application program; n is a positive integer.
In addition, each memory pool includes at least one memory block. For example, when N is 3, the 1 st memory pool may include 10000 memory blocks of 32 bytes, the 2 nd memory pool includes 5000 memory blocks of 64 bytes, and the 3 rd memory pool includes 1000 memory blocks of 256 bytes.
In addition, the sizes of the memory blocks in the same memory pool are the same. That is, a memory pool may include a plurality of memory blocks of the same size.
In addition, the memory blocks in the memory pool can be connected according to a single linked list. For example, as shown in FIG. 4, when the ith memory pool includes MiWhen the ith memory pool is connected according to the single linked list, the address pointer in the ith memory pool can be set as follows: storing the memory address of the j +1 th memory block of the ith memory pool to the jth memory block of the ith memory pool, and storing the preset invalid address to the Mth memory block of the ith memory pooliA memory block, so that the memory block can be stored in the jth memory block of the ith memory pool according to the groundAnd directly finding the memory address of the j +1 th memory block of the ith memory pool for storage.
In addition, in this embodiment, when the memory blocks in the memory pools are connected according to a single linked list and the sizes of the memory blocks located in the same memory pool are the same, the process of constructing the N memory pools specifically includes steps 11 to 13:
step 11: and acquiring the memory address of the original memory area by using a system memory application function.
The system memory application function refers to a function used when the system memory allocation scheme performs memory allocation.
The original memory area is a memory area with a larger memory space obtained by application for the application program when the application program is started; furthermore, the original memory area includes a large number of memory addresses with consecutive addresses.
Step 12: and dividing the memory address of the original memory area into memory pools to obtain the memory addresses of the N memory pools.
The memory pool division is not limited in the embodiment of the application, the memory pool division can be uniform, and the memory pool division can also be performed according to a preset memory pool size list. The preset memory pool size list can be preset according to an application scene.
Based on the above, in the embodiment of the present application, after the original memory area is obtained, the original memory area may be divided into N memory pools, so that each memory pool includes a plurality of consecutive memory addresses. For example, when the original memory region is 896000 bytes, the original memory region may be divided into 3 memory pools, and the 1 st memory pool is 320000 bytes, the 2 nd memory pool is 320000 bytes, and the 3 rd memory pool is 256000 bytes.
Step 13: dividing memory blocks of the memory address of the ith memory pool according to the ith size to obtain M of the ith memory pooliStoring the memory address of the j +1 th memory block of the ith memory pool to the jth memory block of the ith memory pool and storing the preset invalid address to the ith memory blockM th of the pooliEach memory block; j is a positive integer, j is less than or equal to Mi-1;MiIs a positive integer; i is a positive integer, and i is not more than N.
The ith size is set in advance according to an application scenario. For example, the 1 st size is 32, the 1 st size is 64, and the 3 rd size is 256.
Presetting an invalid address for marking the memory block as the last memory area in the memory pool to which the memory block belongs; moreover, the embodiment of the present application does not limit the representation manner of the preset invalid address, for example, the preset invalid address may be 0.
Based on the above, in the embodiment of the present application, after each memory pool is obtained, each memory pool may be uniformly divided into a plurality of memory blocks, so that each memory pool includes a plurality of memory blocks with the same size. For example, when the original memory area is divided into 3 memory pools, and the 1 st memory pool is 320000 bytes, the 2 nd memory pool is 320000 bytes, and the 3 rd memory pool is 256000 bytes, the 1 st memory pool may be uniformly divided into 10000 memory blocks of 32 bytes, the 2 nd memory pool may be uniformly divided into 5000 memory blocks of 64 bytes, and the 3 rd memory pool may be uniformly divided into 1000 memory blocks of 256 bytes.
In addition, as shown in fig. 4, the specific partitioning process of the ith memory pool is as follows: firstly, dividing memory blocks of memory addresses of an ith memory pool according to an ith size to obtain M of the ith memory pooliA memory block such that MiThe sizes of the memory blocks are all the ith size. Then, the memory address of the 2 nd memory block of the ith memory pool is stored to the 1 st memory block of the ith memory pool, the memory address of the 3 rd memory block of the ith memory pool is stored to the 2 nd memory block of the ith memory pool, … … (and so on), and the mth memory block of the ith memory pool is storedi-memory addresses of 1 memory block are stored to Mth memory pool of ith memory pooli-2 memory blocks and in Mth memory pool of ith memory pooliThe memory block stores a preset invalid address. Therefore, in the subsequent addressing process of the ith memory pool, the memory location of the 2 nd memory block of the ith memory pool can be found from the 1 st memory block of the ith memory poolAddress, finding the memory address of the 3 rd memory block of the ith memory pool from the 2 nd memory block of the ith memory pool, … … (and so on), until from the M < th > memory pooliWhen the preset invalid address is found in each memory block, it may be determined that each memory block in the ith memory pool has been traversed. Wherein j is a positive integer, and j is less than or equal to Mi-1;MiIs a positive integer; i is a positive integer, and i is not more than N.
Based on the related contents of the memory pools, in the embodiment of the present application, when an application program is started, a plurality of memory pools may be first constructed, so that each memory pool includes a plurality of memory blocks having the same size and connected to each other by addresses, and the memory blocks in different memory pools have different sizes. Therefore, in the subsequent memory application process, the memory blocks in different memory pools can be used for responding to the memory applications with different size requirements.
It should be noted that, in the embodiment of the present application, the size of the original memory area, the size of each memory pool, and the manner of obtaining the size of each memory block are not limited. For example, the size may be set in advance according to an application scenario, or may be set based on statistical information of simulation test data of an application (for example, statistical information such as an application amount, an application frequency, and an application peak of memories of different sizes).
In addition, the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identifier.
The memory pool identification is used for uniquely marking the memory pool. In addition, the embodiment of the present application does not limit the representation manner of the memory identifier, for example, the memory identifier may be an arrangement number ("1", "2", "3", … …) of the memory pool in the original memory area.
In addition, an embodiment of the present application further provides an implementation manner for constructing the first mapping relationship, which specifically includes steps 21 to 28:
step 21: and calculating the difference value between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference value set.
The size of the memory block is used to characterize the size of the memory block in the memory pool. For example, when a memory pool includes 10000 memory blocks of 32 bytes, the size of the memory block of the memory pool is 32.
In this embodiment of the present application, after the N memory pools are obtained, a difference between memory block sizes of any two memory pools may be calculated by using formula (1), so as to obtain a first difference set.
exy=|lx-ly| (1)
In the formula, exyRepresenting the difference value between the memory block size of the x memory pool and the memory block size of the y memory pool; lxThe memory block size of the xth memory pool is represented; lyThe memory block size of the y memory pool is represented; x is a positive integer, x is not more than N, and x is not equal to y, y is a positive integer, and y is not more than N; and N is the total number of the memory pools.
As an example, when the 1 st memory pool includes 10000 memory blocks of 32 bytes, the 2 nd memory pool includes 5000 memory blocks of 64 bytes, and the 3 rd memory pool includes 1000 memory blocks of 256 bytes, the memory block size of the 1 st memory pool is 32, the memory block size of the 2 nd memory pool is 64, and the memory block size of the 3 rd memory pool is 256; the difference between the block size of the 1 st pool and the block size of the 2 nd pool is 32, the difference between the block size of the 1 st pool and the block size of the 3 rd pool is 224, and the difference between the block size of the 2 nd pool and the block size of the 3 rd pool is 192. Thus, the first difference set is {32, 224, 192 }.
Step 22: the minimum value in the first set of difference values is determined as the first size value.
In the embodiment of the application, after the first difference set is obtained, the minimum value in the first difference set is determined as the first size value. For example, when the first set of difference values is 32,224, 192, then the first size value is 32.
Step 23: and determining the minimum value of the memory block sizes of the N memory pools as a second size value.
In this embodiment of the application, after the N memory pools are obtained, the minimum value in the memory block sizes of the N memory pools may be determined as the second size value. For example, based on the example content in step S21, if the block size of the 1 st memory pool is 32, the block size of the 2 nd memory pool is 64, and the block size of the 3 rd memory pool is 256, the second size value is 32.
Step 24: the minimum of the first size value and the second size value is determined as a third size value.
In this embodiment of the application, after the first size value and the second size value are obtained, a minimum value of the first size value and the second size value may be determined as a third size value, which specifically is: if the first size value is larger than the second size value, determining the second size value as a third size value; if the first size value is equal to the second size value, determining the second size value or the first size value as a third size value; and if the first size value is smaller than the second size value, determining the first size value as a third size value. For example, when the first size value is 32 and the second size value is 32, the third size value is determined to be 32.
Step 25: and determining the maximum value in the memory block sizes of the N memory pools as a fourth size value.
In this embodiment of the present application, after the N memory pools are obtained, a maximum value in the memory block sizes of the N memory pools may be determined as a fourth size value. For example, based on the example content in step S21, if the block size of the 1 st memory pool is 32, the block size of the 2 nd memory pool is 64, and the block size of the 3 rd memory pool is 256, the fourth size value is 256.
Step 26: the ratio of the fourth size value and the third size value is determined as the first number. The first number is used for representing the number of the relations in the first mapping relation.
In this embodiment of the application, after the fourth size value and the third size value are obtained, a ratio of the fourth size value to the third size value may be determined as a first number, so that the first number can represent a relationship number in the first mapping relationship. For example, when the third size value is 32 and the fourth size value is 256, the first number is 256 ÷ 32 ═ 8. At this time, the first mapping relationship includes 8 corresponding relationships.
Step 27: respectively subtracting the block size of each memory pool from a third size value which is r times to obtain a second difference value set, and taking the memory pool identifier of the memory pool corresponding to the minimum non-negative difference value in the second difference value set as the memory pool identifier corresponding to the r-th application size interval; the size interval of the r application is (r-1 times of the third size value and r times of the third size value), r is a positive integer, and r is less than or equal to the first number.
As an example, when the memory block size of the 1 st memory pool is 32, the memory block size of the 2 nd memory pool is 64, the memory block size of the 3 rd memory pool is 256, the third size value is 32, and the first number is 8; and when the memory pool identifier of the 1 st memory pool is "0", the memory pool identifier of the 2 nd memory pool is "1", and the memory pool identifier of the 3 rd memory pool is "2", step 27 may specifically include the following steps:
and respectively subtracting the memory block size (32) of the 1 st memory pool from the third size value (32) which is 1 time, subtracting the memory block size (64) of the 2 nd memory pool from the third size value (32) which is 1 time, and subtracting the memory block size (256) of the 3 rd memory pool from the third size value (32) which is 1 time to obtain a second difference set {0,32,224}, and taking the memory pool identifier (' 0 ') of the memory pool corresponding to the minimum non-negative difference value (0) in the second difference set as the memory pool identifier corresponding to the 1 st application size interval ((0, 32) }, namely, the application size interval (0,32 ') corresponds to the memory pool identifier ' 0 ' of the 1 st memory pool.
Respectively subtracting the block size (32) of the 1 st memory pool from the third size value (64) which is 2 times, subtracting the block size (64) of the 2 nd memory pool from the third size value (64) which is 2 times, and subtracting the block size (256) of the 3 rd memory pool from the third size value (64) which is 2 times to obtain a second difference set { -32,0,192}, and using the memory pool identifier ("1") of the memory pool corresponding to the minimum non-negative difference value (0) in the second difference set as the memory pool identifier corresponding to the 2 nd application size interval ((32, 64) ], that is, the application size interval (32,64] corresponds to the memory pool identifier "1" of the 2 nd memory pool.
And respectively subtracting the block size (32) of the 1 st memory pool from the third size value (96) which is 3 times, subtracting the block size (64) of the 2 nd memory pool from the third size value (96) which is 3 times, and subtracting the block size (256) of the 3 rd memory pool from the third size value (96) which is 3 times to obtain a second difference set { -64, -32,160}, and taking the memory pool identifier ("2") of the memory pool corresponding to the minimum non-negative difference value (160) in the second difference set as the memory pool identifier corresponding to the 3 rd application size interval ((64, 96) }, namely, the application size interval (64, 96) corresponds to the memory pool identifier "2" of the 3 rd memory pool.
… … (and so on), 8 correspondences can be finally obtained, and the 8 correspondences are specifically: the application size interval (0, 32) corresponds to a memory pool identifier '0' of a 1 st memory pool, the application size interval (32, 64) corresponds to a memory pool identifier '1' of a 2 nd memory pool, the application size interval (64, 96) corresponds to a memory pool identifier '2' of a 3 rd memory pool, the application size interval (96,128) corresponds to a memory pool identifier '2' of a 3 rd memory pool, the application size interval (128,160) corresponds to a memory pool identifier '2' of a 3 rd memory pool, the application size interval (160,192) corresponds to a memory pool identifier '2' of a 3 rd memory pool, the application size interval (192,224) corresponds to a memory pool identifier '2' of a 3 rd memory pool, and the application size interval (224,256) corresponds to a memory pool identifier '2' of a 3 rd memory pool.
Step 28: and establishing a first mapping relation according to each application size interval and the corresponding memory pool identifier.
The embodiment of the present application does not limit the expression of the first mapping relationship, and for example, the first mapping relationship may be expressed by a mapping table, a function, or an array. For ease of understanding, the following description is made in conjunction with an array.
In one possible embodiment, when the first mapping relationship is expressed by an array, the first mapping relationship is [ F ]1,F2,F3,……,FR]Wherein F isrThe position of the r element in the array; and FrIndicating the r application ruleCun interval (r-1 times the third dimension, r times the third dimension]And (3) corresponding memory pool identifiers, wherein R is a positive integer, R is less than or equal to R, and R represents the total number of the relationships (namely, the first number) in the first mapping relationship. In addition, the first mapping relationship may be used using equation (3).
Figure BDA0002429499480000131
In the formula, SarrayRepresenting the position of a memory pool identifier corresponding to the size of the memory to be queried in the array; lsRepresenting the size of a memory to be queried; l isaRepresents a third dimension value; [. the]Representing a rounding function.
To facilitate understanding of the above, the following description is made with reference to examples.
As an example, when the first mapping relationship is expressed in an array manner, based on the example content of step 27, the first mapping relationship is [0,1,2,2,2,2 ]. At this time, if the size of the memory to be queried is 50, the position of the memory pool identifier corresponding to the size of the memory to be queried in the array is [ (50-1)/32] +1 ═ 2, and it can be seen that the memory pool identifier corresponding to the size of the memory to be queried 50 is located at the position of the 2 nd element in the array [0,1,2,2,2,2,2,2], that is, the memory pool identifier corresponding to the size of the memory to be queried 50 is 1.
Based on the related content of the first mapping relationship, in the embodiment of the present application, after the N memory pools are obtained, the application size interval in which each memory pool is responsible for responding may be determined according to the memory block size of each memory pool, so that different memory pools can manage memory applications of different sizes. Therefore, the memory pools with different memory block sizes can respectively manage the memory applications with different sizes, so that the memory pools with different memory block sizes can simultaneously process the memory applications with different sizes, and thus the parallel execution of the memory applications with different sizes can be realized.
In addition, the second mapping relationship is used for recording the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool.
The memory address of the next memory block to be allocated in the memory pool refers to the memory address of the next free memory block in the memory pool. For example, as shown in fig. 5, when the 1 st memory block to the 3 rd memory block of the ith memory pool are all used, the 4 th memory block is the next memory block to be allocated in the ith memory pool, and the memory address of the 4 th memory block is the memory address of the next memory block to be allocated in the memory pool. In addition, because the memory address of the 4 th memory block is stored in the 3 rd memory block, when the 3 rd memory block is allocated to the application program for use, the memory address of the 4 th memory block may be directly read from the 3 rd memory block, and the memory address of the 4 th memory block is used as the memory address of the next to-be-allocated memory block in the memory pool corresponding to the memory pool identifier of the ith memory pool and is recorded in the second mapping relationship, so that the second mapping relationship may clearly record the correspondence relationship between the memory address of the 4 th memory block and the memory pool identifier of the ith memory pool.
In addition, when an application start request is received, initialization processing may be performed on the second mapping relationship, specifically: the second mapping relationship includes N corresponding relationships, and the ith relationship is that the memory pool identifier of the ith memory pool corresponds to the memory address of the memory block with the memory address ordering closest to the memory address in the ith memory pool. Wherein i is a positive integer, and i is not more than N.
In addition, the second mapping relationship is updated according to the memory application and release of the application program, and the updating process will be described in detail below.
Based on the above content, in the embodiment of the application, when a user wants to run an application on a terminal device, the user may trigger an application start request by clicking an application icon on the terminal device, so that the terminal device constructs N memory pools after receiving the application start request, so that different memory pools are responsible for managing memory applications of different sizes; meanwhile, a first mapping relationship and a second mapping relationship need to be established, so that a memory pool which should be used by each memory application can be determined by using the first mapping relationship in the following, and a memory address of a memory block to be allocated next in the memory pool is allocated to an application program according to the second mapping relationship, so that the application program can perform corresponding tasks by using the memory block corresponding to the memory address.
S302: and after receiving a memory application request sent by the application program, acquiring the memory application size of the application program according to the memory application request.
The memory application request refers to a request sent by an application program for applying a memory to be allocated; moreover, the memory application request carries the size of the memory to be allocated.
The memory application size of an application refers to the size of the memory to be allocated.
Based on the above, in the running process of the application program, after receiving the memory application request sent by the application program, the size of the memory to be allocated may be directly extracted from the memory application request, and the size is used as the memory application size of the application program, so that a memory block matching the memory application size of the application program can be determined from the memory pool subsequently based on the memory application size of the application program.
S303: and determining a first identifier of the first memory pool according to the memory application size of the application program and the first mapping relation.
The present embodiment is not limited to the embodiment of S303, and two possible embodiments will be described below.
In a first possible implementation manner, when the first mapping relationship is represented by using a mapping table, S303 may specifically be: firstly, determining a target application size interval to which the memory application size of the application program belongs according to the memory application size of the application program and a first mapping relation; and determining a first identifier of the first memory pool according to the target application size interval and the first mapping relation.
It can be seen that, in the embodiment of the present application, when the first mapping relationship is represented in the form of a mapping table, after the memory application size of the application program is obtained, the memory application size of the application program may be compared with each application size interval in the first mapping relationship, so as to determine a target application size interval to which the memory application size of the application program belongs; and searching a memory pool identifier corresponding to the target application size interval in the first mapping relation, and using the memory pool identifier as a first identifier of the first memory pool. For example, as can be seen from the above example in step 27, when the memory request size of the application program is 50, the memory request size 50 of the application program belongs to the request size interval (32, 64), so that the first identifier corresponding to the memory request size 50 of the application program should be the memory pool identifier "1" of the 2 nd memory pool.
Based on the related content of the first possible embodiment, when the first mapping relationship is expressed in the form of a mapping table, the first mapping relationship may be first used to determine a target application size interval to which the memory application size of the application program belongs, and then the target application size interval and the first mapping relationship are used to determine the first identifier of the first memory pool, so that the next memory block to be allocated in the first memory pool can be subsequently allocated to the application program for use.
In a second possible implementation manner, when the first mapping relationship is expressed in an array manner, S303 may specifically be: determining a first position by using a formula (3) according to the memory application size of the application program, wherein the first position represents the position of a memory pool identifier corresponding to the memory application size of the application program in a first mapping relation; and determining the memory pool identifier positioned at the first position in the first mapping relation as the first identifier of the first memory pool.
Based on the above related contents, in the embodiment of the present application, after the memory application size of the application program is obtained, the memory application size of the application program may be first used as the memory size S to be queriedarrayThen, the formula (3) is used to determine the memory pool identifier corresponding to the memory application size of the application program in the first mapping relation (i.e. the array [ F ]1,F2,F3,……,FR]) And determining the memory pool identifier at the position in the first mapping relationship as the first identifier of the first memory pool. For example, based on the example contents in step S28, when the application program is executedWhen the memory application size is 50, the memory pool identifier corresponding to the memory application size of 50 of the application program is located in the array [0,1,2,2,2,2]The 2 nd element position, so that the first identifier corresponding to the memory application size 50 of the application program should be the memory pool identifier "1" of the 2 nd memory pool.
Based on the relevant content of S303, in this embodiment of the application, after the memory application size of the application program is obtained, the first identifier of the first memory pool may be determined according to the memory application size of the application program and the first mapping relationship, so that the next memory block to be allocated in the first memory pool can be allocated to the application program for use based on the first identifier in the following process.
S304: and acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
The effective address corresponds to the above invalid address, and the effective address refers to a non-invalid address, that is, an address corresponding to the actual memory block.
In this embodiment of the application, after the first identifier is obtained, a memory address of a next memory block to be allocated in a memory pool corresponding to the first identifier may be first queried in a second mapping relationship, and the memory address is used as the first memory address; judging whether the first memory address is an effective address, if so, determining that the first memory pool has available idle memory blocks at the current moment, and at the moment, directly sending the first memory address to an application program so that the application program can use the memory blocks corresponding to the first memory address to execute corresponding tasks; if not, determining that each memory block in the first memory pool is in a use state at the current time, so that the first memory pool cannot allocate the memory for the application program, and at this time, in order to ensure the normal operation of the application program, calling a system memory application function so as to allocate the memory for the application program according to a default system memory allocation scheme.
Based on the relevant contents of the above steps S301 to S304, in the memory allocation method provided in this embodiment of the present application, when an application start request is received, a plurality of memory pools, a first mapping relationship and a second mapping relationship are first constructed, so that each memory pool includes at least one memory block, the first mapping relationship is used to record a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used to record a mapping relationship between a memory pool identifier and an address of a next memory block to be allocated in the memory pool. Then, after receiving a memory application request sent by an application program, obtaining the memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when the first memory address is determined to be an effective address, sending the first memory address to the application program, so that the subsequent application program can use the memory block corresponding to the first memory address.
Therefore, after a memory application request sent by the application program is received in the running process of the application program, the memory allocated to the application program can be determined only by inquiring the first mapping relation and the second mapping relation, so that the memory application flow in the running process of the application program is simplified, the memory allocation efficiency is improved, and the running smoothness of the application program is improved.
Method embodiment two
In addition, if the first memory address is sent to the application program, it indicates that the memory block corresponding to the first memory address is allocated to the application program for use, and thus indicates that the memory block corresponding to the first memory address is in a use state at the current time. At this time, in order to enable the first memory pool with the first memory address to continuously allocate the spatial memory blocks to the application program for use, after the first memory address is determined, the second mapping relationship needs to be updated, so that other free memory blocks in the first memory pool can be subsequently found by using the second mapping relationship to be allocated.
Based on this, an embodiment of the present application further provides an implementation manner of the memory allocation method, in which the memory allocation method may further include S305 to S307 (as shown in fig. 6), in addition to the above S301 to S304:
s305: and when the first memory address is determined to be the effective address, determining the first memory block according to the first memory address.
The first memory block refers to a memory block having a first memory address in the first memory pool.
S306: and reading the second memory address stored in the first memory block.
The second memory address refers to an address stored in the first memory block.
S307: and updating the second mapping relation by using the first identifier and the second memory address, so that the second mapping relation comprises the corresponding relation between the first identifier and the second memory address.
In this embodiment of the present application, after determining that the first memory address is an effective address, a memory block corresponding to the first memory address may be determined as a first memory block, and a second memory address is read from the first memory block; then, the second memory address is used as the next memory block to be allocated in the first memory pool, that is, the second memory address is set as the next memory block to be allocated in the first memory pool corresponding to the first identifier, so that the second mapping relationship is updated by using the first identifier and the next memory block to be allocated (that is, the second memory address) in the first memory pool corresponding to the first identifier, and the updating process is specifically; and deleting the corresponding relation between the first identifier and the first memory address, and increasing the corresponding relation between the first identifier and the second memory address.
For example, as shown in fig. 7, when the first memory pool is the ith memory pool shown in fig. 5, and the first memory address is an address of the 4 th memory block of the ith memory pool, S305 to S307 may specifically be: after determining that the address of the 4 th memory block of the ith memory pool is an effective address, determining the 4 th memory block of the ith memory pool according to the address of the 4 th memory block of the ith memory pool, and reading the address of the 5 th memory block of the ith memory pool from the 4 th memory block of the ith memory pool; and then, determining the address of the 5 th memory block of the ith memory pool as the next memory block to be allocated in the ith memory pool, and replacing the memory identifier (i.e., the first identifier) of the ith memory pool corresponding to the next memory block to be allocated in the ith memory pool with the address of the 5 th memory block of the ith memory pool in the second mapping relationship, so that the address of the 5 th memory block of the ith memory pool can be directly found when the next memory block to be allocated in the ith memory pool is subsequently found by means of the memory identifier of the ith memory pool and the second mapping relationship.
Based on the above-mentioned relevant contents of S305 to S307, in the embodiment of the present application, the second mapping relationship is updated along with the memory application process of the application program, which specifically includes: when it is determined that the first memory block corresponding to the first memory address is allocated to the application program, the next memory block to be allocated in the first memory pool corresponding to the first identifier may be replaced with the second memory address stored in the first memory block in the second mapping relationship, so that the second memory address in the first memory pool can be found directly by means of the first identifier and the second mapping relationship in the subsequent memory allocation process. Therefore, the second mapping relation can be updated along with the memory application process of the application program, so that the second mapping relation can accurately reflect the use states of different memory blocks in each memory pool, and the memory blocks which can be allocated to the application program in each memory pool at the current moment can be accurately determined based on the second mapping relation, thereby being beneficial to improving the allocation efficiency on the premise of improving the allocation accuracy.
In addition, in order to improve the memory allocation accuracy, each memory pool can only respond to one memory application request of the application program within a time period, that is, when one memory pool is responding to one memory application request, the memory pool cannot respond to other requests (for example, memory application requests or memory release requests), but other memory pools except the memory pool can respond to other requests.
Based on this, an embodiment of the present application further provides another implementation of the memory allocation method, in which the memory allocation method may further include, in addition to all or part of the above steps, S308 to S313 (as shown in fig. 8):
s308: and constructing a third mapping relation when an application program starting request is received.
And the third mapping relation is used for recording the mapping relation between the memory pool identification and the memory pool enabling state.
The memory enable state is used for representing whether the memory pool is in a use permission state. In addition, when the memory use state of one memory pool is the use-allowed state, it indicates that the memory pool can allocate memory blocks for the application program; however, when the memory usage status of a memory pool is a non-usage-permitted status, it indicates that the memory pool can not allocate memory blocks for the application temporarily.
In addition, in the initial third mapping relationship (i.e., the third mapping relationship constructed when the application start request is received), the memory pool enable state corresponding to each memory pool identifier is the allowed use state.
Based on the above, in the embodiment of the application, when the application start request is received, a third mapping relationship may be further constructed, so that the third mapping relationship may accurately record the enable state of each memory pool.
S309: and determining the enabling state of the first memory pool according to the first identifier and the third mapping relation.
In this embodiment of the application, after the first identifier is obtained, the memory pool enabling state corresponding to the first identifier may be found from the third mapping relationship, and is used as the enabling state of the first memory pool, so that whether the first memory pool can be used or not may be determined based on the enabling state of the first memory pool in the following.
It should be noted that S309 needs to be executed after S303 is executed and before S304 is executed.
S310: judging whether the enabling state of the first memory pool is a use-permitted state, if so, executing S311; if not, S313 is executed.
S311: and updating the enabling state of the first memory pool in the third mapping relation to a non-use-allowed state, and continuing to execute the steps S304-S307.
In this embodiment of the present application, when it is determined that the enable state of the first memory pool is the use-permitted state, it may be determined that the first memory pool may respond to a memory application request of an application program at the current time. At this time, in order to achieve the purpose that the first memory pool only responds to one request within a time period, the enable state of the first memory pool in the third mapping relationship may be updated to the non-use-permitted state, and then the above S304-S307 are executed. In the period from S304 to S307, the enabling state of the first memory pool recorded in the third mapping relationship is a non-use-permitted state, so that the first memory pool does not respond to any other request during the period from S304 to S307, and thus the first memory pool can accurately and smoothly execute S304 to S307, which can effectively avoid a confusion phenomenon caused by the first memory pool responding to two requests in the same time period, and thus the memory allocation accuracy of the memory pool is improved.
S312: and updating the enabling state of the first memory pool in the third mapping relation to be the use-allowed state.
In this embodiment, after the first memory pool completes executing the above S304-S307, it may be determined that the first memory pool has completed the memory application request sent by the response application program, so that it may be determined that the first memory pool is already in a stable state, at this time, the first memory pool may continue to respond to other requests, and therefore, after S304-S307 is completed, the enable state of the first memory pool in the third mapping relationship may be restored to the use-permitted state, so that the first memory pool may respond to other requests.
S313: and waiting for a preset time length, and continuing to execute S309.
The preset duration may be set in advance according to an application scenario. For example, the preset time period may be 3 seconds.
In this embodiment of the application, when it is determined that the enable state of the first memory pool is the non-use-permitted state, it may be determined that the first memory pool is processing other requests at the current time, and thus it may be determined that the first memory pool may not temporarily respond to the memory application request of the application program at the current time. At this time, the preset duration may be waited, and whether the enabling state of the first memory pool in the second mapping relationship changes or not may be queried again, and if not, the preset duration is waited continuously; … … (repeated multiple times), until the enabling state of the first memory pool in the second mapping relation is determined to be the usage-allowed state, then the above S311 and its subsequent steps may be executed.
Based on the above-mentioned related contents of S308 to S313, in the embodiment of the present application, after the first identifier of the first memory pool is determined, it may be determined, by using the third mapping relationship, whether the first memory pool is in the permitted use state, so that when it is determined that the first memory pool is in the non-permitted use state, waiting is performed until the first memory pool is determined that the first memory pool is in the permitted use state, and the memory blocks in the first memory pool are allocated to the application program according to the first identifier and the second mapping relationship. The first memory pool can accurately and smoothly respond to the request of the application program because the memory pool can only process one request in one time period, so that the chaotic phenomenon caused by the fact that the first memory pool simultaneously responds to two requests in one time period can be effectively avoided, and the memory allocation accuracy of the memory pool is improved.
In addition, when the application program uses up the memory blocks, the memory blocks can be released, so that when the memory pool responds to the memory application request again later, the released memory blocks are preferentially provided for the application program to be used again, and thus, the memory blocks with the next rank can be further used only on the basis that the memory blocks with the previous rank are used in the memory pool. Based on this, an embodiment of the present application further provides another implementation manner of the memory allocation method, in which the memory allocation method may further include, in addition to all or part of the above steps, S314 to S319 (as shown in fig. 9):
s314: and constructing a fourth mapping relation when an application program starting request is received.
The fourth mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the memory pool.
In this embodiment of the application, after the N memory pools are obtained, a fourth mapping relationship may be constructed by using the memory identifier and the memory address of each memory pool, so that the fourth mapping relationship may be used to record the memory address of each memory pool, so that the memory address of the memory pool may be found from the fourth mapping relationship directly by using the memory identifier of one memory pool in the following.
S315: after a memory release request sent by an application program is received, determining the memory address of a memory area to be released according to the memory release request.
The memory release request is used for representing a request for releasing a memory area to be released sent by an application program; moreover, the memory release request carries the memory address of the memory area to be released.
In the embodiment of the application, after a memory release request sent by an application program is received, a memory address of a memory area to be released can be directly extracted from the memory release request, so that memory release can be performed subsequently based on the memory address.
S316: judging whether the memory area to be released belongs to the original memory area or not according to the memory address of the original memory area and the memory address of the memory area to be released, if so, executing S317; if not, go to S319.
The original memory area includes N memory pools, and the content of the original memory area is referred to above.
In this embodiment of the application, after the memory address of the memory area to be released is obtained, whether the memory area to be released belongs to the original memory area or not may be determined according to the memory address of the original memory area and the memory address of the memory area to be released, if yes, it is determined that the memory area to be released belongs to a memory block in a memory pool, and at this time, the memory block release process may be completed by using the following steps S317 to S318; if not, determining that the memory area to be released does not belong to the memory block in the memory pool, and at this time, directly using a system memory release function to release the memory area to be released.
S317: and determining a second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relation.
The second memory area is used for representing a memory pool to which the memory area to be released belongs.
In this embodiment of the present application, when it is determined that the memory area to be released belongs to the original memory area, the memory identifier corresponding to the memory address of the memory area to be released may be queried in the fourth mapping relationship, and the second identifier of the second memory pool is determined, where the querying process specifically may be: determining the memory address of a target memory pool to which the memory address of the memory area to be released belongs according to the memory address of the memory area to be released and a fourth mapping relation; and determining a second identifier of the second memory pool according to the fourth mapping relation and the memory address of the target memory pool. That is, when it is determined that the memory area to be released belongs to the original memory area, the memory address of each memory pool recorded in the fourth mapping relationship is compared with the memory address of the memory area to be released, and the memory address of the target memory pool to which the memory address of the memory area to be released belongs is determined; and directly searching the memory identifier corresponding to the memory address of the target memory pool in the fourth mapping relation, and using the memory identifier as the second identifier of the second memory pool.
S318: and according to the second identifier and the second mapping relationship, acquiring a third memory address corresponding to the second identifier, storing the third memory address to the memory area to be released, and updating the second mapping relationship by using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship comprises the corresponding relationship between the second identifier and the memory address of the memory area to be released.
In this embodiment of the present application, in order to achieve the purpose that only the memory blocks with the earlier memory address arrangement order in the memory pool are used, the memory blocks with the later memory address arrangement order are further used, after the memory pool to which the memory area to be released belongs is determined, the memory address of the next memory block to be allocated in the memory pool to which the memory area to be released belongs at the current time is first queried from the second mapping relationship, and the memory address is used as the third memory address; and then, storing the third memory address to the memory area to be released, and replacing the memory address of the next memory block to be allocated in the memory pool corresponding to the second identifier in the second mapping relationship with the memory address of the memory area to be released, so that the use sequence of the memory blocks corresponding to the third memory address is located behind the memory area to be released. For ease of understanding, the following description is made in conjunction with the example shown in FIG. 7
As an example, when the first memory pool is the ith memory pool shown in fig. 5, the third memory address is an address of the 4 th memory block of the ith memory pool, and the to-be-released memory region is an address of the 1 st memory block of the ith memory pool, then S318 may specifically be: when it is determined that the to-be-released memory area belongs to the ith memory pool, the memory address of the next to-be-allocated memory block in the ith memory pool may be obtained from the second mapping relationship as the address of the 4 th memory block of the ith memory pool; storing the address of the 4 th memory block of the ith memory pool to the 1 st memory block of the ith memory pool, so that the address of the 4 th memory block of the ith memory pool can be read from the 1 st memory block of the ith memory pool when the 1 st memory block of the ith memory pool is allocated to an application program for use in the future; meanwhile, the memory address of the next memory block to be allocated in the ith memory pool in the second mapping relationship needs to be replaced by the 1 st memory block of the ith memory pool, so that the 1 st memory block of the ith memory pool can be directly allocated to the application program again for use when the ith memory pool responds to the memory application request of the application program next time.
It can be seen that, in the embodiment of the present application, each memory pool may preferentially use the released memory blocks to respond to the memory application request of the application program, so that the memory pool can preferentially use the memory blocks with the memory addresses sorted in the front to respond to the memory application request of the application program, so that the size of the memory pool can be adjusted in the future based on the use condition of the memory blocks in each memory pool, and the size of the memory pool can be reduced on the premise that the adjusted memory pool meets the use condition of the memory of the application program. For example, when a memory pool includes 1000 memory blocks, if it is found in an actual application process that 800 memory blocks with the top memory address rank in the memory pool are used, but 200 memory blocks with the top memory address rank are never used, it may be determined that the size of the memory pool is larger, and the size of the memory pool should be reduced.
S319: and releasing the memory area to be released by using a system memory release function.
The system memory release function refers to a function used when the memory is released by the above system memory allocation scheme.
Based on the relevant contents of S314 to S319, in this embodiment of the application, after receiving a memory release request sent by an application program, determining a memory address of a memory area to be released according to the memory release request, determining whether the memory area to be released belongs to an original memory area according to the memory address of the original memory area and the memory address of the memory area to be released, so that when it is determined that the memory area to be released belongs to the original memory area, determining a second identifier of a second memory pool according to the memory address of the memory area to be released and a fourth mapping relationship, and obtaining a third memory address corresponding to the second identifier according to the second identifier and the second mapping relationship; and storing the third memory address to the memory area to be released, and updating the second mapping relationship by using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship comprises the corresponding relationship between the second identifier and the memory address of the memory area to be released.
When the memory area to be released in the second memory pool is released, the memory address of the next memory block to be allocated in the memory pool corresponding to the second identifier in the second mapping relationship may be directly replaced with the memory address of the memory area to be released, so that the use sequence of the memory blocks corresponding to the third memory address is located behind the memory area to be released. It can be seen that, in the embodiment of the present application, each memory pool may preferentially use the released memory blocks to respond to the memory application request of the application program, so that the memory pool can preferentially use the memory blocks with the memory addresses sorted in the front to respond to the memory application request of the application program, so that the size of the memory pool can be adjusted in the future based on the use condition of the memory blocks in each memory pool, and the size of the memory pool can be reduced on the premise that the adjusted memory pool meets the use condition of the memory of the application program.
In addition, in order to improve the memory release accuracy, each memory pool can only respond to one memory release request of the application program within a time period, that is, when one memory pool is responding to one memory release request, the memory pool cannot respond to other requests (for example, a memory application request or a memory release request), but other memory pools except the memory pool can respond to other requests.
Based on this, an embodiment of the present application further provides another implementation manner of the memory allocation method, in which the memory allocation method may further include, in addition to all or part of the above steps, S320 to S324 (as shown in fig. 10):
s320: and determining the enabling state of the second memory pool according to the second identifier and the third mapping relation.
In this embodiment of the application, after the second identifier is obtained, the memory pool enabling state corresponding to the second identifier may be found from the third mapping relationship, and is used as the enabling state of the second memory pool, so that whether the second memory pool can be used or not may be determined based on the enabling state of the second memory pool in the following.
It should be noted that S320 needs to be executed after S317 is executed and before S318 is executed.
S321: judging whether the enabling state of the second memory pool is a use-allowed state, if so, executing S322; if not, go to S324.
S322: the enable state of the second memory pool in the third mapping relationship is updated to the non-use-permitted state, and the process continues to execute S318.
In this embodiment of the application, when it is determined that the enable state of the second memory pool is the use-permitted state, it may be determined that the second memory pool may respond to the memory release request of the application program at the current time. At this time, in order to achieve the purpose that the second memory pool only responds to one request within one time period, the enabling state of the second memory pool in the third mapping relationship may be updated to the non-use permitted state, and then the above S318 is executed. In the process of executing S318, the enabling state of the second memory pool recorded in the third mapping relationship is a non-use-permitted state, so that the second memory pool does not respond to any other request during the process of executing S318, and thus the second memory pool can accurately and smoothly execute S318, which can effectively avoid a confusion phenomenon caused by the second memory pool responding to two requests in the same time period, and thus the memory release accuracy of the memory pool is improved.
S323: and updating the enabling state of the second memory pool in the third mapping relation to be the use-allowed state.
In this embodiment of the application, after the second memory pool completes executing the above S318, it may be determined that the second memory pool has completed the memory release request sent by the response application, so that it may be determined that the second memory pool is already in a stable state, and at this time, the second memory pool may continue to respond to other requests, and therefore, after S318 is completed, the enable state of the second memory pool in the third mapping relationship may be restored to the use-permitted state, so that the second memory pool can respond to other requests.
S324: and waiting for a preset time length, and continuing to execute S320.
In this embodiment of the application, when it is determined that the enable state of the second memory pool is the non-use-permitted state, it may be determined that the second memory pool is processing other requests at the current time, and thus it may be determined that the second memory pool may not respond to the memory release request of the application program temporarily at the current time. At this time, the preset duration may be waited, and whether the enabling state of the second memory pool in the second mapping relationship changes is queried again, and if not, the preset duration is waited continuously; … … (repeated multiple times), until the enabling state of the second memory pool in the second mapping relationship is determined to be the usage-allowed state, then the above S322 and its subsequent steps can be executed.
Based on the related contents of S320 to S324, in the embodiment of the present application, after the second identifier of the second memory pool is determined, it may be determined whether the second memory pool is in the permitted usage state by using the third mapping relationship, so that when it is determined that the second memory pool is in the non-permitted usage state, waiting is performed until the to-be-released memory area is released according to the second identifier and the second mapping relationship when it is determined that the second memory pool is in the permitted usage state. The second memory pool can accurately and smoothly respond to the request of the application program because the memory pool can only process one request in one time period, so that the chaotic phenomenon caused by the fact that the second memory pool simultaneously responds to two requests in one time period can be effectively avoided, and the memory allocation accuracy of the memory pool is improved.
In order to further understand the memory allocation method provided in the embodiment of the present application, a scenario embodiment will be described in conjunction with fig. 11 to describe the memory allocation method provided in the embodiment of the present application as a whole.
Scene embodiment
For the sake of brevity, the contents of the parts in the scenario embodiment and the method embodiment are the same, and are not repeated herein, and for technical details of the parts with the same contents, reference is made to the relevant contents in the method embodiment.
Referring to fig. 11, a flowchart of a memory allocation method applied to the application scenario shown in fig. 1 according to an embodiment of the present application is shown.
The memory allocation method provided by the embodiment of the application comprises the following steps of S1101-S1120:
s1101: when receiving an application program starting request triggered by a user, the terminal equipment uses a system memory application function to acquire a memory address of an original memory area.
See step 11 above for the relevant contents of the original memory area.
The application may refer to game software, and in particular, may refer to massively multiplayer online role-playing game software created using a mercury engine. The game content of the large-scale multi-player online role playing game software is very rich, large scenes are supported to be connected seamlessly, various large and small objects are arranged in the scenes, characters are also very complicated, a large number of clothes are arranged, and hundreds of people can fight with each other on the same screen. It can be seen that such a large number of service requirements have high requirements on the memory application allocation performance at the time of game loading and running.
In this embodiment of the application, when a user clicks an application icon (for example, a game software icon as shown in fig. 12) on a terminal device, an application start request is triggered, and at this time, the terminal device may receive the application start request, and use a system memory application function to obtain a memory address of an original memory area, so that a memory pool can be subsequently constructed based on the original memory area.
S1102: and the terminal equipment constructs N memory pools by using the original memory area.
It should be noted that, in step S1102, the above steps 12 to 13 may be adopted to construct N memory pools, please refer to the above steps 12 to 13.
S1103: and the terminal equipment constructs a first mapping relation, a second mapping relation, a third mapping relation and a fourth mapping relation according to the N memory pools.
The first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; the fourth mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the memory pool; and the third mapping relation is used for recording the mapping relation between the memory pool identification and the memory pool enabling state.
In addition, the embodiment of the present application is not limited to the relationship among the first mapping relationship, the second mapping relationship, the fourth mapping relationship, and the third mapping relationship in actual application, for example, the second mapping relationship, the fourth mapping relationship, and the third mapping relationship may be represented by using a global array, and the first mapping relationship may be represented by using a static array, so that the global array is used to record the corresponding relationship between the memory pool identifier and the memory pool size, the memory address, the memory block size, the memory address of the next memory block to be allocated, and the enable state of the memory pool; and the static array is used for recording the corresponding relation between the application size interval and the memory pool identification.
S1104: after receiving a memory application request sent by an application program, the terminal device obtains a memory application size of the application program according to the memory application request, and determines a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation.
S1105: and the terminal equipment determines the enabling state of the first memory pool according to the first identifier and the third mapping relation.
S1106: the terminal device determines whether the enabled state of the first memory pool is a permitted use state, and if yes, executes S1108; if not, S1107 is executed.
S1107: and the terminal equipment waits for the preset time length and continues to execute S1105.
S1108: and the terminal equipment updates the enabling state of the first memory pool in the third mapping relation to a non-use-allowed state, and acquires a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation.
S1109: the terminal device determines whether the first memory address is an effective address, if so, executes S1110; if not, S1111 is executed.
S1110: the terminal equipment determines a first memory block according to the first memory address, reads a second memory address stored in the first memory block, and updates a second mapping relation by using the first identifier and the second memory address, so that the second mapping relation comprises a corresponding relation between the first identifier and the second memory address; and sending the first memory address to the application program.
S1111: and the terminal equipment allocates the memory which accords with the memory application size of the application program to the application program by using the system memory application function.
S1112: and the terminal equipment updates the enabling state of the first memory pool in the third mapping relation into a permitted using state.
S1113: after receiving a memory release request sent by an application program, the terminal equipment determines a memory address of a memory area to be released according to the memory release request.
S1114: the terminal device judges whether the memory area to be released belongs to the original memory area or not according to the memory address of the original memory area and the memory address of the memory area to be released, if so, S1116 is executed; if not, go to S1115.
S1115: and the terminal equipment releases the memory area to be released by using a system memory release function.
S1116: and the terminal equipment determines a second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relation.
S1117: and the terminal equipment determines the enabling state of the second memory pool according to the second identifier and the third mapping relation.
S1118: the terminal device judges whether the enabling state of the second memory pool is a use-permitted state, if yes, S1119 is executed; if not, go to S1120.
S1119: the terminal equipment updates the enabling state of the second memory pool in the third mapping relation to be a non-use-allowed state, acquires a third memory address corresponding to the second identifier according to the second identifier and the second mapping relation, stores the third memory address to the memory area to be released, updates the second mapping relation by using the second identifier and the memory address of the memory area to be released, enables the second mapping relation to comprise the corresponding relation between the second identifier and the memory address of the memory area to be released, and updates the enabling state of the second memory pool in the third mapping relation to be a use-allowed state.
S1120: the terminal device waits for a preset duration and continues to execute S1117.
Based on the above-mentioned relevant contents of S1101 to S1120, in the embodiment of the present application, since each memory pool, the first mapping relationship, and the second mapping relationship are constructed when the application program is started, after a memory application request sent by the application program is received in an application program running process, the memory allocated to the application program can be determined only by querying the first mapping relationship and the second mapping relationship, so that a memory application flow in the application program running process is simplified, which is beneficial to improving memory allocation efficiency, and is beneficial to improving the running smoothness of the application program.
Based on the related content of the memory allocation method provided above, the embodiment of the present application further provides a corresponding memory allocation apparatus, so that the memory allocation method provided above can be applied and implemented in practice.
Device embodiment
It should be noted that, for technical details of the memory allocation apparatus provided in this embodiment, reference may be made to the memory allocation method provided above.
Referring to fig. 13, a schematic structural diagram of a memory allocation apparatus according to an embodiment of the present disclosure is shown.
The memory allocation apparatus 1300 provided in the embodiment of the present application includes:
a constructing unit 1301, configured to construct the N memory pools, the first mapping relationship, and the second mapping relationship when receiving an application start request; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
a first processing unit 1302, configured to, after receiving a memory application request sent by the application program, obtain a memory application size of the application program according to the memory application request, and determine a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relationship;
and the allocating unit 1303 is configured to obtain a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship, and send the first memory address to the application program when the first memory address is determined to be an effective address.
Optionally, on the basis of the memory allocation apparatus 1300 shown in fig. 13, the building unit 1301 is specifically configured to: when the memory blocks in the memory pool are connected according to the single linked list and the sizes of the memory blocks in the same memory pool are the same,
using a system memory application function to obtain a memory address of an original memory area;
performing memory pool division on the memory address of the original memory area to obtain memory addresses of N memory pools;
dividing memory blocks of an ith memory pool according to the ith size to obtain Mi memory blocks of the ith memory pool, storing the memory address of a j +1 th memory block of the ith memory pool to a jth memory block of the ith memory pool, and storing a preset invalid address to the Mi memory block of the ith memory pool; j is a positive integer, j is not more than Mi-1; mi is a positive integer; i is a positive integer, and i is not more than N.
Optionally, on the basis of the memory allocation apparatus 1300 shown in fig. 13, the building unit 1301 is specifically configured to: when the sizes of the memory blocks in the same memory pool are the same,
calculating the difference value between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference value set; the size of the memory block is used for representing the size of the memory block in the memory pool;
determining a minimum value in the first difference value set as a first size value, determining a minimum value in the memory block sizes of the N memory pools as a second size value, and determining a minimum value in the first size value and the second size value as a third size value;
determining the maximum value in the memory block sizes of the N memory pools as a fourth size value, and determining the ratio of the fourth size value to the third size value as a first number; the first number is used for representing the relation number in the first mapping relation;
respectively subtracting the block size of each memory pool from a third size value which is r times to obtain a second difference value set, and taking the memory pool identifier of the memory pool corresponding to the minimum non-negative difference value in the second difference value set as the memory pool identifier corresponding to the r-th application size interval; the r application size interval is an interval (r-1 times of a third size value and r times of the third size value), r is a positive integer, and r is less than or equal to a first number;
and establishing a first mapping relation according to each application size interval and the corresponding memory pool identifier.
Optionally, on the basis of the memory allocation apparatus 1300 shown in fig. 13, the first processing unit 1302 is specifically configured to:
determining a target application size interval to which the memory application size of the application program belongs according to the memory application size of the application program and the first mapping relation;
and determining a first identifier of a first memory pool according to the target application size interval and the first mapping relation.
Optionally, on the basis of the memory allocation apparatus 1300 shown in fig. 13, as shown in fig. 14, the memory allocation apparatus 1300 further includes:
a first updating unit 1304, configured to determine a first memory block according to the first memory address when the first memory address is determined to be an effective address; reading a second memory address stored in the first memory block; and updating the second mapping relation by using the first identifier and the second memory address, so that the second mapping relation comprises a corresponding relation between the first identifier and the second memory address.
Optionally, on the basis of the memory allocation apparatus 1300 shown in fig. 13 or fig. 14, as shown in fig. 15, the memory allocation apparatus 1300 further includes:
the constructing unit 1301 is further configured to construct a third mapping relationship when receiving an application start request;
a determining unit 1305, configured to determine, according to the first identifier and the third mapping relationship, an enabling state of the first memory pool;
the allocating unit 1303 is specifically configured to, when it is determined that the enabling state of the first memory pool is the use-permitted state, obtain a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship.
Optionally, on the basis of the memory allocation apparatus 1300 shown in any one of fig. 13 to 15, as shown in fig. 16, the memory allocation apparatus 1300 further includes:
the constructing unit 1301 is further configured to construct a fourth mapping relationship when receiving an application start request; the fourth mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the memory pool;
a second processing unit 1306, configured to determine, after receiving a memory release request sent by the application program, a memory address of a memory area to be released according to the memory release request, and determine whether the memory area to be released belongs to an original memory area according to the memory address of the original memory area and the memory address of the memory area to be released; the original memory area comprises N memory pools;
a releasing unit 1307, configured to, when it is determined that the memory area to be released belongs to the original memory area, determine a second identifier of a second memory pool according to the memory address of the memory area to be released and the fourth mapping relationship, and obtain a third memory address corresponding to the second identifier according to the second identifier and the second mapping relationship; and storing the third memory address to the memory area to be released, and updating the second mapping relationship by using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship comprises the corresponding relationship between the second identifier and the memory address of the memory area to be released.
Based on the related content of the memory allocation apparatus 1300, in the memory allocation method provided in this embodiment of the present application, when an application start request is received, a plurality of memory pools, a first mapping relationship and a second mapping relationship are first constructed, so that each memory pool includes at least one memory block, the first mapping relationship is used to record a mapping relationship between an application size interval and a memory pool identifier, and the second mapping relationship is used to record a mapping relationship between a memory pool identifier and an address of a next memory block to be allocated in a memory pool. Then, after receiving a memory application request sent by an application program, obtaining the memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and a first mapping relation; and then, according to the first identifier and the second mapping relationship, obtaining a first memory address corresponding to the first identifier, and when the first memory address is determined to be an effective address, sending the first memory address to the application program, so that the subsequent application program can use the memory block corresponding to the first memory address.
Therefore, after a memory application request sent by the application program is received in the running process of the application program, the memory allocated to the application program can be determined only by inquiring the first mapping relation and the second mapping relation, so that the memory application flow in the running process of the application program is simplified, the memory allocation efficiency is improved, and the running smoothness of the application program is improved.
The embodiment of the present application further provides a terminal device and a server for allocating a memory, and the terminal device and the server for allocating a memory provided in the embodiment of the present application will be described in terms of hardware materialization.
Fig. 17 is a schematic structural diagram of a terminal device according to an embodiment of the present application. For convenience of explanation, only the parts related to the embodiments of the present application are shown, and details of the specific technology are not disclosed. The terminal may be any terminal device including a mobile phone, a tablet computer, a Personal Digital Assistant (PDA, for short, in english), a Sales terminal (POS, for short, in english), a vehicle-mounted computer, and the like, where the terminal is the tablet computer:
fig. 17 is a block diagram illustrating a partial structure of a tablet computer related to a terminal provided in an embodiment of the present application. Referring to fig. 17, the tablet computer includes: radio Frequency (RF) circuit 1710, memory 1720, input unit 1730, display unit 1740, sensor 1750, audio circuit 1760, wireless fidelity (WiFi) module 1770, processor 1780, and power supply 1790. Those skilled in the art will appreciate that the tablet configuration shown in fig. 17 is not intended to be a limitation of a tablet and may include more or fewer components than those shown, or some components may be combined, or a different arrangement of components.
The memory 1720 can be used for storing software programs and modules, and the processor 1780 executes various functional applications and data processing of the tablet computer by operating the software programs and modules stored in the memory 1720. The memory 1720 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required for at least one function (such as a sound playing function, an image playing function, and the like), and the like; the stored data area may store data (such as audio data, a phonebook, etc.) created according to the use of the tablet computer, and the like. Further, the memory 1720 may include high-speed random access memory and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
The processor 1780 is a control center of the tablet computer, connects various parts of the whole tablet computer by using various interfaces and lines, performs various functions of the tablet computer and processes data by operating or executing software programs and/or modules stored in the memory 1720 and calling data stored in the memory 1720, thereby monitoring the whole tablet computer. Optionally, processor 1780 may include one or more processing units; preferably, the processor 1780 may integrate an application processor, which primarily handles operating systems, user interfaces, application programs, etc., and a modem processor, which primarily handles wireless communications. It will be appreciated that the modem processor described above may not be integrated into processor 1780.
In the embodiment of the present application, the processor 1780 included in the terminal further has the following functions:
when an application program starting request is received, constructing N memory pools, a first mapping relation and a second mapping relation; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
after receiving a memory application request sent by the application program, obtaining a memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
Optionally, the processor 1780 is further configured to execute the steps of any implementation manner of the memory allocation method provided in the embodiment of the present application.
A server is also provided in the embodiments of the present application, and fig. 18 is a schematic structural diagram of a server provided in the embodiments of the present application, where the server 1800 may have a relatively large difference due to different configurations or performances, and may include one or more Central Processing Units (CPUs) 1822 (e.g., one or more processors) and a memory 1832, and one or more storage media 1830 (e.g., one or more mass storage devices) storing an application program 1842 or data 1844. The memory 1832 and the storage medium 1830 may be, among other things, transient storage or persistent storage. The program stored on the storage medium 1830 may include one or more modules (not shown), each of which may include a series of instruction operations on a server. Still further, a central processor 1822 may be provided in communication with the storage medium 1830 to execute a series of instruction operations in the storage medium 1830 on the server 1800.
The server 1800 may also include one or more power supplies 1826, one or more wired or wireless network interfaces 1850, one or more input-output interfaces 1858, and/or one or more operating systems 1841, such as Windows Server, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, and so forth.
The steps performed by the server in the above embodiment may be based on the server structure shown in fig. 18.
The CPU 1822 is configured to perform the following steps:
when an application program starting request is received, constructing N memory pools, a first mapping relation and a second mapping relation; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
after receiving a memory application request sent by the application program, obtaining a memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
Optionally, the CPU 1822 may also be configured to execute the steps of any implementation manner of the memory allocation method in the embodiment of the present application.
An embodiment of the present application further provides a computer-readable storage medium, configured to store a computer program, where the computer program is configured to execute any implementation manner of the memory allocation method described in the foregoing embodiments.
The present application further provides a computer program product including instructions, which when run on a computer, causes the computer to execute any one of the embodiments of the memory allocation method described in the foregoing embodiments.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the several embodiments provided in the present application, it should be understood that the disclosed system, apparatus and method may be implemented in other manners. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present application may be substantially implemented or contributed to by the prior art, or all or part of the technical solution may be embodied in a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present application. And the aforementioned storage medium includes: a U disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing computer programs.
The above embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions in the embodiments of the present application.

Claims (10)

1. A method for allocating memory, comprising:
when an application program starting request is received, constructing N memory pools, a first mapping relation and a second mapping relation; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
after receiving a memory application request sent by the application program, obtaining a memory application size of the application program according to the memory application request, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
2. The method according to claim 1, wherein when the memory blocks in the memory pools are connected according to a single linked list and the sizes of the memory blocks located in the same memory pool are the same, the constructing N memory pools specifically includes:
using a system memory application function to obtain a memory address of an original memory area;
performing memory pool division on the memory address of the original memory area to obtain memory addresses of N memory pools;
dividing memory blocks of the memory address of the ith memory pool according to the ith size to obtain M of the ith memory pooliStoring the memory address of the j +1 th memory block of the ith memory pool to the jth memory block of the ith memory pool, and storing the preset invalid address to the Mth memory block of the ith memory pooliEach memory block; j is a positive integer, j is less than or equal to Mi-1;MiIs a positive integer; i is a positive integer, and i is not more than N.
3. The method according to claim 1, wherein when the sizes of the memory blocks in the same memory pool are the same, the constructing the first mapping relationship includes:
calculating the difference value between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference value set; the size of the memory block is used for representing the size of the memory block in the memory pool;
determining a minimum value in the first difference value set as a first size value, determining a minimum value in the memory block sizes of the N memory pools as a second size value, and determining a minimum value in the first size value and the second size value as a third size value;
determining the maximum value in the memory block sizes of the N memory pools as a fourth size value, and determining the ratio of the fourth size value to the third size value as a first number; the first number is used for representing the relation number in the first mapping relation;
respectively subtracting the block size of each memory pool from a third size value which is r times to obtain a second difference value set, and taking the memory pool identifier of the memory pool corresponding to the minimum non-negative difference value in the second difference value set as the memory pool identifier corresponding to the r-th application size interval; the r application size interval is an interval (r-1 times of a third size value and r times of the third size value), r is a positive integer, and r is less than or equal to a first number;
and establishing a first mapping relation according to each application size interval and the corresponding memory pool identifier.
4. The method of claim 1, wherein determining the first identifier of the first memory pool according to the memory application size of the application program and the first mapping relationship comprises:
determining a target application size interval to which the memory application size of the application program belongs according to the memory application size of the application program and the first mapping relation;
and determining a first identifier of a first memory pool according to the target application size interval and the first mapping relation.
5. The method of claim 1, further comprising:
when the first memory address is determined to be an effective address, determining a first memory block according to the first memory address;
reading a second memory address stored in the first memory block;
and updating the second mapping relation by using the first identifier and the second memory address, so that the second mapping relation comprises a corresponding relation between the first identifier and the second memory address.
6. The method of claim 1, further comprising:
constructing a third mapping relation when an application program starting request is received;
determining an enabling state of the first memory pool according to the first identifier and the third mapping relation;
the obtaining a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship includes:
and when the enabling state of the first memory pool is determined to be the use-allowed state, acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation.
7. The method of claim 1, further comprising:
when an application program starting request is received, a fourth mapping relation is constructed; the fourth mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the memory pool;
after receiving a memory release request sent by the application program, determining a memory address of a memory area to be released according to the memory release request, and judging whether the memory area to be released belongs to an original memory area or not according to the memory address of the original memory area and the memory address of the memory area to be released; wherein the original memory area comprises the N memory pools;
when the memory area to be released is determined to belong to the original memory area, determining a second identifier of a second memory pool according to the memory address of the memory area to be released and the fourth mapping relation, and acquiring a third memory address corresponding to the second identifier according to the second identifier and the second mapping relation;
and storing the third memory address to the memory area to be released, and updating the second mapping relationship by using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship comprises the corresponding relationship between the second identifier and the memory address of the memory area to be released.
8. A memory allocation apparatus, comprising:
the device comprises a construction unit, a first mapping unit and a second mapping unit, wherein the construction unit is used for constructing N memory pools, a first mapping relation and a second mapping relation when an application program starting request is received; the memory pool comprises at least one memory block; the first mapping relation is used for recording the mapping relation between the application size interval and the memory pool identification; the second mapping relation is used for recording the mapping relation between the memory pool identification and the memory address of the next memory block to be allocated in the memory pool; n is a positive integer;
the first processing unit is used for acquiring the memory application size of the application program according to the memory application request after receiving the memory application request sent by the application program, and determining a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relation;
and the allocation unit is used for acquiring a first memory address corresponding to the first identifier according to the first identifier and the second mapping relation, and sending the first memory address to the application program when the first memory address is determined to be an effective address.
9. An apparatus, comprising a processor and a memory:
the memory is used for storing a computer program;
the processor is configured to perform the method of any one of claims 1-7 in accordance with the computer program.
10. A computer-readable storage medium, characterized in that the computer-readable storage medium is used to store a computer program for performing the method of any of claims 1-7.
CN202010231780.6A 2020-03-27 2020-03-27 Memory allocation method, device, equipment and storage medium Pending CN113448720A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010231780.6A CN113448720A (en) 2020-03-27 2020-03-27 Memory allocation method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010231780.6A CN113448720A (en) 2020-03-27 2020-03-27 Memory allocation method, device, equipment and storage medium

Publications (1)

Publication Number Publication Date
CN113448720A true CN113448720A (en) 2021-09-28

Family

ID=77808107

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010231780.6A Pending CN113448720A (en) 2020-03-27 2020-03-27 Memory allocation method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN113448720A (en)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114328298A (en) * 2022-03-14 2022-04-12 南京芯驰半导体科技有限公司 System and method for mapping addresses of on-chip memories for vector access
CN114579198A (en) * 2022-01-20 2022-06-03 飞腾信息技术有限公司 Memory chip starting method and device, computer equipment and storage medium
CN117573377A (en) * 2024-01-15 2024-02-20 摩尔线程智能科技(北京)有限责任公司 Memory management method, device, equipment and storage medium
CN117573378A (en) * 2024-01-15 2024-02-20 摩尔线程智能科技(北京)有限责任公司 Memory management method, device, equipment and storage medium

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114579198A (en) * 2022-01-20 2022-06-03 飞腾信息技术有限公司 Memory chip starting method and device, computer equipment and storage medium
CN114579198B (en) * 2022-01-20 2024-02-20 飞腾信息技术有限公司 Memory chip starting method and device, computer equipment and storage medium
CN114328298A (en) * 2022-03-14 2022-04-12 南京芯驰半导体科技有限公司 System and method for mapping addresses of on-chip memories for vector access
CN117573377A (en) * 2024-01-15 2024-02-20 摩尔线程智能科技(北京)有限责任公司 Memory management method, device, equipment and storage medium
CN117573378A (en) * 2024-01-15 2024-02-20 摩尔线程智能科技(北京)有限责任公司 Memory management method, device, equipment and storage medium

Similar Documents

Publication Publication Date Title
CN113448720A (en) Memory allocation method, device, equipment and storage medium
CN110222288B (en) Page display method, device and storage medium
CN110955584B (en) Block device access tracking method and device, storage medium and terminal
CN110442308B (en) Data block storage method, device, equipment and storage medium
CN111897818A (en) Data storage method and device, electronic equipment and storage medium
CN111104178A (en) Dynamic library loading method, terminal device and storage medium
CN114546897A (en) Memory access method and device, electronic equipment and storage medium
CN110928397A (en) User interface refreshing method and device, storage medium and electronic device
CN115421787A (en) Instruction execution method, apparatus, device, system, program product, and medium
CN109683798B (en) Text determination method, terminal and computer readable storage medium
CN112587922B (en) Model display method and device
CN114564418A (en) Data reading and writing method, system, terminal equipment and storage medium
CN113905252B (en) Data storage method and device for live broadcasting room, electronic equipment and storage medium
CN115016944A (en) Process access method and device and electronic equipment
CN114756788A (en) Cache updating method, device, equipment and storage medium
CN110045929B (en) Mounting point management method and related device
CN113590509A (en) Page exchange method, storage system and electronic equipment
CN113012022B (en) Method and device for processing graphics of applet, electronic equipment and medium
CN111813307A (en) Application program display method and device and electronic equipment
CN117112044B (en) Instruction processing method, device, equipment and medium based on network card
CN113101647B (en) Information display method, device, equipment and storage medium
CN117909258B (en) Optimization method and device for processor cache, electronic equipment and storage medium
CN111831397B (en) Method, device, equipment and storage medium for processing IO request
CN116069754A (en) File storage method and device, electronic equipment and storage medium
CN117851517A (en) Identity identification method, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination