Memory allocation method, device, electronic device and storage medium
By converting the application's memory allocation request into a file mapping method, additional memory space is obtained, which solves the problem that the application cannot be used normally due to memory overflow and achieves stable operation of the application.
Patent Information
- Application Number
- CN202010558389.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-18
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-06-18
AI Technical Summary
When an application occupies memory space that exceeds the operating system's allowed threshold, it causes memory overflow (OOM), causing the application to be unable to be used normally.
By creating a target file and mapping it to the process virtual address space, the file-mapped memory allocation interface is used to obtain additional memory space to avoid counting the file-mapped memory space into the application's memory footprint.
It effectively avoids the problem of forced application shutdown due to memory overflow, ensuring that the application can run normally and reducing the occurrence rate of OOM.
Smart Images

Figure CN111736997B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a memory allocation method, device, electronic device and storage medium. Background Art
[0002] With the development of computer technology, there are more and more applications, and the functions they implement are also increasing. The application is generally run in the memory of the electronic device. Since the memory space of the electronic device is limited, the size of the memory space that can be allocated to each application is certain. When the memory space occupied by the application exceeds the threshold of the memory space that can be used by a single application allowed by the electronic device operating system, an out of memory (OOM) situation occurs, and the electronic device operating system will forcibly close the application, or reduce the memory usage of the functional operation that occupies a large memory space, resulting in the application being unable to be used normally. Summary of the invention
[0003] The present disclosure provides a memory allocation method to at least solve the problem in the related art that when an application needs to occupy a large amount of memory space, the application cannot be used normally. The technical solution of the present disclosure is as follows:
[0004] According to a first aspect of an embodiment of the present disclosure, a memory allocation method is provided, comprising:
[0005] In response to a memory allocation request triggered by an application, creating a target file corresponding to the application;
[0006] Obtaining a target virtual address mapped by the target file in the process virtual address space;
[0007] Allocate the memory space corresponding to the target virtual address to the application.
[0008] In one embodiment, before obtaining the target virtual address of the target file mapped in the process virtual address space, the method further includes:
[0009] Get the file handle and file size of the target file;
[0010] Calling a file mapping memory allocation interface, the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the first address of the target virtual space, and return it to the caller as the target virtual address.
[0011] In one embodiment, the step of creating a target file corresponding to the application in response to a memory allocation request triggered by the application includes:
[0012] Determine the size of the memory space to be allocated according to the memory allocation request;
[0013] According to the size of the memory space to be allocated, a target file corresponding to the application is created.
[0014] In one embodiment, the step of creating a target file corresponding to the application in response to a memory allocation request triggered by the application includes:
[0015] In response to a memory allocation request triggered by an application, calling a standard memory allocation interface, wherein the standard memory allocation interface is used to allocate an initial virtual address for the application in the process virtual address space;
[0016] Obtaining an amount of memory space allocated to the application program through the standard memory allocation interface;
[0017] When the memory allocation amount is equal to the standard memory allocation space threshold, obtaining the remaining amount of memory space to be allocated;
[0018] A target file corresponding to the application is created according to the remaining amount of the memory space.
[0019] In one embodiment, the method further comprises:
[0020] Obtaining the memory space allocation amount corresponding to the target virtual address;
[0021] When the memory allocation amount corresponding to the target virtual address is equal to the file memory mapping space threshold, the remaining amount of the memory space to be allocated is obtained;
[0022] Calling a standard memory allocation interface, and determining a virtual address corresponding to the remaining amount of the memory space through the standard memory allocation interface;
[0023] The virtual address corresponding to the remaining amount of the memory space is allocated to the application.
[0024] In one embodiment, the method further comprises:
[0025] Get the priority of the standard memory allocation interface and the priority of the file mapping memory allocation interface respectively;
[0026] If the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is called first;
[0027] If the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is preferentially called.
[0028] In one embodiment, the method further comprises:
[0029] Calling a preset memory allocation interface; the preset memory allocation interface includes a standard memory allocation interface and a file mapping memory allocation interface;
[0030] After the preset memory allocation interface is called, the first interface is preferentially called to allocate memory space for the application; when the memory allocation amount allocated to the application by the first interface reaches its corresponding space threshold, the second interface is called to allocate the remaining amount of memory space to be allocated to the application;
[0031] The first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
[0032] According to a second aspect of an embodiment of the present disclosure, a memory allocation device is provided, including:
[0033] a request response unit, configured to execute a memory allocation request in response to the memory allocation request triggered by the application program, and create a target file corresponding to the application program;
[0034] An address acquisition unit is configured to execute acquisition of a target virtual address mapped by the target file in a process virtual address space;
[0035] The memory allocation unit is configured to allocate the memory space corresponding to the target virtual address to the application.
[0036] In one embodiment, the address acquisition unit includes:
[0037] A file parameter acquisition module, configured to execute acquisition of the file handle and file size of the target file;
[0038] The file memory mapping module is configured to execute a call to a file mapping memory allocation interface, wherein the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the first address of the target virtual space, and return it to the caller as the target virtual address.
[0039] In one embodiment, the request response unit includes:
[0040] A module for determining memory to be allocated, configured to determine the size of the memory space to be allocated according to the memory allocation request;
[0041] The target file creation module is configured to execute creation of a target file corresponding to the application program according to the size of the memory space to be allocated.
[0042] In one embodiment, the request response unit is configured to perform: in response to a memory allocation request triggered by an application, calling a standard memory allocation interface, wherein the standard memory allocation interface is used to allocate an initial virtual space for the application in the process virtual address space; obtaining the memory space allocation amount allocated to the application through the standard memory allocation interface; when the memory allocation amount corresponding to the initial virtual address is equal to the standard memory allocation space threshold, obtaining the remaining amount of memory space to be allocated; and creating a target file corresponding to the application based on the remaining amount of memory space.
[0043] In one embodiment, the apparatus further comprises:
[0044] A memory allocation amount determination unit is configured to execute acquisition of a memory space allocation amount corresponding to the target virtual address;
[0045] a memory margin determining unit configured to obtain a remaining amount of memory space to be allocated when the memory allocation amount corresponding to the target virtual address is equal to a file memory mapping space threshold;
[0046] The request response unit is further configured to execute a call to a standard memory allocation interface, and determine a virtual address corresponding to the remaining amount of the memory space through the standard memory allocation interface;
[0047] The memory allocation unit is further configured to allocate the virtual address corresponding to the remaining amount of the memory space to the application.
[0048] In one embodiment, the apparatus further comprises:
[0049] A priority acquisition unit, configured to execute and respectively acquire the priority of a standard memory allocation interface and the priority of a file mapping memory allocation interface;
[0050] The request response unit is also configured to execute: if the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is called first; if the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is called first.
[0051] In one embodiment, the apparatus further comprises:
[0052] The request response unit is further configured to execute and call a preset memory allocation interface; the preset memory allocation interface includes a standard memory allocation interface and a file mapping memory allocation interface;
[0053] After the preset memory allocation interface is called, the first interface is preferentially called to allocate memory space for the application; when the memory allocation amount allocated to the application by the first interface reaches its corresponding space threshold, the second interface is called to allocate the remaining amount of memory space to be allocated to the application;
[0054] The first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
[0055] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the above-mentioned memory allocation method.
[0056] According to a fourth aspect of an embodiment of the present disclosure, a storage medium is provided. When instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the above-mentioned memory allocation method.
[0057] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, the program product comprising a computer program, the computer program being stored in a readable storage medium, at least one processor of a device reading and executing the computer program from the readable storage medium, so that the device executes the memory allocation method described in any one of the embodiments of the first aspect.
[0058] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects: in response to a memory allocation request triggered by an application, a target file corresponding to the application is created, the target virtual address mapped by the target file in the process virtual address space is obtained, and the memory space corresponding to the target virtual address is allocated to the application, thereby converting the application's request for memory allocation into obtaining memory through file mapping, so that the application obtains additional memory quota, while in the operating system, especially the IOS operating system, the memory space occupied by the file mapping is not included in the calculation of the memory space occupied by the application, and the additional memory quota obtained by the application through file mapping is not included in the memory occupancy of the application, thereby avoiding the problem that the application cannot be used normally when it needs to occupy a large amount of memory space.
[0059] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0061] Figure 1 The present invention is an application environment diagram of a memory allocation method according to an exemplary embodiment.
[0062] Figure 2 The figure is a flowchart of a memory allocation method according to an exemplary embodiment.
[0063] Figure 3 The figure is a flowchart of a memory allocation method according to an exemplary embodiment.
[0064] Figure 4 The invention is a block diagram showing a memory allocation device according to an exemplary embodiment.
[0065] Figure 5 The figure is a diagram showing the internal structure of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0066] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0067] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure 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 interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0068] The memory allocation method provided by the present disclosure can be applied to Figure 1 In the application environment shown in FIG. 1 , the memory allocation method can be applied to an electronic device 102. The computer device can be a server or a terminal. In an exemplary embodiment, Figure 1As shown, taking the computer device as a terminal as an example, the user can operate the application installed in the terminal through the interface. When the terminal operates the application, it triggers the application to send a memory allocation request to the operating system of the terminal. After receiving the memory allocation request triggered by the application, the operating system of the terminal responds to the memory allocation request triggered by the application, creates a target file corresponding to the application; obtains the target virtual address mapped by the target file in the process virtual address space; and then allocates the memory space corresponding to the target virtual address to the application, thereby allocating memory to the application and allowing the application to run normally.
[0069] In an exemplary embodiment, still taking the computer device as a terminal as an example, the user can operate the application installed in the terminal 102 through the interface. When the terminal operates the application, it triggers the application to send a memory allocation request to the application layer component of the terminal, wherein the application layer component refers to a component that encapsulates the memory allocation service in the application layer, such as a memory manager. After receiving the memory allocation request triggered by the application, the application layer component of the terminal creates a target file corresponding to the application according to the memory allocation request, maps the target file to the process virtual address space, determines the target virtual address mapped by the target file, and finally determines the target virtual address as the memory address of the memory allocated by the application, thereby allocating memory to the application and allowing the application to run normally.
[0070] Figure 2 is a flowchart of a memory allocation method according to an exemplary embodiment. Figure 2 As shown, this method is applied to Figure 1 Taking the electronic device shown in the figure as an example, the memory allocation method includes the following steps:
[0071] In step S210, in response to a memory allocation request triggered by an application, a target file corresponding to the application is created.
[0072] Among them, the running of applications in electronic devices is generally carried out in memory, and a memory allocation request refers to a request sent to the operating system when the application is started or other operations are performed, which is used to request the operating system to allocate a certain amount of memory to run the application. Specifically, the electronic device can detect the user's triggering operation on the application, and after obtaining the triggering operation, the triggering application sends a corresponding memory allocation request to the operating system of the electronic device, and the operating system of the electronic device responds to the memory allocation request and creates a target file corresponding to the application.
[0073] For example, when an electronic device receives a user's operation to start an application, it can start the application and trigger the application to send a memory allocation request to the operating system, and the operating system receives the memory allocation request. The memory allocation request can carry the size of the memory requested to be allocated; for another example, after the application is started, when the electronic device receives a user's operation to view an image in the application, it needs to decode the image data so as to render and display the image to the user. At this time, the decoding process of the image data needs to allocate memory to store the decoded data corresponding to the image data. At this time, the electronic device triggers the application to send a memory allocation request to the operating system, and the operating system receives the memory allocation request and responds.
[0074] The target file is a file newly created by the operating system, and the file size of the target file can be consistent with the size of the memory space to be allocated, or can be larger or smaller than the size of the memory space to be allocated. Specifically, after receiving the memory allocation request, the operating system creates a target file corresponding to the application in the disk according to the memory allocation request.
[0075] In step S220, the target virtual address mapped by the target file in the process virtual address space is obtained.
[0076] After creating a target file corresponding to the application program in the disk, the operating system of the electronic device can map the target file to the process virtual address space and determine the target virtual address mapped to the target file. The process virtual address space has a mapping relationship with the kernel space provided by the operating system for the application program to use.
[0077] In an exemplary embodiment, after creating the target file, the operating system can map the target file to the process virtual address space by calling the file mapping memory allocation interface, so that the file disk address and a virtual address segment in the process virtual address space have a one-to-one correspondence, and obtain the target virtual space mapped with the target file in the process virtual address space. Among them, the file mapping memory allocation interface refers to the method of memory mapping file provided in the operating system, for example, it can be an mmap calling function; the file mapping memory allocation interface is used to obtain the target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the address of the target virtual space, and return it to the caller (i.e., the operating system) as the target virtual address. Thereafter, the operating system obtains the target virtual address returned by the file mapping memory allocation interface, and obtains the target virtual address mapped by the target file in the process virtual address space.
[0078] In step S230, the memory space corresponding to the target virtual address is allocated to the application.
[0079] Specifically, the operating system returns the target virtual address as the memory address of the allocated memory to the application, so that the application determines the memory address of the allocated memory as the storage address to store data information such as data obtained by running the application.
[0080] In an exemplary embodiment, the file mapping memory allocation interface is used to obtain the target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the first address of the target virtual space, and return it to the caller (i.e., the operating system of the electronic device) as the target virtual address. Correspondingly, in step S230, the first address information of the target virtual space is returned to the application. When the operating system determines the target virtual space mapped by the target file in the process virtual address space, it returns the first address information in the target virtual address corresponding to the target virtual space to the application, so that the application starts to store data and other data information obtained by the operation of the application from the memory address corresponding to the first address information.
[0081] In the above-mentioned memory allocation method, upon receiving a memory allocation request triggered by an application, a target file corresponding to the application is created according to the memory allocation request, the target virtual address mapped by the target file in the process virtual address space is obtained, and finally the target virtual address is determined as the memory address of the memory allocated by the application, thereby converting the application's memory allocation request into memory allocation obtained by file mapping memory, so that the application obtains additional memory quota, while in the operating system, especially the IOS operating system, the memory occupied by the file mapping memory is not included in the calculation of the application's memory usage, and the additional memory quota obtained by the application through file mapping is not included in the application's memory usage, thereby avoiding the application being forced to close due to OOM, while effectively maintaining the original performance of the application.
[0082] Taking the application program as the video editing software QuickShadow as an example, the above embodiment is further explained. Specifically, after the QuickShadow application program is started, when the advanced image editing function is triggered by the home page of the QuickShadow (application program), the implementation of the advanced image editing function, especially the loading of the image to be edited, will occupy a large amount of memory, which is likely to cause the application program to crash, or even worse, cause the application program to be forcibly closed by the operating system. In the disclosed memory allocation method of the above embodiment, when the advanced image editing function is triggered from the home page of the QuickShadow (application program), the QuickShadow (application program) triggers a memory allocation request and sends it to the operating system. After receiving the memory allocation request, the operating system creates a target file corresponding to the QuickShadow (application program) on the disk according to the memory allocation request, and maps the target file to the process virtual address space by calling the file mapping memory allocation interface, so that the file disk address and a virtual address segment in the process virtual address space have a one-to-one correspondence, and obtains the target virtual space mapped with the target file in the process virtual address space, and obtains the target virtual address corresponding to the target virtual space, and obtains the target virtual address mapped by the target file. The operating system returns the target virtual address as the memory address of the allocated memory to the Quick Shadow (application), so that the Quick Shadow (application) determines the memory address of the allocated memory as the storage address to store the decoded data of the image to be edited, realizes image decoding, and renders the decoded data of the image to be edited and displays it to the user. By changing the memory application from the operating system to obtaining memory through file mapping, the memory usage of the image can be excluded from the memory usage of the application, effectively reducing the OOM problem rate.
[0083] In an exemplary embodiment, before the above step S220, it also includes: obtaining the file handle and file size of the target file; calling the file mapping memory allocation interface, the file mapping memory allocation interface is used to obtain the target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the target virtual address of the target virtual space, that is, the first address of the target virtual space, as the target virtual address returned to the caller. Among them, the file handle is the only identification basis for the opened file, and the file size refers to the amount of disk space occupied by the file.
[0084] After creating the target file, the electronic device operating system can obtain the file handle and file size of the target file, and obtain the target virtual space with the same file size as the target file corresponding to the file handle in the process virtual address space by calling the file mapping memory allocation interface, and obtain the target virtual address of the target virtual space. Specifically, after creating the target file, the electronic device operating system can obtain the file handle and file size of the target file, and call the mmap call function by using the file handle and file size as function parameters of the mmap call function to map the target file into the process virtual address space, so that the file disk address of the target file and a section of virtual address in the process virtual address space are mapped one by one, and the target virtual space is obtained. Among them, when the mmap call function is successfully called, the virtual address of the mapped target virtual space, that is, the memory address, is returned; when the mmap call function fails, a null pointer is returned, and the memory allocation fails at this time.
[0085] Through this embodiment, for an application, after creating a target file corresponding to the application in response to a memory allocation request triggered by the application, the target virtual address mapped by the target file in the process virtual address space can be obtained by calling the file mapping memory allocation interface provided by the operating system. The system changes involved are relatively small, which is conducive to reducing implementation costs.
[0086] In an exemplary embodiment, in the above step S210, the step of creating a target file corresponding to the application includes: determining the size of the memory space to be allocated according to the memory allocation request; and creating the target file corresponding to the application according to the size of the memory space to be allocated.
[0087] The size of the memory space to be allocated refers to the size of the memory space requested by the application. After receiving the memory allocation request, the operating system obtains the size of the memory space to be allocated in the memory allocation request, and creates a target file on the disk that matches the size of the memory space to be allocated. For example, after receiving the memory allocation request, the operating system obtains the size of the memory space to be allocated in the memory allocation request, and creates a target file on the disk whose file size is consistent with the size of the memory space to be allocated based on the size of the memory space to be allocated; for another example, after receiving the memory allocation request, the operating system obtains the size of the memory space to be allocated in the memory allocation request, and creates a target file on the disk whose file size is slightly larger than the size of the memory space to be allocated based on the size of the memory space to be allocated; to avoid memory waste.
[0088] In an exemplary embodiment, in the above step S210, in response to the memory allocation request triggered by the application, the step of creating a target file corresponding to the application may specifically include: in response to the memory allocation request triggered by the application, calling a standard memory allocation interface, the standard memory allocation interface is used to obtain the initial virtual address allocated by the application in the process virtual address space; counting the memory space allocation amount corresponding to the initial virtual address, and obtaining the memory space allocation amount allocated to the application through the standard memory allocation interface; when the memory allocation amount is equal to the standard memory allocation space threshold, obtaining the remaining amount of memory space to be allocated; according to the remaining amount of memory space, creating a target file corresponding to the application, and then converting to obtain memory through file mapping, so that the application obtains additional memory quota. Accordingly, the operating system will combine the above initial virtual address obtained through the standard memory allocation interface and the target virtual address obtained through file mapping to determine the memory address of the memory allocated to the application.
[0089] In the present disclosure, the standard memory allocation interface refers to the standard C language memory allocation interface provided by the operating system, such as the maclloc (memory allocation, dynamic memory allocation) function, calloc (clear allocation, dynamic memory allocation and initialization) function, new function, etc. provided by the operating system. After receiving the memory allocation request triggered by the application, the operating system allocates the initial virtual space in the process virtual address space by calling the standard memory allocation interface, such as calling the maclloc function interface, and obtains the initial virtual address of the allocated initial virtual space.
[0090] The amount of memory space allocated corresponding to the initial virtual address refers to the size of the memory space allocated by the standard memory allocation method. The remaining amount of memory space to be allocated refers to the size of the memory space required after deducting the memory space allocated by the standard memory allocation method.
[0091] In the above embodiment, in response to a memory allocation request triggered by an application, memory space is first allocated to the application by means of standard memory allocation. When the amount of memory space allocated to the application by means of the standard memory allocation interface reaches the threshold of the standard memory allocation space, a target file corresponding to the application is created according to the remaining amount of memory space to be allocated, and memory space is then allocated to the application by means of file mapping. In this way, memory space is first allocated to the application by means of standard memory allocation, and even if all the required memory space cannot be allocated to the application by means of the standard memory allocation interface, OOM will not be caused and the application will not be closed. In addition, the initial virtual address of the memory space allocated by means of standard memory allocation can be continuous with the target virtual address of the memory space allocated to the application by means of file mapping memory, and the adaptability to the application is good. Finally, the operating system determines the memory address of the memory allocated to the application by means of the initial virtual address and the target virtual address obtained by different means.
[0092] In another exemplary embodiment, when responding to a memory allocation request triggered by an application, the operating system first creates a target file corresponding to the application; obtains the file handle and file size of the target file, calls the file mapping memory allocation interface to obtain the target virtual address mapped by the target file in the process virtual address space, and allocates the memory space corresponding to the target virtual address to the application; at the same time, it also obtains the memory space allocation amount corresponding to the target virtual address, and when the memory allocation amount corresponding to the target virtual address is equal to the file memory mapping space threshold, obtains the remaining amount of memory space to be allocated, calls the standard memory allocation interface, determines the virtual address corresponding to the remaining amount of memory space through the standard memory allocation interface, and allocates the virtual address corresponding to the remaining amount of memory space to the application.
[0093] The remaining amount of memory space to be allocated refers to the amount of memory space required after deducting the memory space allocated by file mapping memory. The standard memory allocation interface refers to the standard C language memory allocation interface provided by the operating system, such as the maclloc (memory allocation, dynamic memory allocation) function, calloc (clear allocation, dynamic memory allocation and initialization) function, new function, etc. provided by the operating system.
[0094] In this embodiment, in response to a memory allocation request triggered by an application, memory space is first allocated to the application by calling a file mapping memory allocation interface. Since the memory space that can be used by the file mapping memory method is also limited, the size of the memory space allocated by calling the file mapping memory allocation interface is counted in real time. When the amount of memory space allocated to the application by the file mapping memory allocation interface reaches the threshold of the file mapping memory allocation space, the memory space is allocated to the application by the standard memory allocation method according to the current memory space to be allocated. In this way, memory space is first allocated to the application by the file mapping method, and even if all the required memory space cannot be allocated to the application by the file mapping method, the application will not be closed; and the target virtual address of the memory space allocated to the application by the file mapping memory method is continuous with the initial virtual address of the memory space allocated by the standard memory allocation method, which has good adaptability to the application. Finally, the operating system determines the memory address of the memory allocated to the application by the virtual addresses obtained by different methods.
[0095] Furthermore, in one embodiment, the memory allocation method of the present invention also includes: respectively obtaining the priority of a standard memory allocation interface and the priority of a file mapping memory allocation interface; if the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is preferentially called; if the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is preferentially called.
[0096] Through this embodiment, when responding to a memory allocation request triggered by an application, the standard memory allocation interface and the file mapping memory allocation interface can be called in sequence according to their priority, with the interface with the current higher priority being called first, which can further avoid conflicts caused by a certain interface being called simultaneously by multiple application processes.
[0097] In an exemplary embodiment, based on the above embodiment of allocating memory space to the application program through two memory allocation interfaces in sequence, the memory allocation method of the present disclosure may further include:
[0098] In response to a memory allocation request triggered by an application, a preset memory allocation interface is called; the preset memory allocation interface is an interface pre-configured for the application, including a standard memory allocation interface and a file mapping memory allocation interface. After the preset memory allocation interface is called, the first interface is preferentially called to allocate memory space for the application, and when the memory allocation amount allocated to the application by the first interface reaches the corresponding space threshold, the second interface is called to allocate the remaining amount of memory space to be allocated to the application; wherein the first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
[0099] In this embodiment, a preset memory allocation interface is set for the application, a unified allocation interface is provided for the application to use, and the memory that can be obtained by the standard memory allocation interface and the file mapping memory allocation interface is assembled into a memory pool. For the application, by calling the memory allocation interface based on the memory pool, on the one hand, a larger allocatable memory space is provided for the application, and on the other hand, the application does not need to know the specific status of the current standard memory allocation interface and the file mapping memory allocation interface, and the memory allocation interface based on the memory pool decides which memory allocation interface to use first to allocate memory space for the current application, which reduces the information processing amount of the caller and is conducive to interface expansion or upgrade.
[0100] Furthermore, in the present disclosure, when the memory space allocated by two types of memory allocation interfaces in the memory pool-based memory allocation interface to the application exceeds their respective allocatable thresholds, the memory pool-based memory allocation interface can return a null pointer to the caller, indicating that the response to the memory allocation request triggered by the application has failed.
[0101] In one embodiment, Figure 3 As shown, the memory allocation method includes the following steps:
[0102] In step S301, a memory allocation request triggered by an application is received.
[0103] In step S302, a target file corresponding to the application is created according to the memory allocation request.
[0104] In step S303, the file handle and file size of the target file are obtained.
[0105] In step S304, a file mapping memory allocation interface is called, and the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain a target virtual address of the target virtual space.
[0106] In step S305, the memory space allocation amount corresponding to the target virtual address is counted.
[0107] In step S306, when the memory allocation amount is equal to the file memory mapping space threshold, the remaining amount of memory space to be allocated is obtained.
[0108] In step S307, a standard memory allocation interface is called, where the standard memory allocation interface is used to determine a virtual address corresponding to the remaining amount of memory space based on the target virtual address.
[0109] In step S308, the target virtual address and the virtual address corresponding to the remaining amount of memory space are determined as the memory address of the memory allocated to the application.
[0110] In step S309, the first address information of the memory address is determined, and the first address information is returned to the application.
[0111] Taking the application program as the video editing software Quick Shadow as an example, the above embodiment is further explained. Specifically, after the Quick Shadow application program is started, when the advanced image editing function is triggered by the homepage of the Quick Shadow (application program), the Quick Shadow (application program) triggers a memory allocation request and sends it to the operating system. After receiving the memory allocation request, the operating system does not directly call standard memory allocation interfaces such as malloc, calloc, and new, but creates a new target file corresponding to the Quick Shadow (application program) on the disk according to the memory allocation request, and obtains the file handle and file size of the target file. By using the file handle and file size as parameters of the file mapping memory allocation interface such as mmap, the target file is mapped into the process virtual address space, so that the file disk address and a virtual address segment in the process virtual address space have a one-to-one mapping relationship, and the target virtual space mapped with the target file in the process virtual address space is obtained. By obtaining the target virtual address corresponding to the target virtual space, the target virtual address mapped by the target file is obtained. At the same time, the operating system counts the memory space allocation amount corresponding to the target virtual address in real time, that is, the size of the memory space allocated by calling the file mapping memory allocation interface is obtained in real time. When the memory allocation amount is equal to the file memory mapping space threshold, the operating system cannot continue to obtain memory space by calling the file mapping memory allocation interface. At this time, the operating system can allocate memory by calling the standard memory allocation interface. Based on the target virtual address, it continues to allocate memory to obtain a memory space whose content space size is consistent with the remaining amount of memory space, and determines the virtual address of the memory space. Finally, the operating system determines the target virtual address and the virtual address of the memory space whose content space size is consistent with the remaining amount of memory space as the memory address of the memory allocated by the quick shadow (application), so that the quick shadow (application) determines the memory address of the allocated memory as the storage address to store the decoded data of the image to be edited, realizes image decoding, and renders the decoded data of the image to be edited and displays it to the user. By changing the memory application to the operating system to obtain memory through file mapping, the memory occupation of the image can be excluded from the memory occupation of the application, effectively reducing the OOM problem incidence.
[0112] It should be understood that although Figure 2 as well as Figure 3 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, these steps are not strictly limited in order and can be executed in other orders. Figure 2 as well as Figure 3At least part of the steps may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the steps or stages in other steps.
[0113] Figure 4 is a block diagram of a memory allocation device according to an exemplary embodiment. Figure 4 , the device comprises:
[0114] The request response unit 410 is configured to execute a memory allocation request in response to the memory allocation request triggered by the application program and create a target file corresponding to the application program;
[0115] The address acquisition unit 420 is configured to execute acquisition of the target virtual address mapped by the target file in the process virtual address space;
[0116] The memory allocation unit 430 is configured to allocate the memory space corresponding to the target virtual address to the application.
[0117] In an exemplary embodiment, the address obtaining unit 420 includes:
[0118] A file parameter acquisition module is configured to execute acquisition of a file handle and a file size of a target file;
[0119] And, the file memory mapping module is configured to execute a call to a file mapping memory allocation interface, wherein the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, and obtain the first address of the target virtual space, and return it to the caller as the target virtual address.
[0120] In an exemplary embodiment, the request response unit 410 includes:
[0121] A module for determining memory to be allocated, configured to determine the size of memory space to be allocated according to a memory allocation request;
[0122] And, the target file creation module is configured to create a target file corresponding to the application according to the size of the memory space to be allocated.
[0123] In one embodiment, the above-mentioned request response unit 410 is specifically configured to execute: in response to a memory allocation request triggered by an application, calling a standard memory allocation interface, wherein the standard memory allocation interface is used to allocate an initial virtual space for the application in the process virtual address space; obtaining the memory space allocation amount allocated to the application through the standard memory allocation interface; when the memory allocation amount corresponding to the initial virtual address is equal to the standard memory allocation space threshold, obtaining the remaining amount of memory space to be allocated; and creating a target file corresponding to the application according to the remaining amount of memory space.
[0124] Accordingly, the memory allocation unit 430 is specifically configured to determine the target virtual address and the initial virtual address as the memory address of the memory allocated to the application.
[0125] In an exemplary embodiment, the memory allocation device further includes:
[0126] A memory allocation amount determination unit is configured to execute acquisition of a memory space allocation amount corresponding to a target virtual address;
[0127] A memory margin determination unit is configured to obtain a remaining amount of memory space to be allocated when the memory allocation amount corresponding to the target virtual address is equal to the file memory mapping space threshold;
[0128] The request response unit 410 is further configured to execute calling a standard memory allocation interface, and determine a virtual address corresponding to the remaining amount of memory space based on the target virtual address through the standard memory allocation interface;
[0129] The memory allocation unit 430 is further configured to allocate the target virtual address and the virtual address corresponding to the remaining amount of memory space to the application.
[0130] In an exemplary embodiment, the memory allocation device further includes:
[0131] A priority acquisition unit, configured to execute and respectively acquire the priority of a standard memory allocation interface and the priority of a file mapping memory allocation interface;
[0132] The request response unit 410 is also configured to execute: if the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is called first; if the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is called first.
[0133] In an exemplary embodiment, the request response unit 410 is further configured to execute calling a preset memory allocation interface; the preset memory allocation interface includes a standard memory allocation interface and a file mapping memory allocation interface;
[0134] After the preset memory allocation interface is called, the first interface is called first to allocate memory space for the application. When the memory allocation amount allocated to the application by the first interface reaches its corresponding space threshold, the second interface is called to allocate the remaining memory space to be allocated to the application. The first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
[0135] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0136] Figure 5 1 is a block diagram of a device 500 for memory allocation according to an exemplary embodiment. For example, the device 500 may be a mobile phone, a computer, a digital broadcasting device, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
[0137] Reference Figure 5 , device 500 may include one or more of the following components: a processing component 502 , a memory 504 , a power component 506 , a multimedia component 508 , an audio component 510 , an input / output (I / O) interface 512 , a sensor component 514 , and a communication component 516 .
[0138] The processing component 502 generally controls the overall operation of the device 500, such as operations associated with display, phone calls, data communications, camera operations, and recording operations. The processing component 502 may include one or more processors 520 to execute instructions to complete all or part of the steps of the above-mentioned method. In addition, the processing component 502 may include one or more modules to facilitate the interaction between the processing component 502 and other components. For example, the processing component 502 may include a multimedia module to facilitate the interaction between the multimedia component 508 and the processing component 502.
[0139] The memory 504 is configured to store various types of data to support operations on the device 500. Examples of such data include instructions for any application or method operating on the device 500, contact data, phone book data, messages, pictures, videos, etc. The memory 504 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.
[0140] Power supply component 506 provides power to various components of device 500. Power supply component 506 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to device 500.
[0141] The multimedia component 508 includes a screen that provides an output interface between the device 500 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundaries of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 508 includes a front camera and / or a rear camera. When the device 500 is in an operating mode, such as a shooting mode or a video mode, the front camera and / or the rear camera may receive external multimedia data. Each front camera and the rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
[0142] The audio component 510 is configured to output and / or input audio signals. For example, the audio component 510 includes a microphone (MIC), and when the device 500 is in an operating mode, such as a call mode, a recording mode, and a speech recognition mode, the microphone is configured to receive an external audio signal. The received audio signal can be further stored in the memory 504 or sent via the communication component 516. In some embodiments, the audio component 510 also includes a speaker for outputting audio signals.
[0143] I / O interface 512 provides an interface between processing component 502 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include but are not limited to: a home button, a volume button, a start button, and a lock button.
[0144] The sensor assembly 514 includes one or more sensors for providing various aspects of status assessment for the device 500. For example, the sensor assembly 514 can detect the open / closed state of the device 500, the relative positioning of components, such as the display and keypad of the device 500, and the sensor assembly 514 can also detect the position change of the device 500 or a component of the device 500, the presence or absence of user contact with the device 500, the orientation or acceleration / deceleration of the device 500, and the temperature change of the device 500. The sensor assembly 514 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. The sensor assembly 514 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 514 may also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
[0145] The communication component 516 is configured to facilitate wired or wireless communication between the device 500 and other devices. The device 500 can access a wireless network based on a communication standard, such as WiFi, a carrier network (such as 2G, 3G, 4G or 5G), or a combination thereof. In an exemplary embodiment, the communication component 516 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 516 also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology and other technologies.
[0146] In an exemplary embodiment, the device 500 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the above methods.
[0147] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 504 including instructions, which can be executed by a processor 520 of the device 500 to perform the above method. For example, the non-transitory computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0148] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0149] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A memory allocation method, characterized in that: include: In response to a memory allocation request triggered by an application, creating a target file corresponding to the application; The file handle and file size of the target file are obtained, and a file mapping memory allocation interface is called; the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, so that the file disk address and a section of virtual address in the process virtual address space have a one-to-one correspondence, and the first address of the target virtual space is obtained as the target virtual address; the file size is the amount of disk space occupied by the file; Obtaining a target virtual address mapped by the target file in the process virtual address space; Allocating the memory space corresponding to the target virtual address to the application includes: returning the first address information of the target virtual space to the application; the memory space corresponding to the target virtual address is used to store data obtained by running the application; The method further comprises: Calling a preset memory allocation interface; the preset memory allocation interface includes a standard memory allocation interface and a file mapping memory allocation interface; after the preset memory allocation interface is called, preferentially calling the first interface to allocate memory space for the application, and when the memory allocation amount allocated to the application by the first interface reaches its corresponding space threshold, calling the second interface to allocate the remaining amount of memory space to be allocated to the application; The first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
2. The memory allocation method according to claim 1, characterized in that: The step of creating a target file corresponding to the application in response to a memory allocation request triggered by the application comprises: Determine the size of the memory space to be allocated according to the memory allocation request; According to the size of the memory space to be allocated, a target file corresponding to the application is created.
3. The memory allocation method according to claim 1, characterized in that: The step of creating a target file corresponding to the application in response to a memory allocation request triggered by the application comprises: In response to a memory allocation request triggered by an application, calling a standard memory allocation interface, wherein the standard memory allocation interface is used to allocate an initial virtual address for the application in the process virtual address space; Obtaining an amount of memory space allocated to the application program through the standard memory allocation interface; When the memory allocation amount is equal to the standard memory allocation space threshold, obtaining the remaining amount of the memory space to be allocated; A target file corresponding to the application is created according to the remaining amount of the memory space.
4. The memory allocation method according to claim 1, characterized in that: The method further comprises: Obtaining the memory space allocation amount corresponding to the target virtual address; When the memory allocation amount corresponding to the target virtual address is equal to the file memory mapping space threshold, the remaining amount of the memory space to be allocated is obtained; Calling a standard memory allocation interface to obtain a virtual address corresponding to the remaining amount of the memory space determined by the standard memory allocation interface; The virtual address corresponding to the remaining amount of the memory space is allocated to the application.
5. The memory allocation method according to claim 3 or 4, characterized in that: The method further comprises: Get the priority of the standard memory allocation interface and the priority of the file mapping memory allocation interface respectively; If the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is called first; If the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is preferentially called.
6. A memory allocation device, characterized in that: include: a request response unit, configured to execute a memory allocation request in response to the memory allocation request triggered by the application program, and create a target file corresponding to the application program; A file parameter acquisition module is configured to execute and acquire the file handle and file size of the target file; the file size is the amount of disk space occupied by the file; The file memory mapping module is configured to execute a call to a file mapping memory allocation interface; the file mapping memory allocation interface is used to obtain a target virtual space corresponding to the file size in the process virtual address space according to the file handle, so that the file disk address and a segment of virtual addresses in the process virtual address space have a one-to-one correspondence, and obtain the first address of the target virtual space as the target virtual address; An address acquisition unit is configured to execute acquisition of a target virtual address mapped by the target file in a process virtual address space; The memory allocation unit is configured to allocate the memory space corresponding to the target virtual address to the application program, including: returning the first address information of the target virtual space to the application program; the memory space corresponding to the target virtual address is used to store data obtained by running the application program; The request response unit is further used to: call a preset memory allocation interface; the preset memory allocation interface includes a standard memory allocation interface and a file mapping memory allocation interface; after the preset memory allocation interface is called, the first interface is preferentially called to allocate memory space for the application, and when the memory allocation amount allocated to the application by the first interface reaches the corresponding space threshold, the second interface is called to allocate the remaining amount of memory space to be allocated to the application; The first interface is one of the standard memory allocation interface and the file mapping memory allocation interface, and the second interface is the other of the standard memory allocation interface and the file mapping memory allocation interface.
7. The memory allocation device according to claim 6, characterized in that: The request response unit comprises: A module for determining memory to be allocated, configured to determine the size of the memory space to be allocated according to the memory allocation request; The target file creation module is configured to execute creation of a target file corresponding to the application program according to the size of the memory space to be allocated.
8. The memory allocation device according to claim 6, characterized in that: The request response unit is configured to execute: In response to a memory allocation request triggered by an application, a standard memory allocation interface is called, wherein the standard memory allocation interface is used to allocate an initial virtual space for the application in the process virtual address space; the memory space allocation amount allocated to the application through the standard memory allocation interface is obtained; when the memory allocation amount corresponding to the initial virtual address is equal to the standard memory allocation space threshold, the remaining amount of the memory space to be allocated is obtained; and according to the remaining amount of the memory space, a target file corresponding to the application is created.
9. The memory allocation device according to claim 8, characterized in that: The device also includes: A memory allocation amount determination unit is configured to execute acquisition of a memory space allocation amount corresponding to the target virtual address; a memory margin determining unit configured to obtain a remaining amount of memory space to be allocated when the memory allocation amount corresponding to the target virtual address is equal to a file memory mapping space threshold; The request response unit is further configured to execute a call to a standard memory allocation interface to obtain a virtual address corresponding to the remaining amount of the memory space determined by the standard memory allocation interface; The memory allocation unit is further configured to allocate the virtual address corresponding to the remaining amount of the memory space to the application.
10. The memory allocation device according to claim 8 or 9, characterized in that: The device also includes: A priority acquisition unit, configured to execute and respectively acquire the priority of a standard memory allocation interface and the priority of a file mapping memory allocation interface; The request response unit is further configured to execute: If the priority of the standard memory allocation interface is higher than the priority of the file mapping memory allocation interface, the standard memory allocation interface is called first; if the priority of the file mapping memory allocation interface is higher than the priority of the standard memory allocation interface, the file mapping memory allocation interface is called first.
11. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the memory allocation method as described in any one of claims 1 to 5.
12. A storage medium, when instructions in the storage medium are executed by a processor of an electronic device, enables the electronic device to execute the memory allocation method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Program memory processing system and method
CN108762919A
Memory management method,system and related device for I / O stack in kernel state system
CN109445943A
Memory management method and device, terminal and storage medium
CN109669877A
Method and device for assisting bitmap object memory allocation
CN110362311A
Memory management method and device of application program, terminal equipment and medium
CN110633141A
