A memory allocation method, device, equipment and storage medium
By building a memory pool and mapping relationship when the application starts, it simplifies the memory application process, solves the problem of low efficiency of traditional memory allocation methods, and improves the running fluency of the application.
Patent Information
- Application Number
- CN202010231780.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-03-27
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2040-03-27
AI Technical Summary
Traditional memory allocation methods are inefficient when handling frequent memory application and release tasks, resulting in poor application smoothness.
Construct multiple memory pools, first mapping relationships, and second mapping relationships when the application starts, record the application size interval and memory pool identifier, and the address mapping relationship of the memory block to be allocated in the memory pool, simplifying the memory application process.
It improves memory allocation efficiency, improves application running fluency, and reduces the complexity and time-consuming memory application process.
Smart Images

Figure CN113448720B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a memory allocation method, device, equipment and storage medium. Background Art
[0002] Memory allocation refers to the allocation or recovery of storage space during the execution of an application. For example, during the operation of an application (e.g., a game, a music player, etc.), memory needs to be allocated to the application to ensure that the target application can run smoothly.
[0003] At present, the default system memory allocation scheme is usually used for memory allocation, and the system memory allocation scheme will respond to memory application requests of various sizes. However, due to the low efficiency of the memory application and release process of the system memory allocation scheme, when the system memory allocation scheme processes application tasks with frequent memory application and release (for example, loading game levels, etc.), it takes a long time to respond to the memory allocation request of the application task, which results in a long time to complete the application task, resulting in a low running smoothness of the application. Summary of the invention
[0004] The embodiments of the present application provide a memory allocation method, apparatus, device and storage medium, which can improve the efficiency of memory allocation and thus improve the running smoothness of an application.
[0005] In view of this, the first aspect of the present application provides a memory allocation method, comprising:
[0006] Upon receiving an application startup request, N memory pools, a first mapping relationship and a second mapping relationship are constructed; the memory pool includes at least one memory block; the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer;
[0007] After receiving the memory application request sent by the application, obtaining the memory application size of the application according to the memory application request, and determining the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship;
[0008] According to the first identifier and the second mapping relationship, a first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application.
[0009] A second aspect of the present application provides a memory allocation device, comprising:
[0010] A construction unit is used to construct N memory pools, a first mapping relationship and a second mapping relationship when receiving an application startup request; the memory pool includes at least one memory block; the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer;
[0011] a first processing unit, configured to, after receiving a memory application request sent by the application, obtain a memory application size of the application 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 and the first mapping relationship;
[0012] An allocation unit is used to obtain a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship, and when it is determined that the first memory address is a valid address, send the first memory address to the application.
[0013] A third aspect of the present application provides a device, the device comprising a processor and a memory:
[0014] The memory is used to store computer programs;
[0015] The processor is used to execute the memory allocation method described in the first aspect according to the computer program.
[0016] A fourth aspect of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store a computer program, and the computer program is used to execute the memory allocation method described in the first aspect above.
[0017] A fifth aspect of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the memory allocation method described in the first aspect.
[0018] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:
[0019] In the memory allocation method provided by the embodiment of the present application, when receiving an application startup request, multiple 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 the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. Then, after receiving the memory application request sent by the application, the memory application size of the application is first obtained according to the memory application request, and the first identifier of the first memory pool is determined according to the memory application size of the application and the first mapping relationship; then, according to the first identifier and the second mapping relationship, the first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application so that the subsequent application can use the memory block corresponding to the first memory address.
[0020] It can be seen that since each memory pool, the first mapping relationship and the second mapping relationship are all constructed when the application is started, after receiving the memory application request sent by the application during the running of the application, it is only necessary to query the first mapping relationship and the second mapping relationship to determine the memory allocated to the application. This simplifies the memory application process during the running of the application, which is beneficial to improving the memory allocation efficiency, and thus is beneficial to improving the running smoothness of the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A schematic diagram of an application scenario of the memory allocation method provided in an embodiment of the present application;
[0022] Figure 2 A schematic diagram of another application scenario of the memory allocation method provided in an embodiment of the present application;
[0023] Figure 3 A flowchart of a memory allocation method provided in an embodiment of the present application;
[0024] Figure 4 A schematic diagram of memory pool division provided in an embodiment of the present application;
[0025] Figure 5 The address guidance relationship between different memory blocks provided in the embodiment of the present application;
[0026] Figure 6 A schematic diagram of the updating process of the second mapping relationship during the memory application provided in an embodiment of the present application;
[0027] Figure 7 A schematic diagram of updating the address guidance relationship between different memory blocks provided in an embodiment of the present application;
[0028] Figure 8 A flowchart of using a memory pool during memory application provided by an embodiment of the present application;
[0029] Fig. 9 A flowchart of memory release provided in an embodiment of the present application;
[0030] Fig.10 A flowchart of using a memory pool during memory release provided by an embodiment of the present application;
[0031] Fig.11 The application provided in the embodiment of the present application is Figure 1 A flowchart of a memory allocation method in the application scenario shown;
[0032] Fig.12 A schematic diagram of a user-triggered application startup request provided in an embodiment of the present application;
[0033] Fig.13 A schematic diagram of the structure of a memory allocation device provided in an embodiment of the present application;
[0034] Fig.14 A schematic diagram of the structure of another memory allocation device provided in an embodiment of the present application;
[0035] Fig.15 A schematic diagram of the structure of another memory allocation device provided in an embodiment of the present application;
[0036] Fig.16 A schematic diagram of the structure of another memory allocation device provided in an embodiment of the present application;
[0037] Fig.17 A schematic diagram of the structure of a terminal device provided in an embodiment of the present application;
[0038] Fig.18 A schematic diagram of the structure of a server provided in an embodiment of the present application. DETAILED DESCRIPTION
[0039] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0040] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0041] The inventors discovered in their research on traditional memory allocation processes (e.g., system memory allocation schemes) that the memory application and release processes of traditional memory allocation processes are relatively complex, resulting in low efficiency of the memory application and release processes of traditional memory allocation processes. As a result, when the traditional memory allocation process processes application tasks (e.g., loading game levels, etc.) with frequent memory application and release (especially small memory application and release), it takes a long time to respond to the memory allocation request of the application task, which results in a longer time to complete the application task, resulting in lower running fluency of the application.
[0042] In response to the technical problems existing in the above-mentioned traditional memory allocation process, an embodiment of the present application provides a memory allocation method, which can effectively simplify the memory application process during the operation of the application, and is conducive to improving the memory allocation efficiency.
[0043] Specifically, in the memory allocation method provided in the embodiment of the present application, when receiving an application startup request, multiple 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 the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. Then, after receiving the memory application request sent by the application, the memory application size of the application is first obtained according to the memory application request, and the first identifier of the first memory pool is determined according to the memory application size of the application and the first mapping relationship; then, according to the first identifier and the second mapping relationship, the first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application so that the subsequent application can use the memory block corresponding to the first memory address.
[0044] It can be seen that since each memory pool, the first mapping relationship and the second mapping relationship are all constructed when the application is started, after receiving the memory application request sent by the application during the running of the application, it is only necessary to query the first mapping relationship and the second mapping relationship to determine the memory allocated to the application. This simplifies the memory application process during the running of the application and is conducive to improving memory allocation efficiency.
[0045] It should be understood that the memory allocation method provided in the embodiments of the present application can be applied to data processing devices, such as terminal devices, servers, etc.; wherein the terminal device can specifically be a smart phone, a computer, a personal digital assistant (PDA), a tablet computer, etc.; the server can specifically be an application server or a Web server. In actual deployment, the server can be an independent server or a cluster server.
[0046] If the memory allocation method provided in the embodiment of the present application is executed by a terminal device, the terminal device can first construct multiple memory pools, a first mapping relationship and a second mapping relationship when receiving an application startup request, and after receiving the memory application request sent by the application, directly use the first mapping relationship and the second mapping relationship to determine the first memory address from the multiple memory pools, so as to allocate the memory block corresponding to the first memory address to the application. If the memory allocation method provided in the embodiment of the present application is executed by a server, the server first constructs multiple memory pools, a first mapping relationship and a second mapping relationship according to the application startup request uploaded by the terminal device, and after receiving the memory application request uploaded by the terminal device, first directly use the first mapping relationship and the second mapping relationship to determine the first memory address from the multiple memory pools, and then send the first memory address to the terminal device, so that the terminal device allocates the memory block corresponding to the first memory address to the application.
[0047] To facilitate understanding of the technical solution provided in the embodiments of the present application, the following takes the application of the memory allocation method provided in the embodiments of the present application to a terminal device as an example to exemplarily introduce the application scenarios to which the memory allocation method provided in the embodiments of the present application is applicable.
[0048] See also Figure 1 , which is a schematic diagram of an application scenario of the memory allocation method provided in the embodiment of the present application. Figure 1 As shown, the application scenario includes: a terminal device 101 and a user 102; wherein the terminal device 101 is used to execute the memory allocation method provided in an embodiment of the present application to perform memory allocation.
[0049] When the terminal device 101 receives the application startup request triggered by the user 102, the terminal device 101 first constructs multiple 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 to record the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. Then, after receiving the memory application request sent by the application, the terminal device 101 first obtains the memory application size of the application according to the memory application request, and determines the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship; then, according to the first identifier and the second mapping relationship, obtains the first memory address corresponding to the first identifier, and when it is determined that the first memory address is a valid address, allocates the memory block corresponding to the first memory address to the application, so that the subsequent application can use the memory block corresponding to the first memory address to perform corresponding tasks (for example, loading game props, etc.).
[0050] It should be understood that in practical applications, the memory allocation method provided in the embodiment of the present application can also be applied to the server, see Figure 2 , which is a schematic diagram of another application scenario of the memory allocation method provided in the embodiment of the present application. Figure 2 As shown, the server 201 can first construct multiple memory pools, a first mapping relationship, and a second mapping relationship according to the application startup 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 the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the 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 according to the memory application request, and determines the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship; then obtains the first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship, and directly sends the first memory address to the terminal device when determining that the first memory address is a valid address, so that the terminal device allocates the memory block corresponding to the first memory address to the application, so that the subsequent application can use the memory block corresponding to the first memory address to perform corresponding tasks (for example, loading game props, etc.).
[0051] It should be understood that Figure 1 and Figure 2 The application scenarios shown are only examples. In actual applications, the memory allocation method provided in the embodiments of the present application can also be applied to other application scenarios for memory allocation. No limitation is made to the memory allocation method provided in the embodiments of the present application.
[0052] The memory allocation method provided by this application is introduced below through embodiments.
[0053] Method Example 1
[0054] See also Figure 3 , Figure 3 The following is a flowchart of a memory allocation method provided in an embodiment of the present application. For ease of description, the following embodiment only takes the terminal device as the execution subject to introduce the memory allocation method. Figure 3 As shown, the memory allocation method includes S301-S304:
[0055] S301: upon receiving an application startup request, constructing N memory pools, a first mapping relationship, and a second mapping relationship.
[0056] An application is a computer software, and in particular, may be a computer software that needs to frequently request the release of small memory during operation. In addition, the embodiments of the present application are not limited to application programs, for example, the application program may be a game software, and the game software needs to frequently request the release of small memory during operation (such as loading a level with complex content, etc.).
[0057] The embodiments of the present application do not limit the method for obtaining the application startup request. For example, when a user clicks on an application icon on a terminal device, the application startup request is triggered, and the terminal device can then receive the application startup request.
[0058] N memory pools are used to provide memory blocks of various sizes to applications; N is a positive integer.
[0059] In addition, each memory pool includes at least one memory block. For example, when N is 3, the first memory pool may include 10,000 32-byte memory blocks, the second memory pool includes 5,000 64-byte memory blocks, and the third memory pool includes 1,000 256-byte memory blocks.
[0060] In addition, the sizes of the memory blocks in the same memory pool are the same, that is, a memory pool may include multiple memory blocks of the same size.
[0061] In addition, the memory blocks in the memory pool can be connected in a singly linked list. Figure 4 As shown, when the i-th memory pool includes M i When there are consecutive memory blocks with different addresses, and the i-th memory pool is connected according to a single linked list, the address pointer in the i-th memory pool can be set as follows: the memory address of the j+1th memory block of the i-th memory pool is stored in the jth memory block of the i-th memory pool, and the preset invalid address is stored in the Mth memory block of the i-th memory pool. imemory blocks, so that the memory address storage of the j+1th memory block of the ith memory pool can be directly found according to the address stored in the jth memory block of the ith memory pool.
[0062] In addition, the embodiment of the present application further provides a specific implementation method for constructing a memory pool. In this implementation method, when the memory blocks in the memory pool are connected according to a single linked list and the sizes of the memory blocks in the same memory pool are the same, the construction process of N memory pools specifically includes steps 11 to 13:
[0063] Step 11: Use the system memory allocation function to obtain the memory address of the original memory area.
[0064] The system memory request function refers to the function used by the above system memory allocation scheme to allocate memory.
[0065] The original memory area refers to a memory area with a larger memory space that is applied for the application when the application is started; moreover, the original memory area includes a large number of memory addresses with continuous addresses.
[0066] Step 12: Divide the memory addresses of the original memory area into memory pools to obtain memory addresses of N memory pools.
[0067] The embodiment of the present application does not limit the memory pool division, and the memory pool can be divided evenly or according to a preset memory pool size list, wherein the preset memory pool size list can be pre-set according to the application scenario.
[0068] Based on the above content, it can be known that in the embodiment of the present application, after the original memory area is obtained, the original memory area can be divided into N memory pools, so that each memory pool includes multiple continuous memory addresses. For example, when the original memory area is 896000 bytes, the original memory area can be divided into 3 memory pools, and the first memory pool is 320000 bytes, the second memory pool is 320000 bytes, and the third memory pool is 256000 bytes.
[0069] Step 13: Divide the memory address of the i-th memory pool into memory blocks according to the i-th size to obtain the M of the i-th memory pool. i memory blocks, stores the memory address of the j+1th memory block of the ith memory pool to the jth memory block of the ith memory pool, and stores the preset invalid address to the Mth memory block of the ith memory pool. i memory blocks; j is a positive integer, j≤M i -1;M i is a positive integer; i is a positive integer, i≤N.
[0070] The i-th size is pre-set according to the application scenario. For example, the first size is 32, the second size is 64, and the third size is 256.
[0071] The preset invalid address is used to mark the memory block as the last memory area in the memory pool to which it belongs; moreover, the embodiment of the present application does not limit the representation method of the preset invalid address, for example, the preset invalid address can be 0.
[0072] Based on the above content, it can be known that in an embodiment of the present application, after obtaining each memory pool, each memory pool can be evenly divided into multiple memory blocks, so that each memory pool includes multiple memory blocks of the same size. For example, when the original memory area is divided into 3 memory pools, and the first memory pool is 320,000 bytes, the second memory pool is 320,000 bytes, and the third memory pool is 256,000 bytes, the first memory pool can be evenly divided into 10,000 32-byte memory blocks, the second memory pool can be evenly divided into 5,000 64-byte memory blocks, and the third memory pool can be evenly divided into 1,000 256-byte memory blocks.
[0073] In addition, if Figure 4 As shown in the figure, the specific division process of the i-th memory pool is as follows: First, the memory address of the i-th memory pool is divided into memory blocks according to the i-th size to obtain the M of the i-th memory pool. i memory blocks, so that the M i The size of each memory block is the i-th size. Then, the memory address of the second memory block of the i-th memory pool is stored in the first memory block of the i-th memory pool, and the memory address of the third memory block of the i-th memory pool is stored in the second memory block of the i-th memory pool, and so on. i -The memory address of 1 memory block is stored in the Mth memory pool of the i-th memory pool. i -2 memory blocks, and in the Mth memory pool of the i-th memory pool i In this way, in the subsequent addressing process of the i-th memory pool, the memory address of the second memory block of the i-th memory pool can be found from the first memory block of the i-th memory pool, and the memory address of the third memory block of the i-th memory pool can be found from the second memory block of the i-th memory pool, and so on, until the memory address of the M-th memory block of the i-th memory pool is found. i When the preset invalid address is found in the memory blocks, it can be determined that all memory blocks in the i-th memory pool have been traversed. Where j is a positive integer, j≤M i -1;M i is a positive integer; i is a positive integer, i≤N.
[0074] Based on the above-mentioned contents related to the memory pool, it can be known that in the embodiment of the present application, when the application is started, multiple memory pools can be constructed first, so that each memory pool includes multiple memory blocks of the same size and connected addresses, and the sizes of the memory blocks in different memory pools are different. In this way, in the subsequent memory application process, the memory blocks in different memory pools can be used to respond to memory applications with different size requirements.
[0075] It should be noted that the embodiment of the present application does not limit the size of the original memory area, the size of each memory pool, and the method for obtaining the size of each memory block. For example, the above sizes can be set in advance according to the application scenario, or can be set based on the statistical information of the simulation test data of the application (for example, the application amount, application frequency, application peak value and other statistical information of memory of different sizes).
[0076] In addition, the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier.
[0077] The memory pool identifier is used to uniquely identify the memory pool. In addition, the embodiment of the present application does not limit the representation method of the memory identifier. For example, the memory identifier can be the arrangement number ("1", "2", "3", ...) of the memory pool in the original memory area.
[0078] In addition, the embodiment of the present application also provides an implementation method for constructing a first mapping relationship, which specifically includes steps 21 to 28:
[0079] Step 21: Calculate the difference between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference set.
[0080] The memory block size is used to indicate the size of the memory blocks in the memory pool. For example, when the memory pool includes 10,000 32-byte memory blocks, the memory block size of the memory pool is 32.
[0081] In the embodiment of the present application, after obtaining N memory pools, the difference between the memory block sizes of any two memory pools can be calculated using formula (1) to obtain a first difference set.
[0082] e xy =|l x -l y | (1)
[0083] In the formula, e xy Indicates the difference between the memory block size of the xth memory pool and the memory block size of the yth memory pool; l x Indicates the memory block size of the xth memory pool; l yIndicates the memory block size of the y-th memory pool; x is a positive integer, x≤N, and x≠y, y is a positive integer, y≤N; N is the total number of memory pools.
[0084] As an example, when the first memory pool includes 10,000 32-byte memory blocks, the second memory pool includes 5,000 64-byte memory blocks, and the third memory pool includes 1,000 256-byte memory blocks, the memory block size of the first memory pool is 32, the memory block size of the second memory pool is 64, and the memory block size of the third memory pool is 256; the difference between the memory block size of the first memory pool and the memory block size of the second memory pool is 32, the difference between the memory block size of the first memory pool and the memory block size of the third memory pool is 224, and the difference between the memory block size of the second memory pool and the memory block size of the third memory pool is 192. Thus, the first difference value set is {32, 224, 192}.
[0085] Step 22: Determine the minimum value in the first difference value set as the first size value.
[0086] In the embodiment of the present application, after the first difference value set is obtained, the minimum value in the first difference value set is determined as the first size value. For example, when the first difference value set is {32, 224, 192}, the first size value is 32.
[0087] Step 23: Determine the minimum value of the memory block sizes of the N memory pools as the second size value.
[0088] In the embodiment of the present application, after obtaining N memory pools, the minimum value of the memory block sizes of the N memory pools can be determined as the second size value. For example, based on the example content in step S21, the memory block size of the first memory pool is 32, the memory block size of the second memory pool is 64, and the memory block size of the third memory pool is 256, then the second size value is 32.
[0089] Step 24: Determine the minimum value of the first size value and the second size value as the third size value.
[0090] In the embodiment of the present application, after obtaining the first size value and the second size value, the minimum value of the first size value and the second size value can be determined as the third size value, which is specifically: if the first size value is greater than the second size value, the second size value is determined as the third size value; if the first size value is equal to the second size value, the second size value or the first size value is determined as the third size value; if the first size value is less than the second size value, the first size value is determined as the 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.
[0091] Step 25: Determine the maximum value of the memory block sizes of the N memory pools as the fourth size value.
[0092] In the embodiment of the present application, after obtaining N memory pools, the maximum value of the memory block sizes of the N memory pools can be determined as the fourth size value. For example, based on the example content in step S21, the memory block size of the first memory pool is 32, the memory block size of the second memory pool is 64, and the memory block size of the third memory pool is 256, then the fourth size value is 256.
[0093] Step 26: Determine the ratio of the fourth size value to the third size value as a first number, wherein the first number is used to represent the number of relations in the first mapping relationship.
[0094] In the embodiment of the present application, after obtaining the fourth size value and the third size value, the ratio of the fourth size value to the third size value can be determined as the first number, so that the first number can represent the number of relations 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 relations.
[0095] Step 27: Subtract the memory block size of each memory pool from r times the third size value to obtain a second difference value set, and use 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-th application size interval is (r-1 times the third size value, r times the third size value]; r is a positive integer, r≤the first number.
[0096] As an example, when the memory block size of the first memory pool is 32, the memory block size of the second memory pool is 64, and the memory block size of the third memory pool is 256, the third size value is 32, and the first number is 8; and the memory pool identifier of the first memory pool is "0", the memory pool identifier of the second memory pool is "1", and the memory pool identifier of the third memory pool is "2", then step 27 may specifically include the following steps:
[0097] The memory block size (32) of the first memory pool is subtracted from 1 times the third size value (32), the memory block size (64) of the second memory pool is subtracted from 1 times the third size value (32), and the memory block size (256) of the third memory pool is subtracted from 1 times the third size value (32) to obtain a second difference value set {0, 32, 224}, and the memory pool identifier ("0") of the memory pool corresponding to the minimum non-negative difference value (0) in the second difference value set is used as the memory pool identifier corresponding to the first application size interval ((0, 32]). That is, the application size interval (0, 32] corresponds to the memory pool identifier "0" of the first memory pool.
[0098] The memory block size (32) of the first memory pool is subtracted from twice the third size value (64), the memory block size (64) of the second memory pool is subtracted from twice the third size value (64), and the memory block size (256) of the third memory pool is subtracted from twice the third size value (64) to obtain a second difference value set {-32, 0, 192}, and the memory pool identifier ("1") of the memory pool corresponding to the minimum non-negative difference value (0) in the second difference value set is used as the memory pool identifier corresponding to the second application size interval ((32, 64]). That is, the application size interval (32, 64] corresponds to the memory pool identifier "1" of the second memory pool.
[0099] The memory block size (32) of the first memory pool is subtracted from three times the third size value (96), the memory block size (64) of the second memory pool is subtracted from three times the third size value (96), and the memory block size (256) of the third memory pool is subtracted from three times the third size value (96) to obtain a second difference value set {-64, -32, 160}, and the memory pool identifier ("2") of the memory pool corresponding to the minimum non-negative difference value (160) in the second difference value set is used as the memory pool identifier corresponding to the third application size interval ((64, 96]). That is, the application size interval (64, 96] corresponds to the memory pool identifier "2" of the third memory pool.
[0100] ... (and so on), and finally 8 corresponding relationships can be obtained, which are as follows: the application size interval (0,32] corresponds to the memory pool identifier "0" of the first memory pool; the application size interval (32,64] corresponds to the memory pool identifier "1" of the second memory pool; the application size interval (64,96] corresponds to the memory pool identifier "2" of the third memory pool; the application size interval (96,128] corresponds to the memory pool identifier "2" of the third memory pool; the application size interval (128,160] corresponds to the memory pool identifier "2" of the third memory pool; the application size interval (160,192] corresponds to the memory pool identifier "2" of the third memory pool; the application size interval (192,224] corresponds to the memory pool identifier "2" of the third memory pool; the application size interval (224,256] corresponds to the memory pool identifier "2" of the third memory pool.
[0101] Step 28: Establish a first mapping relationship according to each application size interval and its corresponding memory pool identifier.
[0102] The embodiment of the present application does not limit the representation method of the first mapping relationship. For example, it can be represented by a mapping table, a function, or an array. For ease of understanding, the following description is combined with the array method.
[0103] In a possible implementation manner, when the first mapping relationship is represented by an array, the first mapping relationship is [F 1 ,F 2 ,F 3 ,……,F R ], where F r is at the rth element in the array; and F r Indicates the memory pool identifier corresponding to the r-th application size interval (r-1 times the third size value, r times the third size value], r is a positive integer, r≤R, and R represents the total number of relations in the first mapping relationship (that is, the first number). In addition, the first mapping relationship can be used using formula (3).
[0104]
[0105] In the formula, S array Indicates the position of the memory pool identifier corresponding to the memory size to be queried in the array; l s Indicates the memory size to be queried; L a represents the third size value; [·] represents the rounding function.
[0106] To facilitate understanding of the above content, an example is provided below for explanation.
[0107] As an example, when the first mapping relationship is represented by an array, based on the example content of step 27, it can be known that the first mapping relationship is [0, 1, 2, 2, 2, 2, 2]. At this time, if the memory size to be queried is 50, the position of the memory pool identifier corresponding to the memory size to be queried in the array is [(50-1) / 32]+1=2, which shows that the memory pool identifier corresponding to the memory size to be queried 50 is located at the second element position in the array [0, 1, 2, 2, 2, 2, 2, 2], that is, the memory pool identifier corresponding to the memory size to be queried 50 is 1.
[0108] Based on the relevant content of the above-mentioned first mapping relationship, it can be known that in the embodiment of the present application, after obtaining N memory pools, the application size interval that each memory pool is responsible for responding can be determined according to the memory block size of each memory pool, so that different memory pools can manage memory applications of different sizes. It can be seen that because memory pools with different memory block sizes can manage memory applications of different sizes respectively, memory pools with different memory block sizes can process memory applications of different sizes at the same time, so that parallel execution of memory applications of different sizes can be achieved.
[0109] In addition, the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool.
[0110] 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. Figure 5 As shown, when the 1st to 3rd memory blocks of the i-th memory pool have been used, the 4th memory block is the next memory block to be allocated in the i-th memory pool, and the memory address of the 4th memory block is the memory address of the next memory block to be allocated in the memory pool. In addition, since the memory address of the 4th memory block is stored in the 3rd memory block, when the 3rd memory block is allocated to the application for use, the memory address of the 4th memory block can be directly read from the 3rd memory block, and the memory address of the 4th memory block is used as the memory address of the next memory block to be allocated in the memory pool corresponding to the memory pool identifier of the i-th memory pool, and recorded in the second mapping relationship, so that the second mapping relationship can clearly record the corresponding relationship between the memory address of the 4th memory block and the memory pool identifier of the i-th memory pool.
[0111] In addition, when the application startup request is received, the second mapping relationship can be initialized, specifically: the second mapping relationship includes N corresponding relationships, and the i-th relationship is that the memory pool identifier of the i-th memory pool corresponds to the memory address of the memory block with the highest memory address sorting in the i-th memory pool. Wherein, i is a positive integer, i≤N.
[0112] In addition, the second mapping relationship is updated as the memory is requested and released by the application program. The update process will be described in detail below.
[0113] Based on the above content, it can be known that in an embodiment of the present application, when a user wants to run an application on a terminal device, the user can trigger an application startup request by clicking on the application icon on the terminal device, so that after receiving the application startup request, the terminal device constructs N memory pools, so that different memory pools are responsible for managing memory applications of different sizes; at the same time, it is also necessary to construct a first mapping relationship and a second mapping relationship, so that the first mapping relationship can be used to determine the memory pool that each memory application should use, and according to the second mapping relationship, the memory address of the next memory block to be allocated in the memory pool is allocated to the application, so that the application can use the memory block corresponding to the memory address to perform the corresponding task.
[0114] S302: After receiving the memory application request sent by the application, obtain the memory application size of the application according to the memory application request.
[0115] The memory application request refers to a request sent by an application program for applying for memory to be allocated; and the memory application request carries the size of the memory to be allocated.
[0116] The memory request size of an application refers to the size of the memory to be allocated.
[0117] Based on the above content, it can be known that during the running of the application, after receiving the memory application request sent by the application, the size of the memory to be allocated can be directly extracted from the memory application request as the memory application size of the application, so that subsequently, based on the memory application size of the application, a memory block that matches the memory application size of the application can be determined from the memory pool.
[0118] S303: Determine a first identifier of a first memory pool according to the memory application size of the application program and the first mapping relationship.
[0119] The embodiment of the present application does not limit the implementation of S303, and two possible implementations are described below.
[0120] In a first possible implementation, when the first mapping relationship is represented by a mapping table, S303 may specifically be: first determine the target application size interval to which the memory application size of the application belongs based on the memory application size of the application and the first mapping relationship; and then determine the first identifier of the first memory pool based on the target application size interval and the first mapping relationship.
[0121] It can be seen that in the embodiment of the present application, when the first mapping relationship is represented by a mapping table, after obtaining the memory application size of the application, the memory application size of the application can be compared with each application size interval in the first mapping relationship to determine the target application size interval to which the memory application size of the application belongs; then, the memory pool identifier corresponding to the target application size interval is searched in the first mapping relationship as the first identifier of the first memory pool. For example, based on the example content in step 27 above, when the memory application size of the application is 50, because the memory application size 50 of the application belongs to the application size interval (32,64], the first identifier corresponding to the memory application size 50 of the application should be the memory pool identifier "1" of the second memory pool.
[0122] Based on the relevant content of the above-mentioned first possible implementation method, it can be known that when the first mapping relationship is represented by a mapping table, the first mapping relationship can be used to determine the target application size interval to which the application's memory application size belongs, and then the target application size interval and the first mapping relationship can be 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 for use.
[0123] In a second possible implementation, when the first mapping relationship is represented by an array, S303 may specifically be as follows: first, according to the memory application size of the application, determine the first position using formula (3), where the first position represents the position of the memory pool identifier corresponding to the memory application size of the application in the first mapping relationship; and then determine the memory pool identifier at the first position in the first mapping relationship as the first identifier of the first memory pool.
[0124] Based on the above related content, it can be known that in the embodiment of the present application, after obtaining the memory application size of the application, the memory application size of the application can be used as the memory size to be queried S array , and then use formula (3) to determine the memory pool identifier corresponding to the application's memory application size in the first mapping relationship (that is, array [F 1 ,F 2 ,F 3 ,……,F R ]), and determines 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 content in step S28, when the memory request size of the application is 50, because the memory pool identifier corresponding to the memory request size of the application is 50, it is located at the second element position in the array [0,1,2,2,2,2,2,2], so that the first identifier corresponding to the memory request size of the application is 50 should be the memory pool identifier "1" of the second memory pool.
[0125] Based on the relevant content of S303 above, it can be known that in an embodiment of the present application, after obtaining the memory request size of the application, the first identifier of the first memory pool can be determined according to the memory request size of the application 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 for use based on the first identifier.
[0126] S304: According to the first identifier and the second mapping relationship, obtain a first memory address corresponding to the first identifier, and when it is determined that the first memory address is a valid address, send the first memory address to the application.
[0127] The effective address corresponds to the invalid address mentioned above. The effective address refers to a non-invalid address, that is, an address corresponding to an actual memory block.
[0128] In an embodiment of the present application, after obtaining the first identifier, the memory address of the next memory block to be allocated in the memory pool corresponding to the first identifier can be queried in the second mapping relationship as the first memory address; then, it is determined whether the first memory address is a valid address. If so, it is determined that there are available free memory blocks in the first memory pool at the current moment. At this time, the first memory address can be directly sent to the application so that the application can use the memory block corresponding to the first memory address to perform the corresponding task; if not, it is determined that all memory blocks in the first memory pool are in use at the current moment, so that the first memory pool cannot allocate memory to the application. At this time, in order to ensure the normal operation of the application, the system memory request function can be called so that memory can be allocated to the application according to the default system memory allocation scheme.
[0129] Based on the relevant contents of the above steps S301 to S304, it can be known that in the memory allocation method provided in the embodiment of the present application, when receiving an application startup request, multiple 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, and the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. Then, after receiving the memory application request sent by the application, the memory application size of the application is first obtained according to the memory application request, and the first identifier of the first memory pool is determined according to the memory application size of the application and the first mapping relationship; then, according to the first identifier and the second mapping relationship, the first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application so that the subsequent application can use the memory block corresponding to the first memory address.
[0130] It can be seen that since each memory pool, the first mapping relationship and the second mapping relationship are all constructed when the application is started, after receiving the memory application request sent by the application during the running of the application, it is only necessary to query the first mapping relationship and the second mapping relationship to determine the memory allocated to the application. This simplifies the memory application process during the running of the application, which is beneficial to improving the memory allocation efficiency, and thus is beneficial to improving the running smoothness of the application.
[0131] Method Example 2
[0132] In addition, if the first memory address is sent to the application, it means that the memory block corresponding to the first memory address is allocated to the application for use, thereby indicating that the memory block corresponding to the first memory address is in use at the current moment. At this time, in order to enable the first memory pool described by the first memory address to continue to allocate the space memory block to the application for use, it is necessary to update the second mapping relationship after determining the first memory address, so that the second mapping relationship can be used to find other free memory blocks in the first memory pool for allocation.
[0133] Based on this, the embodiment of the present application also provides an implementation of a memory allocation method. In this implementation, in addition to the above S301 to S304, the memory allocation method may also include S305-S307 (such as Figure 6 shown):
[0134] S305: When it is determined that the first memory address is a valid address, determine a first memory block according to the first memory address.
[0135] The first memory block refers to a memory block having a first memory address in the first memory pool.
[0136] S306: Read the second memory address stored in the first memory block.
[0137] The second memory address refers to the address stored in the first memory block.
[0138] S307: Using the first identifier and the second memory address, update the second mapping relationship so that the second mapping relationship includes a corresponding relationship between the first identifier and the second memory address.
[0139] In an embodiment of the present application, after determining that the first memory address is a valid address, the memory block corresponding to the first memory address can be first determined as the first memory block, and the second memory address can be 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 to the next memory block to be allocated in the first memory pool corresponding to the first identifier, so as to use the first identifier and its corresponding next memory block to be allocated in the first memory pool (that is, the second memory address) to update the second mapping relationship, and the update process is specifically as follows: deleting the correspondence between the first identifier and the first memory address, and adding the correspondence between the first identifier and the second memory address.
[0140] As an example, Figure 7 As shown, when the first memory pool is Figure 5For the i-th memory pool shown, when the first memory address is the address of the 4th memory block of the i-th memory pool, S305-S307 may specifically be: after determining that the address of the 4th memory block of the i-th memory pool is a valid address, first determine the 4th memory block of the i-th memory pool according to the address of the 4th memory block of the i-th memory pool, and read the address of the 5th memory block of the i-th memory pool from the 4th memory block of the i-th memory pool; then determine the address of the 5th memory block of the i-th memory pool as the next memory block to be allocated in the i-th memory pool, and replace the next memory to be allocated in the i-th memory pool corresponding to the memory identifier of the i-th memory pool (that is, the first identifier) with the address of the 5th memory block of the i-th memory pool in the second mapping relationship, so that when the next memory to be allocated in the i-th memory pool is subsequently searched for with the help of the memory identifier of the i-th memory pool and the second mapping relationship, the address of the 5th memory block of the i-th memory pool can be directly found.
[0141] Based on the above-mentioned contents of S305 to S307, it can be known that in the embodiment of the present application, the second mapping relationship will be updated with the memory application process of the application program, which is specifically: when determining to allocate the first memory block corresponding to the first memory address to the application program, the next memory block to be allocated in the first memory pool corresponding to the first identifier can be replaced with the second memory address stored in the first memory block in the second mapping relationship, so that in the subsequent memory allocation process, the second memory address in the first memory pool can be directly found with the help of the first identifier and the second mapping relationship. It can be seen that because the second mapping relationship will be updated with the memory application process of the application program, the second mapping relationship can accurately reflect the usage status of different memory blocks in each memory pool, so that based on the second mapping relationship, each memory pool can accurately determine the memory block that can be allocated to the application program at the current moment, which is conducive to improving the allocation efficiency under the premise of improving the allocation accuracy.
[0142] In addition, in order to improve the accuracy of memory allocation, each memory pool can be set to only respond to one memory application request of an application within a time period. That is, when a memory pool is responding to a 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 this memory pool can respond to other requests.
[0143] Based on this, the embodiment of the present application also provides another implementation of the memory allocation method. In this implementation, in addition to including all or part of the above steps, the memory allocation method may also include S308-S313 (such as Figure 8 shown):
[0144] S308: When receiving an application startup request, construct a third mapping relationship.
[0145] The third mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory pool enablement state.
[0146] The memory enablement state is used to indicate whether the memory pool is in a state where it is allowed to be used. In addition, when the memory usage state of a memory pool is in a state where it is allowed to be used, it means that the memory pool can allocate memory blocks to applications; however, when the memory usage state of a memory pool is in a state where it is not allowed to be used, it means that the memory pool cannot allocate memory blocks to applications temporarily.
[0147] In addition, in the initial third mapping relationship (that is, the third mapping relationship constructed when the application startup request is received), the memory pool enablement state corresponding to each memory pool identifier is the use-allowed state.
[0148] Based on the above content, it can be known that in the embodiment of the present application, when an application startup request is received, a third mapping relationship can also be constructed so that the third mapping relationship can accurately record the enable status of each memory pool.
[0149] S309: Determine an enabling state of the first memory pool according to the first identifier and the third mapping relationship.
[0150] In an embodiment of the present application, after obtaining the first identifier, the memory pool enablement state corresponding to the first identifier can be found from the third mapping relationship as the enablement state of the first memory pool, so that it can be subsequently determined whether the first memory pool can be used based on the enablement state of the first memory pool.
[0151] It should be noted that S309 needs to be executed after S303 is executed and before S304 is executed.
[0152] S310: Determine whether the enabling state of the first memory pool is a use-permitted state. If so, execute S311; if not, execute S313.
[0153] S311: Update the enabled state of the first memory pool in the third mapping relationship to a state where use is not allowed, and continue to execute the above S304-S307.
[0154] In the embodiment of the present application, when it is determined that the enable state of the first memory pool is in the state of allowing use, it can be determined that the first memory pool can respond to the memory application request of the application at the current moment. At this time, in order to achieve the purpose of the first memory pool responding to only one request in a time period, the enable state of the first memory pool in the third mapping relationship can be updated to a state of not allowing use, and then the above S304-S307 is executed. Among them, because during the execution of S304 to S307, the enable state of the first memory pool recorded in the third mapping relationship is a state of not allowing use, the first memory pool no longer responds to any other requests during the execution of S304 to S307, so that the first memory pool can accurately and smoothly execute S304 to S307, so that the confusion caused by the first memory pool responding to two requests in the same time period can be effectively avoided, so that the accuracy of memory allocation of the memory pool is improved.
[0155] S312: Update the enabled state of the first memory pool in the third mapping relationship to a use-allowed state.
[0156] In an embodiment of the present application, after the first memory pool executes the above S304-S307, it can be determined that the first memory pool has completed responding to the memory application request sent by the application, and thus it can be determined that the first memory pool is already in a stable state. At this time, the first memory pool can continue to respond to other requests. Therefore, after executing S304-S307, the enable state of the first memory pool in the third mapping relationship can be restored to a state that allows use, so that the first memory pool can respond to other requests.
[0157] S313: Wait for a preset time and continue to execute S309.
[0158] The preset duration can be set in advance according to the application scenario. For example, the preset duration can be 3 seconds.
[0159] In the embodiment of the present application, when it is determined that the enabled state of the first memory pool is not allowed to be used, it can be determined that the first memory pool is processing other requests at the current moment, so that it can be determined that the first memory pool cannot temporarily respond to the memory application request of the application at the current moment. At this time, it is possible to wait for a preset time and query again whether the enabled state of the first memory pool in the second mapping relationship has changed. If it has not changed, continue to wait for the preset time; ... (repeat multiple times), until it is determined that the enabled state of the first memory pool in the second mapping relationship is allowed to be used, then the above S311 and its subsequent steps can be executed.
[0160] Based on the above-mentioned contents of S308 to S313, it can be known that in the embodiment of the present application, after determining the first identifier of the first memory pool, the third mapping relationship can be used to determine whether the first memory pool is in a state where use is allowed, so that when it is determined that the first memory pool is in a state where use is not allowed, the user waits until the first memory pool is in a state where use is allowed, and then allocates a memory block in the first memory pool to the application program according to the first identifier and the second mapping relationship. Among them, because the memory pool can only process one request in a time period, the first memory pool can accurately and smoothly respond to the request of the application program, which can effectively avoid the confusion caused by the first memory pool responding to two requests at the same time in a time period, thereby improving the accuracy of memory allocation of the memory pool.
[0161] In addition, when the application has finished using the memory block, the memory block can be released so that when the memory pool responds to the memory application request again, the released memory block will be given priority to be provided to the application for use again. In this way, it can be ensured that only the memory blocks with the highest order in the memory pool are used before the memory blocks with the lowest order are further used. Based on this, the embodiment of the present application also provides another implementation of the memory allocation method. In this implementation, in addition to all or part of the above steps, the memory allocation method can also include S314-S319 (such as Fig. 9 shown):
[0162] S314: When receiving an application startup request, construct a fourth mapping relationship.
[0163] The fourth mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the memory pool.
[0164] In an embodiment of the present application, after obtaining N memory pools, a fourth mapping relationship can be constructed using the memory identifiers and memory addresses of each memory pool, so that the fourth mapping relationship can be used to record the memory address of each memory pool, so that the memory address of a memory pool can be directly found from the fourth mapping relationship by using the memory identifier of the memory pool.
[0165] S315: After receiving the memory release request sent by the application, determine the memory address of the memory area to be released according to the memory release request.
[0166] The memory release request is used to represent a request sent by an application to release a memory area to be released; moreover, the memory release request carries a memory address of the memory area to be released.
[0167] In an embodiment of the present application, after receiving a memory release request sent by an application, the memory address of the memory area to be released can be directly extracted from the memory release request, so that the memory can be released based on the memory address later.
[0168] S316: According to the memory address of the original memory area and the memory address of the memory area to be released, determine whether the memory area to be released belongs to the original memory area. If so, execute S317; if not, execute S319.
[0169] The original memory area includes N memory pools, and the relevant content of the original memory area can be found above.
[0170] In an embodiment of the present application, after obtaining the memory address of the memory area to be released, it is possible to first determine whether the memory area to be released belongs to the original memory area based on the memory address of the original memory area and the memory address of the memory area to be released. If so, it is determined that the memory area to be released belongs to a memory block in the memory pool. At this time, the following S317-S318 can be used to complete the memory block release process; if not, it is determined that the memory area to be released does not belong to a memory block in the memory pool. At this time, the system memory release function can be directly used to release the memory area to be released.
[0171] S317: Determine a second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relationship.
[0172] The second memory area is used to represent the memory pool to which the memory area to be released belongs.
[0173] In an 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 can be queried in the fourth mapping relationship to determine the second identifier of the second memory pool, and the query process can be specifically as follows: first, according to the memory address of the memory area to be released and the fourth mapping relationship, determine the memory address of the target memory pool to which the memory address of the memory area to be released belongs; then, according to the fourth mapping relationship and the memory address of the target memory pool, determine the second identifier of the second memory pool. That is, when it is determined that the memory area to be released belongs to the original memory area, first, use the memory addresses of each memory pool recorded in the fourth mapping relationship to compare with the memory address of the memory area to be released, and determine the memory address of the target memory pool to which the memory address of the memory area to be released belongs; then, directly search the memory identifier corresponding to the memory address of the target memory pool in the fourth mapping relationship as the second identifier of the second memory pool.
[0174] S318: According to the second identifier and the second mapping relationship, obtain the third memory address corresponding to the second identifier, store the third memory address in the memory area to be released, and use the second identifier and the memory address of the memory area to be released to update the second mapping relationship, so that the second mapping relationship includes the correspondence between the second identifier and the memory address of the memory area to be released.
[0175] In an embodiment of the present application, in order to achieve the purpose of further using memory blocks with later memory address arrangement order only after all memory blocks with earlier memory address arrangement order are used in the memory pool, after determining the memory pool to which the memory area to be released belongs, first query 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 moment from the second mapping relationship as the third memory address; then store the third memory address in the memory area to be released, and replace 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 memory block corresponding to the third memory address is used after the memory area to be released. For ease of understanding, the following is combined with Figure 7 The example shown is for illustration
[0176] As an example, when the first memory pool is Figure 5 , when the third memory address is the address of the fourth memory block of the i-th memory pool, and the memory area to be released is the address of the first memory block of the i-th memory pool, S318 may specifically be: when it is determined that the memory area to be released belongs to the i-th memory pool, first, from the second mapping relationship, it may be obtained that the memory address of the next memory block to be allocated in the i-th memory pool is the address of the fourth memory block of the i-th memory pool; and then, the address of the fourth memory block of the i-th memory pool is stored in the first memory block of the i-th memory pool. block, so that when the first memory block of the ith memory pool is allocated to the application for use in the future, the address of the fourth memory block of the ith memory pool can be read from the first memory block of the ith memory pool; at the same time, 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 with the first memory block of the ith memory pool, so that the ith memory pool can directly allocate the first memory block of the ith memory pool to the application for use again when responding to the memory application request of the application next time.
[0177] It can be seen that in the embodiment of the present application, each memory pool can give priority to using the released memory blocks to respond to the memory application request of the application program, so that the memory pool can give priority to using the memory blocks with the highest memory address sorting to respond to the memory application request of the application program, so that the size of the memory pool can be adjusted based on the usage of the memory blocks in each memory pool in the future, so that the adjusted memory pool can reduce the size of the memory pool under the premise of satisfying the memory usage of the application program. For example, when the memory pool includes 1000 memory blocks, if it is found in the actual application process that the 800 memory blocks with the highest memory address sorting in the memory pool have been used, but the 200 memory blocks with the highest memory address sorting have never been used, it can be determined that the size of the memory pool is too large and the size of the memory pool should be reduced.
[0178] S319: Use the system memory release function to release the memory area to be released.
[0179] The system memory release function refers to the function used by the above system memory allocation scheme to release memory.
[0180] Based on the relevant contents of S314 to S319 above, it can be known that in an embodiment of the present application, after receiving a memory release request sent by an application, the memory address of the memory area to be released is first determined according to the memory release request, and then, according to the memory address of the original memory area and the memory address of the memory area to be released, it is judged whether the memory area to be released belongs to the original memory area, so that when it is determined that the memory area to be released belongs to the original memory area, the second identifier of the second memory pool is first determined according to the memory address of the memory area to be released and the fourth mapping relationship, and the third memory address corresponding to the second identifier is obtained according to the second identifier and the second mapping relationship; then, the third memory address is stored in the memory area to be released, and the second identifier and the memory address of the memory area to be released are used to update the second mapping relationship, so that the second mapping relationship includes the correspondence between the second identifier and the memory address of the memory area to be released.
[0181] Among them, 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 can be directly replaced with the memory address of the memory area to be released, so that the usage order of the memory block corresponding to the third memory address is located after the memory area to be released. It can be seen that in the embodiment of the present application, each memory pool can give priority to using the released memory block to respond to the memory application request of the application program, so that the memory pool can give priority to using the memory block with a higher memory address sorting to respond to the memory application request of the application program, so that the size of the memory pool can be adjusted based on the usage of the memory blocks in each memory pool in the future, so that the adjusted memory pool can reduce the size of the memory pool on the premise of meeting the memory usage of the application program.
[0182] In addition, in order to improve the accuracy of memory release, each memory pool can be set to only respond to one memory release request of an application within a time period. That is, when a memory pool is responding to a memory release request, the memory pool cannot respond to other requests (for example, memory application requests or memory release requests), but other memory pools except this memory pool can respond to other requests.
[0183] Based on this, the embodiment of the present application also provides another implementation of the memory allocation method. In this implementation, in addition to including all or part of the above steps, the memory allocation method may also include S320-S324 (such as Fig.10 shown):
[0184] S320: Determine an enabling state of the second memory pool according to the second identifier and the third mapping relationship.
[0185] In an embodiment of the present application, after obtaining the second identifier, the memory pool enablement state corresponding to the second identifier can be found from the third mapping relationship as the enablement state of the second memory pool, so that it can be subsequently determined whether the second memory pool can be used based on the enablement state of the second memory pool.
[0186] It should be noted that S320 needs to be executed after S317 is executed and before S318 is executed.
[0187] S321: Determine whether the enabling state of the second memory pool is a use-permitted state. If so, execute S322; if not, execute S324.
[0188] S322: Update the enabled state of the second memory pool in the third mapping relationship to a state where use is not allowed, and continue to execute the above S318.
[0189] In the embodiment of the present application, when it is determined that the enabled state of the second memory pool is in the allowed state, it can be determined that the second memory pool can respond to the memory release request of the application at the current moment. At this time, in order to achieve the purpose of the second memory pool responding to only one request in a time period, the enabled state of the second memory pool in the third mapping relationship can be updated to a not allowed state, and then the above S318 is executed. Among them, because during the execution of S318, the enabled state of the second memory pool recorded in the third mapping relationship is a not allowed state, the second memory pool no longer responds to any other requests during the execution of S318, so that the second memory pool can accurately and smoothly execute S318, so that the confusion caused by the second memory pool responding to two requests in the same time period can be effectively avoided, so that the accuracy of memory release of the memory pool is improved.
[0190] S323: Update the enabled state of the second memory pool in the third mapping relationship to a use-allowed state.
[0191] In an embodiment of the present application, after the second memory pool executes the above S318, it can be determined that the second memory pool has completed responding to the memory release request sent by the application, and thus it can be determined that the second memory pool is already in a stable state. At this time, the second memory pool can continue to respond to other requests. Therefore, after executing S318, the enable state of the second memory pool in the third mapping relationship can be restored to a state that allows use, so that the second memory pool can respond to other requests.
[0192] S324: Wait for a preset time and continue to execute S320.
[0193] In the embodiment of the present application, when it is determined that the enabled state of the second memory pool is not allowed to be used, it can be determined that the second memory pool is processing other requests at the current moment, so that it can be determined that the second memory pool cannot temporarily respond to the memory release request of the application at the current moment. At this time, it is possible to wait for a preset time and query again whether the enabled state of the second memory pool in the second mapping relationship has changed. If it has not changed, continue to wait for the preset time; ... (repeat multiple times), until it is determined that the enabled state of the second memory pool in the second mapping relationship is allowed to be used, then the above S322 and subsequent steps can be executed.
[0194] Based on the above-mentioned contents of S320 to S324, it can be known that in the embodiment of the present application, after determining the second identifier of the second memory pool, the third mapping relationship can be used to determine whether the second memory pool is in a state where use is allowed, so that when it is determined that the second memory pool is in a state where use is not allowed, the user waits until it is determined that the second memory pool is in a state where use is allowed, and then the memory area to be released is released according to the second identifier and the second mapping relationship. Among them, because the memory pool can only process one request in a time period, the second memory pool can accurately and smoothly respond to the request of the application, which can effectively avoid the confusion caused by the second memory pool responding to two requests at the same time in a time period, thereby improving the accuracy of memory allocation of the memory pool.
[0195] In order to further understand the memory allocation method provided in the embodiment of the present application, the scenario embodiment will be combined with Fig.11 An overall introduction to the memory allocation method provided in the embodiments of the present application is given.
[0196] Scenario Example
[0197] The scenario embodiment is an exemplary introduction to the memory allocation method provided by the method embodiment. For the sake of brevity, some contents in the scenario embodiment are the same as those in the method embodiment and will not be repeated here. For technical details of the parts that are the same, please refer to the relevant contents in the method embodiment.
[0198] See also Fig.11 , which is an example of an embodiment of the present application provided for application Figure 1 Flowchart of the memory allocation method in the application scenario shown.
[0199] The memory allocation method provided in the embodiment of the present application includes S1101-S1120:
[0200] S1101: When receiving an application startup request triggered by a user, the terminal device uses a system memory request function to obtain a memory address of an original memory area.
[0201] For details about the original memory area, see step 11 above.
[0202] Applications can refer to game software, especially MMORPG software built with the Mercury Engine. MMORPG software has rich game content, supports seamless connection of large scenes, contains various large and small objects, has complex characters, has many costumes, and supports hundreds of people fighting on the same screen. It can be seen that so many service requirements have high requirements for memory application and allocation performance during game loading and running.
[0203] In the embodiment of the present application, when the user clicks the application icon on the terminal device (for example, Fig.12 When the game software icon shown in the figure is displayed, an application startup request is triggered. At this time, the terminal device can receive the application startup request and use the system memory application function to obtain the memory address of the original memory area, so that a memory pool can be built based on the original memory area later.
[0204] S1102: The terminal device uses the original memory area to build N memory pools.
[0205] It should be noted that S1102 can use the above steps 12 to 13 to build N memory pools. Please refer to the above steps 12 to 13.
[0206] S1103: The terminal device constructs a first mapping relationship, a second mapping relationship, a third mapping relationship, and a fourth mapping relationship according to the N memory pools.
[0207] Among them, the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record 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 fourth mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the memory pool; the third mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory pool enable status.
[0208] In addition, the embodiments of the present application do not limit the relationship between the first mapping relationship, the second mapping relationship, the fourth mapping relationship and the third mapping relationship in actual application. For example, a global array can be used to represent the second mapping relationship, the fourth mapping relationship and the third mapping relationship, and a static array can be used to represent the first mapping relationship. In this way, the global array is used to record the correspondence between the memory pool identifier and the memory pool size, memory address, memory block size, memory address of the next memory block to be allocated, and enable status of the memory pool; the static array is used to record the correspondence between the application size interval and the memory pool identifier.
[0209] S1104: After receiving the memory application request sent by the application, the terminal device obtains the memory application size of the application according to the memory application request, and determines the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship.
[0210] S1105: The terminal device determines the enabling state of the first memory pool according to the first identifier and the third mapping relationship.
[0211] S1106: The terminal device determines whether the enabling state of the first memory pool is a use-permitted state, and if so, executes S1108; if not, executes S1107.
[0212] S1107: The terminal device waits for a preset time and continues to execute S1105.
[0213] S1108: The terminal device updates the enabled state of the first memory pool in the third mapping relationship to a non-usage state, and obtains a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship.
[0214] S1109: The terminal device determines whether the first memory address is a valid address, and if so, executes S1110; if not, executes S1111.
[0215] S1110: The terminal device determines the first memory block according to the first memory address, reads the second memory address stored in the first memory block, and updates the second mapping relationship using the first identifier and the second memory address, so that the second mapping relationship includes the correspondence between the first identifier and the second memory address; and sends the first memory address to the application.
[0216] S1111: The terminal device uses the system memory request function to allocate memory that matches the memory request size of the application to the application.
[0217] S1112: The terminal device updates the enable state of the first memory pool in the third mapping relationship to a use-allowed state.
[0218] S1113: After receiving the memory release request sent by the application, the terminal device determines the memory address of the memory area to be released according to the memory release request.
[0219] S1114: The terminal device determines whether the memory area to be released belongs to the original memory area according to the memory address of the original memory area and the memory address of the memory area to be released. If so, execute S1116; if not, execute S1115.
[0220] S1115: The terminal device uses the system memory release function to release the memory area to be released.
[0221] S1116: The terminal device determines the second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relationship.
[0222] S1117: The terminal device determines the enabling state of the second memory pool according to the second identifier and the third mapping relationship.
[0223] S1118: The terminal device determines whether the enabling state of the second memory pool is a use-permitted state. If so, execute S1119; if not, execute S1120.
[0224] S1119: The terminal device updates the enable state of the second memory pool in the third mapping relationship to a state not allowed to be used, obtains the third memory address corresponding to the second identifier according to the second identifier and the second mapping relationship, stores the third memory address in the memory area to be released, and updates the second mapping relationship using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship includes the correspondence between the second identifier and the memory address of the memory area to be released, and updates the enable state of the second memory pool in the third mapping relationship to a state allowed to be used.
[0225] S1120: The terminal device waits for a preset time and continues to execute S1117.
[0226] Based on the relevant contents of S1101 to S1120 above, it can be known that in the embodiment of the present application, since each memory pool, the first mapping relationship and the second mapping relationship are all constructed when the application is started, after receiving the memory application request sent by the application during the operation of the application, it is only necessary to query the first mapping relationship and the second mapping relationship to determine the memory allocated to the application. This simplifies the memory application process during the operation of the application, which is beneficial to improving the memory allocation efficiency, thereby helping to improve the running smoothness of the application.
[0227] Based on the relevant contents of the memory allocation method provided above, the embodiment of the present application also provides a corresponding memory allocation device, so that the memory allocation method provided above can be applied and implemented in practice.
[0228] Device Embodiment
[0229] It should be noted that the technical details of the memory allocation device provided in this embodiment may refer to the memory allocation method provided above.
[0230] See also Fig.13 , which is a structural diagram of a memory allocation device provided in an embodiment of the present application.
[0231] The memory allocation device 1300 provided in the embodiment of the present application includes:
[0232] The construction unit 1301 is used to construct N memory pools, a first mapping relationship and a second mapping relationship when receiving an application startup request; the memory pool includes at least one memory block; the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer;
[0233] The first processing unit 1302 is configured to, after receiving the memory application request sent by the application, obtain the memory application size of the application according to the memory application request, and determine the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship;
[0234] The allocation unit 1303 is used 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 when it is determined that the first memory address is a valid address.
[0235] Optional, in Fig.13Based on the memory allocation device 1300 shown, the construction unit 1301 is specifically used to: when the memory blocks in the memory pool are connected according to a single linked list and the sizes of the memory blocks in the same memory pool are the same,
[0236] Use the system memory allocation function to obtain the memory address of the original memory area;
[0237] Divide the memory addresses of the original memory area into memory pools to obtain memory addresses of N memory pools;
[0238] The memory address of the i-th memory pool is divided into memory blocks according to the i-th size to obtain Mi memory blocks of the i-th memory pool, the memory address of the j+1-th memory block of the i-th memory pool is stored in the j-th memory block of the i-th memory pool, and the preset invalid address is stored in the Mi-th memory block of the i-th memory pool; j is a positive integer, j≤Mi-1; Mi is a positive integer; i is a positive integer, i≤N.
[0239] Optional, in Fig.13 Based on the memory allocation device 1300 shown in the figure, the construction unit 1301 is specifically used to: when the sizes of the memory blocks in the same memory pool are the same,
[0240] Calculating the difference between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference set; the memory block size is used to characterize the size of the memory block in the memory pool;
[0241] Determine the minimum value in the first difference value set as a first size value, determine the minimum value in the memory block sizes of the N memory pools as a second size value, and determine the minimum value between the first size value and the second size value as a third size value;
[0242] Determine the maximum value of the memory block sizes of the N memory pools as a fourth size value, and determine the ratio of the fourth size value to the third size value as a first number; the first number is used to represent the number of relations in the first mapping relationship;
[0243] Subtract the memory block size of each of the memory pools from r times the third size value to obtain a second difference value set, and use 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-th application size interval is the interval (r-1 times the third size value, r times the third size value]; r is a positive integer, r≤the first number;
[0244] A first mapping relationship is established according to each application size interval and its corresponding memory pool identifier.
[0245] Optional, in Fig.13 Based on the memory allocation device 1300 shown, the first processing unit 1302 is specifically used for:
[0246] Determine, according to the memory application size of the application and the first mapping relationship, a target application size interval to which the memory application size of the application belongs;
[0247] A first identifier of a first memory pool is determined according to the target application size interval and the first mapping relationship.
[0248] Optional, in Fig.13 The memory allocation device 1300 shown is based on Fig.14 As shown, the memory allocation device 1300 also includes:
[0249] The first update unit 1304 is used to determine the first memory block according to the first memory address when determining that the first memory address is a valid address; read the second memory address stored in the first memory block; and use the first identifier and the second memory address to update the second mapping relationship so that the second mapping relationship includes the correspondence between the first identifier and the second memory address.
[0250] Optional, in Fig.13 or Fig.14 The memory allocation device 1300 shown is based on Fig.15 As shown, the memory allocation device 1300 also includes:
[0251] The construction unit 1301 is further configured to construct a third mapping relationship when receiving an application startup request;
[0252] A determining unit 1305 is configured to determine an enabling state of the first memory pool according to the first identifier and the third mapping relationship;
[0253] The allocation unit 1303 is specifically configured to obtain a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship when it is determined that the enable state of the first memory pool is a use-allowed state.
[0254] Optional, in Figure 13-Figure 15 Based on any of the memory allocation devices 1300 shown in the above, Fig.16 As shown, the memory allocation device 1300 also includes:
[0255] The construction unit 1301 is further used to construct a fourth mapping relationship when receiving an application startup request; the fourth mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the memory pool;
[0256] The second processing unit 1306 is used for, after receiving the memory release request sent by the application, determining the memory address of the memory area to be released according to the memory release request, and judging whether the memory area to be released belongs to the original memory area 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 includes N memory pools;
[0257] The release unit 1307 is used to determine the second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relationship when it is determined that the memory area to be released belongs to the original memory area, and obtain the third memory address corresponding to the second identifier according to the second identifier and the second mapping relationship; store the third memory address to the memory area to be released, and use the second identifier and the memory address of the memory area to be released to update the second mapping relationship, so that the second mapping relationship includes the correspondence between the second identifier and the memory address of the memory area to be released.
[0258] Based on the relevant contents of the above-mentioned memory allocation device 1300, it can be known that in the memory allocation method provided in the embodiment of the present application, when receiving an application startup request, multiple 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, and the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier, and the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the address of the next memory block to be allocated in the memory pool. Then, after receiving the memory application request sent by the application, the memory application size of the application is first obtained according to the memory application request, and the first identifier of the first memory pool is determined according to the memory application size of the application and the first mapping relationship; then, according to the first identifier and the second mapping relationship, the first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application so that the subsequent application can use the memory block corresponding to the first memory address.
[0259] It can be seen that since each memory pool, the first mapping relationship and the second mapping relationship are all constructed when the application is started, after receiving the memory application request sent by the application during the running of the application, it is only necessary to query the first mapping relationship and the second mapping relationship to determine the memory allocated to the application. This simplifies the memory application process during the running of the application, which is beneficial to improving the memory allocation efficiency, and thus is beneficial to improving the running smoothness of the application.
[0260] The embodiment of the present application also provides a terminal device and a server for allocating memory. The terminal device and the server for allocating memory provided in the embodiment of the present application will be introduced from the perspective of hardware instantiation.
[0261] See also Fig.17 , is a schematic diagram of the structure of a terminal device provided in an embodiment of the present application. For the sake of convenience, only the parts related to the embodiment of the present application are shown. For specific technical details not disclosed, please refer to the method part of the embodiment of the present application. The terminal can be any terminal device including a mobile phone, a tablet computer, a personal digital assistant (full name in English: Personal Digital Assistant, English abbreviation: PDA), a sales terminal (full name in English: Point of Sales, English abbreviation: POS), a car computer, etc., taking the terminal as a tablet computer as an example:
[0262] Fig.17 FIG. 1 is a block diagram showing a partial structure of a tablet computer related to a terminal provided in an embodiment of the present application. Fig.17 The tablet computer includes: a radio frequency (RF) circuit 1710, a memory 1720, an input unit 1730, a display unit 1740, a sensor 1750, an audio circuit 1760, a wireless fidelity (WiFi) module 1770, a processor 1780, and a power supply 1790. Those skilled in the art will appreciate that Fig.17 The tablet computer structure shown in the figure does not constitute a limitation on the tablet computer, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0263] The memory 1720 can be used to store software programs and modules. The processor 1780 executes various functional applications and data processing of the tablet computer by running the software programs and modules stored in the memory 1720. The memory 1720 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the tablet computer (such as audio data, a phone book, etc.), etc. In addition, the memory 1720 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0264] The processor 1780 is the control center of the tablet computer. It uses various interfaces and lines to connect various parts of the entire tablet computer. It executes various functions of the tablet computer and processes data by running or executing software programs and / or modules stored in the memory 1720 and calling data stored in the memory 1720. Optionally, the processor 1780 may include one or more processing units; preferably, the processor 1780 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 1780.
[0265] In the embodiment of the present application, the processor 1780 included in the terminal also has the following functions:
[0266] Upon receiving an application startup request, N memory pools, a first mapping relationship and a second mapping relationship are constructed; the memory pool includes at least one memory block; the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer;
[0267] After receiving the memory application request sent by the application, obtaining the memory application size of the application according to the memory application request, and determining the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship;
[0268] According to the first identifier and the second mapping relationship, a first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application.
[0269] Optionally, the processor 1780 is also used to execute the steps of any implementation method of the memory allocation method provided in the embodiments of the present application.
[0270] The embodiment of the present application also provides a server, Fig.181 is a schematic diagram of the structure of a server provided in an embodiment of the present application. The server 1800 may have relatively large differences due to different configurations or performances, and may include one or more central processing units (CPU) 1822 (for example, one or more processors) and memory 1832, and one or more storage media 1830 (for example, one or more mass storage devices) storing application programs 1842 or data 1844. Among them, the memory 1832 and the storage medium 1830 may be short-term storage or permanent storage. The program stored in the storage medium 1830 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the server. Furthermore, the central processing unit 1822 may be configured to communicate with the storage medium 1830 to execute a series of instruction operations in the storage medium 1830 on the server 1800.
[0271] 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 and output interfaces 1858, and / or one or more operating systems 1841, such as Windows ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
[0272] The steps performed by the server in the above embodiment can be based on the Fig.18 The server structure shown.
[0273] The CPU 1822 is used to execute the following steps:
[0274] Upon receiving an application startup request, N memory pools, a first mapping relationship and a second mapping relationship are constructed; the memory pool includes at least one memory block; the first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; the second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer;
[0275] After receiving the memory application request sent by the application, obtaining the memory application size of the application according to the memory application request, and determining the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship;
[0276] According to the first identifier and the second mapping relationship, a first memory address corresponding to the first identifier is obtained, and when it is determined that the first memory address is a valid address, the first memory address is sent to the application.
[0277] Optionally, CPU 1822 can also be used to execute the steps of any implementation method of the memory allocation method in the embodiments of the present application.
[0278] An embodiment of the present application also provides a computer-readable storage medium for storing a computer program, wherein the computer program is used to execute any one of the implementation methods of a memory allocation method described in the aforementioned embodiments.
[0279] An embodiment of the present application also provides a computer program product including instructions, which, when executed on a computer, enables the computer to execute any one of the implementations of a memory allocation method described in the aforementioned embodiments.
[0280] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0281] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0282] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0283] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0284] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (full name in English: Read-Only Memory, English abbreviation: ROM), random access memory (full name in English: Random Access Memory, English abbreviation: RAM), disk or optical disk and other media that can store computer programs.
[0285] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory allocation method, characterized in that: include: Upon receiving an application startup request, constructing N memory pools, a first mapping relationship, and a second mapping relationship; the memory pool includes at least one memory block; The first mapping relationship is used to record the mapping relationship between the application size interval and the memory pool identifier; The second mapping relationship is used to record the mapping relationship between the memory pool identifier and the memory address of the next memory block to be allocated in the memory pool; N is a positive integer; After receiving the memory application request sent by the application, obtaining the memory application size of the application according to the memory application request, and determining the first identifier of the first memory pool according to the memory application size of the application and the first mapping relationship; According to the first identifier and the second mapping relationship, acquiring a first memory address corresponding to the first identifier, and when determining that the first memory address is a valid address, sending the first memory address to the application; When the sizes of the memory blocks in the same memory pool are the same, the first mapping relationship is constructed in the following manner: Calculating the difference between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference set; the memory block size is used to characterize the size of the memory block in the memory pool; Determine the minimum value in the first difference value set as a first size value, determine the minimum value in the memory block sizes of the N memory pools as a second size value, and determine the minimum value between the first size value and the second size value as a third size value; Determine the maximum value of the memory block sizes of the N memory pools as a fourth size value, and determine the ratio of the fourth size value to the third size value as a first number; the first number is used to represent the number of relations in the first mapping relationship; Subtract the memory block size of each of the memory pools from r times the third size value to obtain a second difference value set, and use 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-th application size interval is the interval (r-1 times the third size value, r times the third size value]; r is a positive integer, r≤the first number; A first mapping relationship is established according to each application size interval and its corresponding memory pool identifier.
2. The method according to claim 1, characterized in that: When the memory blocks in the memory pool are connected according to a single linked list and the sizes of the memory blocks in the same memory pool are the same, constructing N memory pools specifically includes: Use the system memory allocation function to obtain the memory address of the original memory area; Divide the memory addresses of the original memory area into memory pools to obtain memory addresses of N memory pools; Divide the memory address of the i-th memory pool into memory blocks according to the i-th size, and get the M of the i-th memory pool. i memory blocks, stores the memory address of the j+1th memory block of the ith memory pool to the jth memory block of the ith memory pool, and stores the preset invalid address to the Mth memory block of the ith memory pool. i memory blocks; j is a positive integer, j≤M i -1;M i is a positive integer; i is a positive integer, i≤N.
3. The method according to claim 1, characterized in that The determining, according to the memory application size of the application and the first mapping relationship, a first identifier of the first memory pool includes: Determine, according to the memory application size of the application and the first mapping relationship, a target application size interval to which the memory application size of the application belongs; A first identifier of a first memory pool is determined according to the target application size interval and the first mapping relationship.
4. The method according to claim 1, characterized in that: The method further comprises: When determining that the first memory address is a valid address, determining a first memory block according to the first memory address; Reading a second memory address stored in the first memory block; The second mapping relationship is updated using the first identifier and the second memory address, so that the second mapping relationship includes the corresponding relationship between the first identifier and the second memory address.
5. The method according to claim 1, characterized in that The method further comprises: Upon receiving an application startup request, constructing a third mapping relationship; Determining an enabling state of the first memory pool according to the first identifier and the third mapping relationship; The acquiring, according to the first identifier and the second mapping relationship, a first memory address corresponding to the first identifier includes: When it is determined that the enabling state of the first memory pool is a use-allowed state, a first memory address corresponding to the first identifier is acquired according to the first identifier and the second mapping relationship.
6. The method according to claim 1, characterized in that The method further comprises: When receiving an application startup request, constructing a fourth mapping relationship; the fourth mapping relationship is used to record a mapping relationship between a memory pool identifier and a memory address of the memory pool; After receiving the memory release request sent by the application, determining the memory address of the memory area to be released according to the memory release request, and judging whether the memory area to be released belongs to the original memory area 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 includes the N memory pools; When it is determined that the memory area to be released belongs to the original memory area, a second identifier of the second memory pool is determined according to the memory address of the memory area to be released and the fourth mapping relationship, and a third memory address corresponding to the second identifier is acquired according to the second identifier and the second mapping relationship; The third memory address is stored in the memory area to be released, and the second mapping relationship is updated using the second identifier and the memory address of the memory area to be released, so that the second mapping relationship includes the correspondence between the second identifier and the memory address of the memory area to be released.
7. A memory allocation device, characterized in that: include: A construction unit, configured to construct N memory pools, a first mapping relationship and a second mapping relationship upon receiving an application startup request; the 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; The second mapping relationship is used to record the mapping relationship between the memory pool identifier 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, configured to, after receiving a memory application request sent by the application, obtain a memory application size of the application 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 and the first mapping relationship; an allocation unit, 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 when determining that the first memory address is a valid address; The building block is specifically used for: When the sizes of the memory blocks in the same memory pool are the same, calculating the difference between the memory block sizes of any two memory pools in the N memory pools to obtain a first difference set; the memory block size is used to characterize the size of the memory blocks in the memory pool; Determine the minimum value in the first difference value set as a first size value, determine the minimum value in the memory block sizes of the N memory pools as a second size value, and determine the minimum value between the first size value and the second size value as a third size value; Determine the maximum value of the memory block sizes of the N memory pools as a fourth size value, and determine the ratio of the fourth size value to the third size value as a first number; the first number is used to represent the number of relations in the first mapping relationship; Subtract the memory block size of each of the memory pools from r times the third size value to obtain a second difference value set, and use 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-th application size interval is the interval (r-1 times the third size value, r times the third size value]; r is a positive integer, r≤the first number; A first mapping relationship is established according to each application size interval and its corresponding memory pool identifier.
8. The device according to claim 7, characterized in that The building block is specifically used for: When the memory blocks in the memory pool are connected according to a single linked list and the sizes of the memory blocks in the same memory pool are the same, a system memory request function is used to obtain a memory address of the original memory area; Divide the memory addresses of the original memory area into memory pools to obtain memory addresses of N memory pools; Divide the memory address of the i-th memory pool into memory blocks according to the i-th size, and get the M of the i-th memory pool. i memory blocks, stores the memory address of the j+1th memory block of the ith memory pool to the jth memory block of the ith memory pool, and stores the preset invalid address to the Mth memory block of the ith memory pool. i memory blocks; j is a positive integer, j≤M i -1;M i is a positive integer; i is a positive integer, i≤N.
9. The device according to claim 7, characterized in that The first processing unit is specifically configured to: Determine, according to the memory application size of the application and the first mapping relationship, a target application size interval to which the memory application size of the application belongs; A first identifier of a first memory pool is determined according to the target application size interval and the first mapping relationship.
10. The device according to claim 7, characterized in that The device also includes: A first update unit is used to determine a first memory block according to the first memory address when determining that the first memory address is a valid address; read a second memory address stored in the first memory block; and use the first identifier and the second memory address to update the second mapping relationship so that the second mapping relationship includes a corresponding relationship between the first identifier and the second memory address.
11. The device according to claim 7, characterized in that The device further comprises: a determination unit; The construction unit is further configured to construct a third mapping relationship when receiving an application startup request; The determining unit is configured to determine an enabling state of the first memory pool according to the first identifier and the third mapping relationship; The allocation unit is specifically configured to obtain a first memory address corresponding to the first identifier according to the first identifier and the second mapping relationship when it is determined that the enable state of the first memory pool is a use-allowed state.
12. The device according to claim 7, characterized in that The device further comprises: a second processing unit and a releasing unit; The construction unit is further used to construct a fourth mapping relationship when receiving an application startup request; the fourth mapping relationship is used to record a mapping relationship between a memory pool identifier and a memory address of the memory pool; The second processing unit is configured to, after receiving the memory release request sent by the application, determine the memory address of the memory area to be released according to the memory release request, and determine whether the memory area to be released belongs to the original memory area 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 includes the N memory pools; The release unit is used to determine the second identifier of the second memory pool according to the memory address of the memory area to be released and the fourth mapping relationship when it is determined that the memory area to be released belongs to the original memory area, and obtain the third memory address corresponding to the second identifier according to the second identifier and the second mapping relationship; store the third memory address in the memory area to be released, and update 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 includes the correspondence between the second identifier and the memory address of the memory area to be released.
13. A device, characterized in that The device comprises a processor and a memory: The memory is used to store computer programs; The processor is configured to execute the method according to any one of claims 1 to 6 according to the computer program.
14. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store a computer program, and the computer program is used to execute the method according to any one of claims 1 to 6.
15. A computer program product comprising instructions, characterized in that When the method is executed on a computer, the computer is enabled to execute the method according to any one of claims 1 to 6.
