Memory allocation method, device, equipment, storage medium and computer program product
By reserving static memory blocks for applications in the operating system and combining them with dynamic allocation, the problem of dynamic memory allocation failure is solved, and predictable and controllable memory allocation is achieved, ensuring the normal operation of applications and business requirements.
Patent Information
- Application Number
- CN202211532281.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-01
- Publication Date
- 2026-01-16
- Estimated Expiration
- 2042-12-01
AI Technical Summary
In operating systems, the catastrophic impact of dynamic memory allocation failures, especially when there is insufficient remaining free memory, makes it impossible to meet deterministic business requirements.
By pre-dividing multiple reserved memory blocks based on the static memory requirements of multiple applications, and combining this with dynamic memory allocation, predictable and controllable memory allocation can be achieved.
It avoids the catastrophic consequences of dynamic memory allocation failure, ensures the normal operation of applications, and meets the high real-time and high-performance requirements of multiple services.
Smart Images

Figure CN115756868B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a memory allocation method and device, equipment, storage medium and computer program product. BACKGROUND
[0002] In the process of running an application program by an operating system, memory needs to be allocated for the application program, and then the executable file of the application program is loaded into the allocated memory, so as to run the application program.
[0003] In the related art, the operating system can dynamically allocate memory for the application program from the current remaining free memory in the process of running the application program, so as to improve the running performance of the device. However, in the case that the current remaining free memory is insufficient, the dynamic memory allocation method inevitably causes the problem of memory allocation failure, which has a disastrous adverse effect on the business with high determinacy requirement. SUMMARY
[0004] The present application provides a memory allocation method, device, equipment, storage medium and computer program product, which can realize predictable and controllable memory allocation. The technical solution is as follows:
[0005] In one aspect, a memory allocation method is provided, which comprises:
[0006] receiving a dynamic memory application request sent by a target application program, the dynamic memory application request carrying an identifier of the target application program, a size of a first memory block to be applied by the target application program and a number of the first memory blocks;
[0007] allocating memory blocks for the target application program from a plurality of reserved memory blocks based on the identifier of the target application program, the size of the first memory block and the number of the first memory blocks, the plurality of reserved memory blocks being memory blocks divided based on static memory requirements of a plurality of application programs, the static memory requirements including the size and number of deterministic memory blocks required by the corresponding application programs.
[0008] Optionally, the allocating memory blocks for the target application program from the plurality of reserved memory blocks based on the identifier of the target application program, the size of the first memory block and the number of the first memory blocks comprises:
[0009] determining the number of available memory blocks of the target application program based on the identifier of the target application program and the size of the first memory block;
[0010] In a case that the number of the first memory blocks is not greater than the number of the available memory blocks, memory blocks are allocated for the target application from the plurality of reserved memory blocks based on the size of the first memory blocks and the number of the first memory blocks.
[0011] Optionally, the determining the number of available memory blocks of the target application based on the identification of the target application and the size of the first memory blocks comprises:
[0012] acquiring a corresponding number of memory blocks as the number of available memory blocks from a correspondence between application identifications, memory block sizes and numbers of memory blocks based on the identification of the target application and the size of the first memory blocks.
[0013] After the allocating the memory blocks for the target application from the plurality of reserved memory blocks based on the size of the first memory blocks and the number of the first memory blocks, the method further comprises:
[0014] updating the correspondence between application identifications, memory block sizes and numbers of memory blocks based on the identification of the target application, the size of the first memory blocks and the number of the first memory blocks.
[0015] Optionally, before the receiving the dynamic memory application request sent by the target application, the method further comprises:
[0016] determining the size and the total number of each deterministic memory block required by the plurality of applications based on the static memory requirements of the plurality of applications.
[0017] dividing corresponding memory blocks from the remaining free memory based on the size and the total number of each deterministic memory block to obtain the plurality of reserved memory blocks.
[0018] Optionally, after the dividing corresponding memory blocks from the remaining free memory based on the size and the total number of each deterministic memory block, the method further comprises:
[0019] creating a correspondence between application identifications, memory block sizes and numbers of memory blocks based on the static memory requirements of the plurality of applications.
[0020] Optionally, the method further comprises:
[0021] receiving a dynamic memory release request sent by the target application, the dynamic memory release request carrying the identification of the target application, the size of a second memory block to be released by the target application and the number of the second memory block.
[0022] based on the identification of the target application program, the size of the second memory block and the number of the second memory blocks, releasing the memory block allocated for the target application program, and taking the released memory block as a reserved memory block.
[0023] In another aspect, a memory allocation apparatus is provided, the apparatus comprising:
[0024] a first receiving module configured to receive a dynamic memory application request sent by a target application program, the dynamic memory application request carrying an identification of the target application program, a size of a first memory block to be applied for by the target application program and a number of the first memory blocks;
[0025] an allocating module configured to allocate, based on the identification of the target application program, the size of the first memory block and the number of the first memory blocks, a memory block for the target application program from a plurality of reserved memory blocks, the plurality of reserved memory blocks being memory blocks divided based on static memory demands of a plurality of application programs, the static memory demands including sizes and numbers of deterministic memory blocks required by the corresponding application programs.
[0026] Optionally, the allocating module comprises:
[0027] a determining unit configured to determine, based on the identification of the target application program and the size of the first memory block, a number of available memory blocks of the target application program;
[0028] an allocating unit configured to, in a case where the number of the first memory blocks is not greater than the number of the available memory blocks, allocate, based on the size of the first memory block and the number of the first memory blocks, a memory block for the target application program from the plurality of reserved memory blocks.
[0029] Optionally, the determining unit is specifically configured to:
[0030] obtain, based on the identification of the target application program and the size of the first memory block, a corresponding number of memory blocks from a correspondence between application program identifications, memory block sizes and numbers of memory blocks, as the number of the available memory blocks;
[0031] the allocating module further comprises:
[0032] an updating unit configured to update, based on the identification of the target application program, the size of the first memory block and the number of the first memory blocks, the correspondence between the application program identifications, the memory block sizes and the numbers of memory blocks.
[0033] Optionally, the apparatus further comprises:
[0034] determining, based on the static memory requirements of the plurality of application programs, a size and a total number of each of the deterministic memory blocks required by the plurality of application programs;
[0035] allocating, based on the size and the total number of each of the deterministic memory blocks, a corresponding memory block from the remaining free memory to obtain the plurality of reserved memory blocks.
[0036] Optionally, the apparatus further comprises:
[0037] creating, based on the static memory requirements of the plurality of application programs, a correspondence between an application program identifier, a memory block size and a memory block number.
[0038] Optionally, the apparatus further comprises:
[0039] receiving a dynamic memory release request sent by the target application program, the dynamic memory release request carrying an identifier of the target application program, a size of a second memory block to be released by the target application program and a number of the second memory block;
[0040] releasing, based on the identifier of the target application program, the size of the second memory block and the number of the second memory block, the memory block allocated for the target application program, and taking the released memory block as a reserved memory block.
[0041] In another aspect, a computer device is provided, which comprises a memory and a processor, the memory is configured to store a computer program, and the processor is configured to execute the computer program stored in the memory to implement the steps of the memory allocation method.
[0042] In another aspect, a computer readable storage medium is provided, which stores a computer program, and the computer program is executed by a processor to implement the steps of the memory allocation method.
[0043] In another aspect, a computer program product comprising instructions which, when executed on a computer, cause the computer to carry out the steps of the memory allocation method.
[0044] The technical solutions provided in the present application can bring at least the following beneficial effects:
[0045] Since the plurality of reserved memory blocks are memory blocks divided based on static memory requirements of the plurality of application programs, that is, before the plurality of application programs run, the static memory allocation is used to reserve memory blocks that can meet the necessary memory blocks for subsequent normal running for each application program. In this way, in the process of running the target application program, the dynamic memory allocation is used to allocate memory blocks for the target application program in real time from the plurality of reserved memory blocks, so as to avoid the disastrous consequences caused by the failure of dynamic memory allocation in the process of running the application program. That is, the embodiment of the present application provides a new memory allocation mechanism, which realizes predictable and controllable memory allocation through the synergistic effect between static memory allocation and dynamic memory allocation. BRIEF DESCRIPTION OF DRAWINGS
[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0047] Figure 1 is a schematic diagram of an architecture of a computer device provided by the embodiment of the present application;
[0048] Figure 2 is a flowchart of a memory allocation method provided by the embodiment of the present application;
[0049] Figure 3 is a schematic diagram of a computer device startup process provided by the embodiment of the present application;
[0050] Figure 4 is a schematic diagram of a memory allocation device provided by the embodiment of the present application;
[0051] Figure 5 is a schematic diagram of a computer device provided by the embodiment of the present application. DETAILED DESCRIPTION
[0052] In order to make the purpose, technical solutions and advantages of the embodiments of the present application more clear, the embodiments of the present application will be further described in detail with reference to the drawings.
[0053] Before the memory allocation method provided by the embodiment of the present application is explained in detail, the application scenario and system architecture provided by the embodiment of the present application are introduced.
[0054] First, the application scenario of the embodiment of the present application is introduced.
[0055] The memory allocation method provided in the embodiments of the present application can be applied to various scenarios, for example, in the fields of servers, cloud computing, etc., memory is usually dynamically allocated for an application program from the current remaining free memory. However, in the case where the current remaining free memory is insufficient, the problem of memory allocation failure is prone to occur. At this time, the memory allocation method provided in the embodiments of the present application can be used to allocate a memory block for the application program.
[0056] For example, in the fields of aerospace, intelligent driving, vehicle OS (Operating System) and OSEK OS, etc., which have very high requirements on service accuracy, in order to avoid the problem of memory allocation failure due to dynamic memory allocation, thereby causing disastrous adverse effects, memory is usually statically allocated for an application program in the compilation process of the application program, thereby providing deterministic memory for the application program. However, for the same application program, different service types may require different memory, so the static memory allocation method cannot meet the multi-service requirements of high real-time and high performance, that is, it cannot support more service requirements on limited resources. At this time, the memory allocation method provided in the embodiments of the present application can be used to allocate a memory block for the application program.
[0057] Secondly, the system architecture of the embodiments of the present application is introduced.
[0058] Please refer to Figure 1 , Figure 1 is a schematic diagram of the architecture of a computer device according to an exemplary embodiment. The computer device includes a plurality of application programs 101( Figure 1 The application program is a target application program) and a memory allocator 102. The target application program 101 can interact with the memory allocator 102, and in the process of running the target application program 101 by the computer device through an operating system, the memory allocator 102 allocates memory for the target application program 101.
[0059] The target application program 101 is configured to send a dynamic memory application request to the memory allocator 102, and the dynamic memory application request carries an identifier of the target application program 101, a size of a first memory block to be applied by the target application program 101, and a number of the first memory block. After the memory allocator 102 receives the dynamic memory application request sent by the target application program 101, the memory allocator 102 allocates a memory block for the target application program 101 from a plurality of reserved memory blocks based on the identifier of the target application program 101, the size of the first memory block, and the number of the first memory block.
[0060] The plurality of reserved memory blocks are memory blocks divided by the memory allocator 102 based on static memory requirements of the plurality of applications, wherein the static memory requirements include sizes and quantities of deterministic memory blocks required by the respective applications. That is, the memory allocator 102 determines sizes and total quantities of each of the deterministic memory blocks required by the plurality of applications based on the static memory requirements of the plurality of applications. Then, based on the sizes and total quantities of each of the deterministic memory blocks, corresponding memory blocks are divided from the remaining free memory to obtain the plurality of reserved memory blocks.
[0061] The target application 101 can be any computer program, such as text entry, video playback, etc. The memory allocator 102 is a hardware device or a software program, such as the Dlmalloc memory allocation software, etc.
[0062] The computer device can be any electronic product that can interact with a user through one or more ways, such as a keyboard, a touchpad, a touch screen, a remote control, voice interaction, or a handwriting device, etc. For example, a PC (Personal Computer), a mobile phone, a smart phone, a PDA (Personal Digital Assistant), a wearable device, a PPC (Pocket PC), a tablet computer, a smart car machine, a smart television, a smart speaker, etc.
[0063] Those skilled in the art should understand that the above-mentioned target application 101, memory allocator 102, and computer device are only examples, and other existing or future application programs, memory allocators, or computer devices that can be applicable to the embodiments of the present application should also be included in the protection scope of the embodiments of the present application, and are hereby included by reference.
[0064] It should be noted that the application scenarios and system architectures described in the embodiments of the present application are for more clearly explaining the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided by the embodiments of the present application. Those skilled in the art can know that, as the system architecture evolves and new application scenarios appear, the technical solutions provided by the embodiments of the present application are also applicable to similar technical problems.
[0065] Next, the memory allocation method provided by the embodiments of the present application will be explained in detail.
[0066] Figure 2 is a flowchart of a memory allocation method provided by the embodiments of the present application, please refer to Figure 2 The method includes the following steps.
[0067] Step 201: receiving a dynamic memory application request sent by a target application, the dynamic memory application request carrying an identifier of the target application, a size of a first memory block to be applied for by the target application, and a quantity of the first memory blocks.
[0068] The memory allocator is provided with a dynamic memory application interface. During the running of the target application, the target application can send a dynamic memory application request to the memory allocator by invoking the dynamic memory application interface. Of course, the target application can also send a dynamic memory application request to the memory allocator in other manners, which is not limited in the embodiments of the present application.
[0069] In some embodiments, before receiving the dynamic memory application request sent by the target application, the size and the total quantity of each deterministic memory block in the deterministic memory blocks required by the plurality of applications are determined based on the static memory requirements of the plurality of applications. Based on the size and the total quantity of each deterministic memory block, the corresponding memory blocks are divided from the remaining free memory to obtain the plurality of reserved memory blocks.
[0070] The application programs have corresponding configuration files, and the configuration files store the static memory requirements of the application programs. Therefore, the memory allocator can sequentially scan the configuration files corresponding to the plurality of application programs to obtain the static memory requirements of the plurality of application programs. Of course, obtaining the static memory requirements of the application programs by scanning the configuration files is only an example. In actual applications, the static memory requirements of the application programs can also be obtained in other manners. For example, the static memory requirements of the plurality of application programs are stored in a specified location, and the memory allocator can obtain the static memory requirements of the plurality of application programs by accessing the specified location.
[0071] It should be noted that the static memory requirements of the plurality of application programs are determined by an administrator / program developer according to a related algorithm. Taking the administrator as an example, before the administrator deploys the plurality of application programs on the computer device, the static memory requirements of the plurality of application programs are determined according to the related algorithm, and the static memory requirement of each application program is stored in the configuration file corresponding to the corresponding application program. Alternatively, the static memory requirements of the plurality of application programs are uniformly stored in a specified location.
[0072] Since the static memory requirement of each application program includes the size and the number of the deterministic memory blocks required by the corresponding application program, after the static memory requirement of the plurality of application programs is obtained, the size and the total number of each deterministic memory block required by the plurality of application programs can be counted. Then, according to the size and the total number of each deterministic memory block, the corresponding memory block is divided from the remaining free memory to obtain the plurality of reserved memory blocks. The deterministic memory block refers to the memory block necessary to guarantee the normal operation of the application program.
[0073] For example, the plurality of application programs includes application program A and application program B. The static memory requirement of application program A is shown in Table 1, and the static memory requirement of application program B is shown in Table 2. As can be seen from Table 1, the deterministic memory blocks required by application program A include 8 deterministic memory blocks with a size of 16K (Kilo) bytes and 4 deterministic memory blocks with a size of 2M (Mega) bytes. As can be seen from Table 2, the deterministic memory blocks required by application program B include 4 deterministic memory blocks with a size of 16K bytes and 2 deterministic memory blocks with a size of 1M bytes.
[0074] By counting the size and the number of the deterministic memory blocks required by application program A and application program B, it can be determined that the total number of the deterministic memory blocks with a size of 16K bytes required by application program A and application program B is 12, the total number of the deterministic memory blocks with a size of 2M bytes is 4, and the total number of the deterministic memory blocks with a size of 1M bytes is 2. At this time, the plurality of reserved memory blocks divided from the remaining free memory includes 12 reserved memory blocks with a size of 16K bytes, 4 reserved memory blocks with a size of 2M bytes, and 2 reserved memory blocks with a size of 1M bytes.
[0075] Table 1
[0076] Static memory requirement of application A program_A_conf mem1_size = 16 * 1024 mem1_num = 8 mem2_size = 2 * 1024 * 1024 mem2_num = 4
[0077] Table 2
[0078]
[0079]
[0080] After the plurality of reserved memory blocks are divided, a correspondence between application identifiers, memory block sizes, and memory block quantities can be created based on the static memory requirements of the plurality of applications. That is, for any application in the plurality of applications, a correspondence between an identifier of the application, a size of each deterministic memory block required by the application, and a quantity of each deterministic memory block required by the application is created. In this way, the correspondence can record the applications that have usage rights for the plurality of reserved memory blocks, and the size and quantity of reserved memory blocks that each application can use.
[0081] For example, assume that the plurality of applications includes application A and application B. After the plurality of reserved memory blocks are divided, a correspondence between application identifiers, memory block sizes, and memory block quantities can be created as shown in Table 3 below.
[0082] Table 3
[0083] Application identifier Memory block size Memory block number A 16 Kbytes 8 A 2 Mbytes 4 B 16 Kbytes 4 B 1 Mbytes 2
[0084] The application identifier is used to uniquely identify the application, and can be a number, name, and functional description of the application, or a combination of these.
[0085] In some embodiments, after the memory allocator divides the plurality of reserved memory blocks from the remaining free memory, the plurality of reserved memory blocks can be provided with corresponding tag information. That is, each reserved memory block in the plurality of reserved memory blocks has tag information, and the tag information is used to indicate that the corresponding reserved memory block is not released.
[0086] Since the plurality of reserved memory blocks are memory blocks divided based on the static memory requirements of the plurality of applications, that is, the plurality of reserved memory blocks are all in an "idle" state. However, in general, after a memory block changes from an "in use" state to an "idle" state, the memory allocator will release the "idle" memory block so that the memory allocator can continue to allocate the "idle" memory block to other applications. Therefore, in order to avoid the memory allocator releasing the plurality of reserved memory blocks, the plurality of reserved memory blocks can be provided with corresponding tag information, thereby providing a protection mechanism for the plurality of reserved memory blocks. Of course, in actual applications, the plurality of reserved memory blocks can also be prevented from being released by other means, which are not limited by the embodiments of the present application.
[0087] The above is to determine the size and total number of each deterministic memory block required by the plurality of applications, and then directly divide the corresponding memory block from the remaining free memory. However, in actual application, the size of the remaining free memory can be less than the product of the size and total number of the deterministic memory blocks required by the plurality of applications, that is, the remaining free memory cannot meet the static memory requirements of the plurality of applications. At this time, a prompt message can be displayed to prompt the administrator that the static memory requirements of the plurality of applications are unreasonable, and to prompt the administrator to stop starting the computer device.
[0088] That is, as shown in Figure 3 , Figure 3 is a schematic diagram of a computer device startup process provided by an embodiment of the present application. Before the computer device starts, the memory allocator deployed on the computer device obtains the static memory requirements of the plurality of applications, and compares the static memory requirements of the plurality of applications with the remaining free memory. In the case where the remaining free memory can meet the static memory requirements of the plurality of applications, the plurality of reserved memory blocks are divided from the remaining free memory, and the computer device is started. In the case where the remaining free memory cannot meet the static memory requirements of the plurality of applications, the computer device is stopped. That is, before the plurality of applications run, the static memory allocation is used to reserve the memory block that can meet the subsequent normal running of each application at one time, so as to avoid the disastrous consequences caused by the failure of dynamic memory allocation during the running of each application. In other words, when the computer device starts, the deterministic memory block is reserved for each application according to the static memory requirements of each application, and the current business type does not need to be considered. Even if a certain application does not need to run when the computer device starts, the memory block needs to be reserved for the application when the computer device starts.
[0089] In the case where the remaining free memory cannot meet the static memory requirements of the plurality of applications, the administrator can also adjust the static memory requirements of each application in the plurality of applications in real time through the communication interface by using OTA (Over The Air Technology) in combination with the prompt message.
[0090] It should be noted that, in the case that the remaining free memory cannot meet the static memory requirements of the plurality of application programs, directly stopping the starting of the computer device is only an example. In other embodiments, since the plurality of application programs include necessary application programs and non-essential application programs, in the case that the remaining free memory cannot meet the static memory requirements of the plurality of application programs, the corresponding memory blocks can be divided from the remaining free memory based on the size and total number of the deterministic memory blocks required by the necessary application programs, so as to guarantee the normal operation of the necessary application programs.
[0091] Based on the above description, each application program has a corresponding configuration file in which the tag information of the corresponding application program can be stored, and the tag information indicates whether the application program is a necessary application program or a non-essential application program. Of course, in actual application, the application program can also be determined to be a necessary application program or a non-essential application program by other ways, and the embodiments of the present application do not limit this.
[0092] Among them, the necessary application program refers to the application program necessary to guarantee the normal operation of the computer device. For example, in the field of intelligent driving, the vehicle braking program is a necessary application program, and the music player is a non-essential application program.
[0093] Step 202: Based on the identification of the target application program, the size of the first memory block and the number of the first memory block, allocate memory blocks for the target application program from the plurality of reserved memory blocks.
[0094] In some embodiments, based on the identification of the target application program and the size of the first memory block, the number of available memory blocks of the target application program is determined. In the case that the number of the first memory block is not greater than the number of available memory blocks, based on the size of the first memory block and the number of the first memory block, allocate memory blocks for the target application program from the plurality of reserved memory blocks.
[0095] Among them, the implementation process of determining the number of available memory blocks of the target application program based on the identification of the target application program and the size of the first memory block includes: based on the identification of the target application program and the size of the first memory block, obtaining the corresponding number of memory blocks from the correspondence between the application program identification, the memory block size and the number of memory blocks as the number of available memory blocks. That is, the identification of the target application program and the size of the first memory block are obtained from the correspondence between the application program identification, the memory block size and the number of memory blocks, and the obtained number of memory blocks is determined as the number of available memory blocks.
[0096] Since the correspondence between the application identifier, the memory block size and the memory block quantity is used to record the application program with the use right of the reserved memory block, and the size and the quantity of the reserved memory block that each application program can use, when the target application program applies for the first memory block, the available memory block quantity of the target application program can be determined through the correspondence. In this way, the size and the quantity of the memory block that the target application program can apply for can be limited, and the target application program can be prevented from applying for the memory block corresponding to other application programs without authorization.
[0097] In a case where the quantity of the first memory block is not greater than the available memory block quantity, it is indicated that the currently available memory block of the target application program can satisfy the currently applied memory block of the target application program. At this time, the target application program can be allocated the memory block with the size of the size of the first memory block and the quantity of the quantity of the first memory block from the plurality of reserved memory blocks based on the size of the first memory block and the quantity of the first memory block. That is, in the process of running the target application program, the memory block is allocated to the target application program in a dynamic memory allocation manner in real time.
[0098] For example, taking the target application program as the above-mentioned application program A, the size of the first memory block to be applied for by the application program A is 2M bytes, and the quantity of the first memory block is 4. At this time, based on the identifier of the application program A and the size of the first memory block, it is determined from the correspondence between the application identifier, the memory block size and the memory block quantity that the application program A corresponds to 4 memory blocks with the size of 2M bytes. That is, the available memory block quantity of the application program A is 4. Since the quantity of the first memory block to be applied for by the application program A is not greater than the available memory block quantity, the application program A can be allocated 4 memory blocks with the size of 2M bytes from the plurality of reserved memory blocks.
[0099] In a case where the quantity of the first memory block is greater than the available memory block quantity, it is indicated that the currently available memory block of the target application program cannot satisfy the currently applied memory block of the target application program. At this time, the memory allocator sends a prompt message to the target application program, and the prompt message is used to prompt that the currently applied memory block of the target application program is unreasonable.
[0100] Based on the above description, after the plurality of reserved memory blocks are divided from the remaining free memory, each of the plurality of reserved memory blocks is in an "idle" state. However, before the target application program applies for a memory block, there can be other application programs that have already applied for memory blocks, at this time, there can be memory blocks in the "idle" state in the plurality of reserved memory blocks, and there can also be memory blocks in the "in use" state. Therefore, the memory block can be allocated to the target application program from the memory blocks in the "idle" state in the plurality of reserved memory blocks. Moreover, after the memory block is allocated to the target application program from the plurality of reserved memory blocks, the state of the memory block allocated to the target application program in the plurality of reserved memory blocks can be changed from the "idle" state to the "in use" state.
[0101] It should be noted that the memory allocator can represent the state of the reserved memory block as "idle" or "in use" through various implementation manners. For example, the plurality of reserved memory blocks are divided into a first memory group and a second memory group, the state of each memory block in the first memory group is "idle", and the state of each memory block in the second memory group is "in use". That is, based on the group to which each memory block belongs, the state of each memory block is determined. For another example, each memory block corresponds to a signal bit, and the state of the memory block is represented by different values of the signal bit. For example, when the value of the signal bit corresponding to a certain memory block is 1, it is determined that the memory block is in the "in use" state, and when the value of the signal bit corresponding to the memory block is 0, it is determined that the memory block is in the "idle" state.
[0102] In some embodiments, after the memory block is allocated to the target application program from the plurality of reserved memory blocks based on the size of the first memory block and the number of the first memory block, the correspondence between the application program identifier, the memory block size and the memory block quantity can be updated based on the identifier of the target application program, the size of the first memory block and the number of the first memory block. That is, the number of memory blocks corresponding to the identifier of the target application program and the size of the first memory block in the correspondence between the application program identifier, the memory block size and the memory block quantity is subtracted by the number of the first memory block to obtain the updated correspondence between the application program identifier, the memory block size and the memory block quantity.
[0103] Optionally, the memory allocator can also release the memory blocks allocated to the target application into the plurality of reserved memory blocks after the target application ends running. That is, receiving a dynamic memory release request sent by the target application, the dynamic memory release request carrying an identification of the target application, a size of second memory blocks to be released by the target application, and a number of the second memory blocks. Based on the identification of the target application, the size of the second memory blocks, and the number of the second memory blocks, releasing the memory blocks allocated to the target application, and taking the released memory blocks as reserved memory blocks.
[0104] In some embodiments, based on the identification of the target application, the size of the second memory blocks, and the number of the second memory blocks, the memory blocks allocated to the target application can be released, the size of which is the size of the second memory blocks and the number of which is the number of the second memory blocks, and the state of the released memory blocks can be changed from the "in use" state to the "idle" state, so as to take the released memory blocks as reserved memory blocks.
[0105] After releasing the memory blocks allocated to the target application and taking the released memory blocks as reserved memory blocks, the correspondence between the application identification, the memory block size, and the memory block number can also be updated based on the identification of the target application, the size of the second memory blocks, and the number of the second memory blocks. That is, in the correspondence between the application identification, the memory block size, and the memory block number, the number of the memory blocks corresponding to the identification of the target application and the size of the second memory blocks can be added by the number of the second memory blocks to obtain the updated correspondence between the application identification, the memory block size, and the memory block number.
[0106] In the embodiments of the present application, the plurality of reserved memory blocks are memory blocks divided based on the static memory requirements of the plurality of applications, that is, before the plurality of applications run, the static memory allocation is used to reserve memory blocks for each application that can meet the necessary memory blocks for subsequent normal running. In this way, in the process of running the target application, the dynamic memory allocation is used to allocate memory blocks to the target application in real time from the plurality of reserved memory blocks, so as to avoid the disastrous consequences caused by the failure of dynamic memory allocation during the running of the application. That is, the embodiments of the present application provide a new memory allocation mechanism, which realizes predictable and controllable memory allocation through the synergistic effect between static memory allocation and dynamic memory allocation.
[0107] In addition, the embodiment of the present application acquires the static memory requirements of the plurality of application programs before deploying the plurality of application programs on the computer device. In this way, in the case that the remaining free memory cannot meet the static memory requirements of the plurality of application programs, the administrator can also adjust the static memory requirements of each of the plurality of application programs in real time. Meanwhile, since a certain memory block is not bound to a certain application program, each memory block in the "free" state can be called by a qualified application program, thereby facilitating the additional resource management and memory block calling of the memory allocator.
[0108] Figure 4 is a structural schematic diagram of a memory allocation apparatus provided by the embodiment of the present application. The memory allocation apparatus can be realized by software, hardware or a combination of both as part of or all of a memory allocator. Please refer to Figure 4 The apparatus comprises a first receiving module 401 and an allocation module 402.
[0109] The first receiving module 401 is configured to receive a dynamic memory application request sent by a target application program, wherein the dynamic memory application request carries an identifier of the target application program, a size of a first memory block to be applied for by the target application program and a number of the first memory blocks. For details, please refer to the corresponding content in the above embodiments, which will not be described herein again.
[0110] The allocation module 402 is configured to allocate memory blocks for the target application program from a plurality of reserved memory blocks based on the identifier of the target application program, the size of the first memory block and the number of the first memory blocks, wherein the plurality of reserved memory blocks are memory blocks divided based on static memory requirements of a plurality of application programs, and the static memory requirements comprise sizes and numbers of deterministic memory blocks required by the corresponding application programs. For details, please refer to the corresponding content in the above embodiments, which will not be described herein again.
[0111] Optionally, the allocation module 402 comprises:
[0112] A determination unit configured to determine a number of available memory blocks of the target application program based on the identifier of the target application program and the size of the first memory block.
[0113] An allocation unit configured to allocate memory blocks for the target application program from the plurality of reserved memory blocks based on the size of the first memory block and the number of the first memory blocks in the case that the number of the first memory blocks is not greater than the number of the available memory blocks.
[0114] Optionally, the determination unit is specifically configured to:
[0115] obtain a corresponding number of memory blocks as the number of the available memory blocks from a correspondence between the identifier of the application program, the size of the memory block and the number of the memory block based on the identifier of the target application program and the size of the first memory block.
[0116] The allocation module 402 further comprises:
[0117] an updating unit, configured to update a correspondence between application program identifiers, memory block sizes and memory block quantities based on an identifier of a target application program, a size of a first memory block and a quantity of the first memory block.
[0118] Optionally, the apparatus further comprises:
[0119] a determination module, configured to determine a size and a total quantity of each deterministic memory block required by the plurality of application programs based on static memory requirements of the plurality of application programs;
[0120] a division module, configured to divide a corresponding memory block from remaining free memory based on the size and the total quantity of each deterministic memory block, to obtain the plurality of reserved memory blocks.
[0121] Optionally, the apparatus further comprises:
[0122] a creation module, configured to create a correspondence between application program identifiers, memory block sizes and memory block quantities based on static memory requirements of the plurality of application programs.
[0123] Optionally, the apparatus further comprises:
[0124] a second receiving module, configured to receive a dynamic memory release request sent by a target application program, the dynamic memory release request carrying an identifier of the target application program, a size of a second memory block to be released by the target application program and a quantity of the second memory block;
[0125] a release module, configured to release memory blocks allocated for the target application program based on the identifier of the target application program, the size of the second memory block and the quantity of the second memory block, and to take the released memory blocks as reserved memory blocks.
[0126] In the embodiments of the present application, the plurality of reserved memory blocks are memory blocks divided based on static memory requirements of the plurality of application programs, that is, before the plurality of application programs run, the static memory allocation is used to reserve memory blocks necessary for subsequent normal running for each application program. In this way, in the process of running of a target application program, the dynamic memory allocation is used to allocate memory blocks for the target application program in real time from the plurality of reserved memory blocks, so that disastrous consequences caused by failure of dynamic memory allocation in the process of running of an application program can be avoided. That is, the embodiments of the present application provide a new memory allocation mechanism, which realizes predictable and controllable allocation of memory through the synergistic effect between static memory allocation and dynamic memory allocation.
[0127] In addition, the embodiments of the present application acquire the static memory requirements of the plurality of application programs before deploying the plurality of application programs on the computer device. In this way, in the case that the remaining free memory cannot meet the static memory requirements of the plurality of application programs, the administrator can also adjust the static memory requirements of each of the plurality of application programs in real time. Meanwhile, since a certain memory block is not bound to a certain application program, each memory block in the "free" state can be called by a qualified application program, thereby facilitating the additional resource management and memory block calling of the memory allocator.
[0128] It should be noted that the memory allocation apparatus provided by the above embodiments is only used for example to illustrate the division of the above functional modules during memory allocation. In actual applications, the above functions can be completed by different functional modules according to requirements, that is, the internal structure of the apparatus is divided into different functional modules to complete all or part of the above described functions. In addition, the memory allocation apparatus and the memory allocation method provided by the above embodiments belong to the same concept, and the specific implementation process is described in the method embodiments, which will not be repeated here.
[0129] Figure 5 is a structural block diagram of a computer device 500 provided by an embodiment of the present application. The computer device 500 can be a portable mobile terminal, such as a smart phone, a tablet computer, an MP3 player (Moving Picture Experts Group Audio Layer III), an MP4 player (Moving Picture Experts Group Audio Layer IV), a notebook computer or a desktop computer. The computer device 500 can also be referred to as a user device, a portable terminal, a laptop terminal, a desktop terminal or other names.
[0130] Generally, the computer device 500 includes a processor 501 and a memory 502.
[0131] The processor 501 can include one or more processing cores, such as a 4-core processor, an 8-core processor, and the like. The processor 501 can be implemented in at least one of a hardware form of a DSP (Digital Signal Processing), an FPGA (Field-Programmable Gate Array), a PLA (Programmable Logic Array). The processor 501 can also include a main processor and a coprocessor. The main processor is a processor for processing data in an awake state, also known as a CPU (Central Processing Unit). The coprocessor is a low-power processor for processing data in a standby state. In some embodiments, the processor 501 can be integrated with a GPU (Graphics Processing Unit) that is responsible for rendering and drawing content required to be displayed on the display screen. In some embodiments, the processor 501 can further include an AI (Artificial Intelligence) processor for processing machine learning related computing operations.
[0132] The memory 502 can include one or more computer-readable storage media that can be non-transitory. The memory 502 can also include a high-speed random access memory, and a nonvolatile memory such as one or more disk storage devices, flash storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 502 is used to store at least one instruction for being executed by the processor 501 to implement the memory allocation method provided by the method embodiments in the present application.
[0133] In some embodiments, the computer device 500 can also optionally include a peripheral device interface 503 and at least one peripheral device. The processor 501, the memory 502, and the peripheral device interface 503 can be connected through a bus or a signal line. Each peripheral device can be connected to the peripheral device interface 503 through a bus, a signal line, or a circuit board. Specifically, the peripheral device includes at least one of a radio frequency circuit 504, a touch display screen 505, a camera 506, an audio circuit 507, a positioning component 508, and a power supply 509.
[0134] The peripheral interface 503 can be used to connect at least one I / O (Input / Output) related peripheral device to the processor 501 and the memory 502. In some embodiments, the processor 501, the memory 502 and the peripheral interface 503 are integrated on the same chip or circuit board; in some other embodiments, any one or two of the processor 501, the memory 502 and the peripheral interface 503 can be implemented on a separate chip or circuit board, and the present embodiments are not limited in this regard.
[0135] The radio frequency circuit 504 is used to receive and transmit RF (Radio Frequency) signals, also known as electromagnetic signals. The radio frequency circuit 504 communicates with a communication network and other communication devices through electromagnetic signals. The radio frequency circuit 504 converts electrical signals into electromagnetic signals for transmission, or converts received electromagnetic signals into electrical signals. Optionally, the radio frequency circuit 504 includes an antenna system, an RF transceiver, one or more amplifiers, a tuner, an oscillator, a digital signal processor, a codec chipset, a subscriber identity module card, and the like. The radio frequency circuit 504 can communicate with other computer devices through at least one wireless communication protocol. The wireless communication protocol includes but is not limited to: the World Wide Web, a metropolitan area network, an intranet, various generations of mobile communication networks (2G, 3G, 4G and 5G), a wireless local area network and / or a WiFi (Wireless Fidelity) network. In some embodiments, the radio frequency circuit 504 can also include NFC (Near Field Communication) related circuitry, and the present embodiments are not limited in this regard.
[0136] The display screen 505 is configured to display a UI (User Interface). The UI can include graphics, text, icons, video, and any combination thereof. When the display screen 505 is a touch display screen, the display screen 505 is further configured to capture touch signals on or above the surface of the display screen 505. The touch signals can be input to the processor 501 as control signals for processing. In this case, the display screen 505 can also be configured to provide virtual buttons and / or virtual keyboard, also known as soft buttons and / or soft keyboard. In some embodiments, the display screen 505 can be one, arranged on the front panel of the computer device 500; in other embodiments, the display screen 505 can be at least two, arranged on different surfaces of the computer device 500 or in a folding design; in yet other embodiments, the display screen 505 can be a flexible display screen, arranged on a curved surface or a folding surface of the computer device 500. Even, the display screen 505 can also be arranged in an irregular shape, i.e., a special-shaped screen. The display screen 505 can be made of LCD (Liquid Crystal Display), OLED (Organic Light-Emitting Diode), etc.
[0137] The camera assembly 506 is configured to capture images or videos. Optionally, the camera assembly 506 includes a front-facing camera and a rear-facing camera. Typically, the front-facing camera is arranged on the front panel of the computer device, and the rear-facing camera is arranged on the back of the computer device. In some embodiments, the rear-facing camera is at least two, which are any one of a main camera, a depth-of-field camera, a wide-angle camera, and a telephoto camera, to realize the background blur function by fusing the main camera and the depth-of-field camera, the panoramic shooting and VR (Virtual Reality) shooting function by fusing the main camera and the wide-angle camera, or other fusion shooting functions. In some embodiments, the camera assembly 506 can further include a flash. The flash can be a single-color-temperature flash or a dual-color-temperature flash. The dual-color-temperature flash refers to the combination of a warm light flash and a cold light flash, which can be used for light compensation under different color temperatures.
[0138] The audio circuit 507 can include a microphone and a speaker. The microphone is used to collect sound waves of a user and an environment, and convert the sound waves into an electrical signal input to the processor 501 for processing, or input to the radio frequency circuit 504 to realize voice communication. The microphone can be multiple for the purpose of stereo sound collection or noise reduction, and arranged at different parts of the computer device 500. The microphone can also be an array microphone or an omnidirectional collection microphone. The speaker is used to convert an electrical signal from the processor 501 or the radio frequency circuit 504 into sound waves. The speaker can be a conventional diaphragm speaker, or a piezoelectric ceramic speaker. When the speaker is a piezoelectric ceramic speaker, it can not only convert an electrical signal into a sound wave audible to humans, but also convert an electrical signal into an inaudible sound wave to humans for ranging purposes. In some embodiments, the audio circuit 507 can also include a headphone jack.
[0139] The positioning component 508 is used to position the current geographic location of the computer device 500 to realize navigation or LBS (Location Based Service). The positioning component 508 can be a positioning component based on the GPS (Global Positioning System) of the United States, the Beidou system of China, or the Galileo system of Russia.
[0140] The power supply 509 is used to supply power to various components in the computer device 500. The power supply 509 can be alternating current, direct current, disposable battery or rechargeable battery. When the power supply 509 includes a rechargeable battery, the rechargeable battery can be a wired charging battery or a wireless charging battery. The wired charging battery is a battery charged through a wired line, and the wireless charging battery is a battery charged through a wireless coil. The rechargeable battery can also be used to support fast charging technology.
[0141] Those skilled in the art can understand that the structure shown in the above embodiments does not constitute a limitation on the computer device 500, and can include more or fewer components than shown, or combine certain components, or use different component arrangements. Figure 5 Those skilled in the art can understand that the structure shown in the above embodiments does not constitute a limitation on the computer device 500, and can include more or fewer components than shown, or combine certain components, or use different component arrangements.
[0142] In some embodiments, a computer readable storage medium is also provided, and the computer readable storage medium stores a computer program which, when executed by a processor, implements the steps of the memory allocation method in the above embodiments. For example, the computer readable storage medium can be a ROM, a RAM, a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0143] It is worth noting that the computer readable storage medium mentioned in the embodiments of the present application can be a non-volatile storage medium, in other words, a non-transitory storage medium.
[0144] It should be understood that all or part of the steps of the above-mentioned embodiments can be realized by software, hardware, firmware or any combination thereof. When realized by software, it can be realized in the form of a computer program product in whole or in part. The computer program product includes one or more computer instructions. The computer instructions can be stored in the computer readable storage medium described above.
[0145] That is, in some embodiments, a computer program product including instructions which, when run on a computer, cause the computer to perform the steps of the memory allocation method described above is also provided.
[0146] It should be understood that "at least one" referred to herein means one or more, and "multiple" means two or more. In the description of the embodiments of the present application, unless otherwise specified, " / " represents the meaning of or, for example, A / B can represent A or B; "and / or" herein is only a description of the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, in order to clearly describe the technical solutions of the embodiments of the present application, in the embodiments of the present application, the same items or similar items with basically the same functions and roles are distinguished by using "first", "second" and the like. The skilled in the art can understand that "first", "second" and the like do not limit the quantity and execution order, and "first", "second" and the like do not necessarily mean different.
[0147] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data for analysis, stored data, displayed data, etc.) and signals involved in the embodiments of the present application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions. For example, the dynamic memory allocation request of the target application program and the static memory demand of the plurality of application programs in the embodiments of the present application are obtained under sufficient authorization.
[0148] The above describes the embodiments provided by the present application, and does not limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A memory allocation method, characterized by, The method comprises: receiving a dynamic memory application request sent by a target application during running, the dynamic memory application request carrying an identification of the target application, a size of a first memory block to be applied for by the target application, and a number of the first memory blocks; based on the identification of the target application and the size of the first memory block, obtaining a corresponding memory block number as an available memory block number of the target application from a correspondence between application identifications, memory block sizes, and memory block numbers; in a case where the number of the first memory blocks is not greater than the available memory block number, allocating memory blocks for the target application from a plurality of reserved memory blocks based on the size of the first memory block and the number of the first memory blocks; wherein the plurality of reserved memory blocks are memory blocks divided based on static memory demands of a plurality of applications before running of the plurality of applications, and the static memory demands include sizes and numbers of deterministic memory blocks required by corresponding applications.
2. The method of claim 1, wherein, After the allocating of the memory blocks for the target application from the plurality of reserved memory blocks based on the size of the first memory block and the number of the first memory blocks, the method further comprises: updating the correspondence between the application identifications, the memory block sizes, and the memory block numbers based on the identification of the target application, the size of the first memory block, and the number of the first memory blocks.
3. The method of claim 1 or 2, wherein, Before the receiving of the dynamic memory application request sent by the target application during running, the method further comprises: based on the static memory demands of the plurality of applications, determining a size and a total number of each deterministic memory block in deterministic memory blocks required by the plurality of applications; based on the size and the total number of each deterministic memory block, dividing a corresponding memory block from remaining free memory to obtain the plurality of reserved memory blocks.
4. The method of claim 3, wherein, After the dividing of the corresponding memory block from the remaining free memory based on the size and the total number of each deterministic memory block, the method further comprises: based on the static memory demands of the plurality of applications, creating the correspondence between the application identifications, the memory block sizes, and the memory block numbers.
5. The method of claim 1, wherein, The method further comprises: receiving a dynamic memory release request sent by the target application, the dynamic memory release request carrying an identification of the target application, a size of a second memory block to be released by the target application, and a number of the second memory blocks; based on the identification of the target application, the size of the second memory block, and the number of the second memory blocks, releasing the memory blocks allocated for the target application, and taking the released memory blocks as reserved memory blocks.
6. A memory allocation apparatus, characterized by comprising: The apparatus comprises: a first receiving module configured to receive a dynamic memory application request sent by a target application during running, the dynamic memory application request carrying an identification of the target application, a size of a first memory block to be applied for by the target application, and a number of the first memory blocks; The allocation module is configured to: obtain, based on the identification of the target application program and the size of the first memory block, a corresponding number of memory blocks as a number of available memory blocks of the target application program from a correspondence between application program identifications, memory block sizes, and numbers of memory blocks; and allocate memory blocks for the target application program from a plurality of reserved memory blocks based on the size of the first memory block and the number of the first memory blocks, in a case where the number of the first memory blocks is not greater than the number of the available memory blocks, wherein the plurality of reserved memory blocks are memory blocks divided based on static memory requirements of a plurality of application programs before the plurality of application programs are run, and the static memory requirements include sizes and numbers of deterministic memory blocks required by the corresponding application programs.
7. A computer device, comprising: The computer device comprises a memory and a processor, the memory is used to store a computer program, and the processor is used to execute the computer program stored on the memory to realize the steps of the method of any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The storage medium stores a computer program, and the computer program is executed by the processor to realize the steps of the method of any one of claims 1-5.
9. A computer program product comprising a computer program, characterized in that, The computer program comprises instructions, and when the instructions run on the computer, the computer executes the steps of the method of any one of claims 1-5.