Optimizing memory management method, device, electronic device and storage medium
By obtaining and replacing the address of the system memory management function and dynamically allocating memory, the problem that memory allocators in the existing technology cannot fully manage memory, and more efficient memory management and use are achieved.
Patent Information
- Application Number
- CN202210762617.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-30
- Publication Date
- 2025-05-09
- Estimated Expiration
- 2042-06-30
Smart Images

Figure CN115145727B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a method, device, electronic device and storage medium for optimizing memory management. Background Art
[0002] In the Android system scenario, Figure 1 As shown, in the running mode, the Android system application starts a virtual machine for each application process. The virtual machine accesses the local environment through the Java Native Interface (JNI). The memory required for the application to run can be divided into JAVA memory and native memory. Among them, the memory used by the dynamic library in the application and the virtual machine itself is local memory. When using the memory as mentioned above, the memory is usually dynamically allocated according to the memory allocator. Specifically, the memory allocator implements dynamic memory management by providing a series of function interfaces for applying for or releasing memory.
[0003] When managing memory through a memory allocator, the default configuration is often not necessarily the optimal memory management solution due to global configuration considerations. Since the memory allocator is a system preset library and is delivered in binary form, external callers only have the application program interface that can be called, and the caller cannot intervene in details such as memory allocation and release.
[0004] In the related art, the malloc hook function solution based on the Procedure Linkage Table Hook (PLT hook) can be used to optimize the memory allocator, that is, to implement function interception and replacement by modifying the global offset table (GOT), but this solution can only take over the memory management of individual libraries, and this solution only records the memory management, does not implement the logic of memory release, and does not form a complete set of functions that a complete memory allocator should support. Specifically, in the process of optimizing the memory manager through this solution, only the content allocation function is intercepted and recorded, and the real memory management is still implemented by forwarding to the preset system library, and can only intercept individual dynamic libraries and only intercept functions exported in the GOT.
[0005] Therefore, the above scheme for optimizing the memory allocator cannot fully take over the allocation and management of memory, and the performance of dynamically allocating memory is poor. Summary of the invention
[0006] The present disclosure provides a method, device, electronic device and storage medium for optimizing memory management, so as to at least solve the problem in the related art that the method of optimizing memory allocator cannot fully take over the allocation and management of memory. The technical solution of the present disclosure is as follows:
[0007] According to a first aspect of an embodiment of the present disclosure, a method for optimizing memory management is provided, including: obtaining a first function address of a first memory management function, the first memory management function being a system memory management function; setting the memory permission corresponding to the first function address to a write permission; replacing the first function address with a second function address of a second memory management function, the second memory management function being a preset memory management function; and calling the second memory management function through the second function address to dynamically allocate memory.
[0008] Optionally, the above-mentioned "obtaining the first function address of the first memory management function" includes: when the first memory management function is an export function of the system library, the address allocated to the first memory management function when the dynamic loading library is used to load the function library with the target character as the suffix is determined as the first function address; when the first memory management function is not an export function of the system library, the address that matches the function name of the first memory management function in the memory mapping area of the current process is determined as the first function address.
[0009] Optionally, the above-mentioned "replacing the first function address with the second function address of the second memory management function" includes: searching for a replacement instruction in the inline hook instruction set, the replacement instruction being an instruction for replacing the function address; and replacing the first function address with the second function address of the second memory management function according to the replacement instruction.
[0010] Optionally, the above-mentioned method for optimizing memory management also includes: after replacing the first function address with the second function address of the second memory management function, using a preset function to clear the first function address in the cache, and setting the memory permission corresponding to the first function address to read-only permission.
[0011] Optionally, the above-mentioned optimized memory management method also includes: before obtaining the first function address of the first memory management function, generating a second memory management function according to the function function of the first memory management function; wherein the function function includes at least one of the following: memory allocation, memory release, memory block allocation, and memory alignment allocation.
[0012] Optionally, the above-mentioned optimized memory management method also includes: after replacing the first function address with the second function address of the second memory management function, capturing the target memory management function used to release the allocated memory, the target memory management function is the first memory management function or the second memory management function; when the target memory management function is the first memory management function, releasing the memory space in the heap memory area corresponding to the first memory management function; when the target memory management function is the second memory management function, releasing the memory space in the heap memory area corresponding to the second memory management function.
[0013] Optionally, the above-mentioned optimized memory management method also includes: after capturing the target memory management function for releasing the allocated memory, based on the dynamic tree length corresponding to the target memory management function, judging whether the target memory management function is the first memory management function or the second memory management function; or, based on the pointer mark of the memory to be released in the target memory management function, judging whether the target memory management function is the first memory management function or the second memory management function.
[0014] According to a second aspect of an embodiment of the present disclosure, there is provided an optimized memory management device, including: an acquisition unit, a setting unit, a replacement unit and a calling unit; the acquisition unit is used to acquire a first function address of a first memory management function, the first memory management function is a system memory management function; the setting unit is used to set the memory permission corresponding to the first function address acquired by the acquisition unit to write permission; the replacement unit is used to replace the first function address acquired by the acquisition unit with a second function address of a second memory management function, the second memory management function is a preset memory management function; the calling unit is used to call the second memory management function to dynamically allocate memory through the second function address replaced by the replacement unit.
[0015] Optionally, the above-mentioned acquisition unit is used to: when the first memory management function is an export function of the system library, determine the address allocated to the first memory management function when the dynamic loading library is used to load the function library with the target character as the suffix as the first function address; when the first memory management function is not an export function of the system library, determine the address that matches the function name of the first memory management function in the memory mapping area of the current process as the first function address.
[0016] Optionally, the replacement unit is used to: search for a replacement instruction in the inline hook instruction set, where the replacement instruction is an instruction for replacing a function address; and replace the first function address with a second function address of the second memory management function according to the replacement instruction.
[0017] Optionally, the above-mentioned optimized memory management device also includes a processing unit; the processing unit is used to replace the first function address with the second function address of the second memory management function after the replacement unit replaces the first function address, and then use a preset function to clear the first function address in the cache and set the memory permission corresponding to the first function address to read-only permission.
[0018] Optionally, the above-mentioned optimized memory management device also includes a generation unit; the generation unit is used to generate a second memory management function according to the function function of the first memory management function before the acquisition unit acquires the first function address of the first memory management function; wherein the function function includes at least one of the following: memory allocation, memory release, memory block allocation, and memory alignment allocation.
[0019] Optionally, the above-mentioned optimized memory management device also includes a capture unit and a release unit; the capture unit is used to capture a target memory management function for releasing the allocated memory after the replacement unit replaces the first function address with the second function address of the second memory management function, the target memory management function being the first memory management function or the second memory management function; the release unit is used to release memory space in the heap memory area corresponding to the first memory management function when the target memory management function captured by the capture unit is the first memory management function; the release unit is also used to release memory space in the heap memory area corresponding to the second memory management function when the target memory management function captured by the capture unit is the second memory management function.
[0020] Optionally, the above-mentioned optimized memory management device also includes a judgment unit; the judgment unit is used to judge whether the target memory management function is the first memory management function or the second memory management function based on the dynamic tree length corresponding to the target memory management function after the capture unit captures the target memory management function for releasing the allocated memory; the judgment unit is also used to judge whether the target memory management function is the first memory management function or the second memory management function based on the pointer mark of the memory to be released in the target memory management function after the capture unit captures the target memory management function for releasing the allocated memory.
[0021] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing processor executable instructions; wherein the processor is configured to execute the above instructions to implement an optimized memory management method provided by the first aspect and any possible design method thereof.
[0022] According to a fourth aspect of an embodiment of the present disclosure, a readable storage medium is provided. When instructions in the readable storage medium are executed by a processor, an optimized memory management method as provided in the first aspect and any possible design thereof can be implemented.
[0023] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including a computer program / instruction, which, when executed by a processor, implements the optimized memory management method provided in the first aspect and any possible design thereof.
[0024] The technical solution provided by the present disclosure brings at least the following beneficial effects: first, obtain the first function address of the first memory management function, then set the memory permission corresponding to the first function address to write permission, then replace the first function address with the second function address of the second memory management function, and finally call the second memory management function through the second function address to dynamically allocate memory. In this way, memory is dynamically allocated at runtime, so that when managing memory, it is no longer limited to the limitation of memory allocation by the system library. It can dynamically allocate and manage memory according to actual operating conditions and needs within the process, and optimize memory application and release performance and memory usage performance. That is, it can reduce memory occupancy, use less memory to complete the same memory application and release requirements, and can also improve memory allocation and release performance, increase memory allocation and release speed, and can also improve memory usage efficiency and memory access rate.
[0025] 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
[0026] 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.
[0027] Figure 1 is a schematic diagram showing that an Android system application is started as a process of each application in a running mode according to an exemplary embodiment;
[0028] Figure 2 is one of the flowcharts of a method for optimizing memory management according to an exemplary embodiment;
[0029] Figure 3 is a second flow chart of a method for optimizing memory management according to an exemplary embodiment;
[0030] Figure 4 is a third flow chart of a method for optimizing memory management according to an exemplary embodiment;
[0031] Figure 5 is a fourth flow chart of a method for optimizing memory management according to an exemplary embodiment;
[0032] Figure 6is a fifth flow chart of a method for optimizing memory management according to an exemplary embodiment;
[0033] Figure 7 is a sixth flowchart of a method for optimizing memory management according to an exemplary embodiment;
[0034] Figure 8 is a schematic diagram of function correspondence of a method for optimizing memory management according to an exemplary embodiment;
[0035] Fig. 9 is a block diagram showing a device for optimizing memory management according to an exemplary embodiment;
[0036] Fig.10 The diagram is a schematic structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0037] 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.
[0038] 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.
[0039] In addition, in the description of the embodiments of the present disclosure, unless otherwise specified, " / " means or, for example, A / B can mean A or B. "And / or" in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present disclosure, "multiple" means two or more than two.
[0040] In order to optimize the memory allocator, the LD_PRELOAD solution based on the environment variable in the Root environment with the highest system permissions can be used. This solution can completely replace the entire memory allocator, but the applicable environment is limited and can only be used in the Root environment. The electronic products purchased by users usually do not open the Root environment and do not have an environment for optimizing memory allocation. Furthermore, this solution can only perform replacement from the beginning of the entire process and cannot be replaced dynamically in real time. As a result, the program that can be optimized by this solution is limited.
[0041] In order to optimize the memory allocator, you can also use the memory leak malloc_debug solution based on the wrapping script wrap.sh. This solution has limited usage environment and can only be used in the debugging environment using the debug package. Second, it has limited functions and can only obtain information about memory allocation and release, and does not implement the complete memory allocator function. Third, the process is running, which results in limited programs that can be optimized by this solution.
[0042] The optimized memory management method provided by the embodiment of the present disclosure is described below in conjunction with the accompanying drawings. The method is exemplarily illustrated below by taking the execution subject as an optimized memory management device as an example. Figure 2 The following is a flow chart of a method for optimizing memory management provided by an embodiment of the present disclosure. Figure 2 As shown, the optimized memory management method provided by the embodiment of the present disclosure includes the following steps 201 to 204.
[0043] Step 201: The memory management optimization device obtains a first function address of a first memory management function.
[0044] In the disclosed embodiment, the first memory management function is a system memory management function. It should be noted that under the von Neumann system, memory is divided into stack memory and dynamically allocated memory. Stack memory is not flexible, and dynamic memory allocation is more commonly used. A memory manager can be used to implement dynamic memory allocation. The first memory management function belongs to the memory manager. The memory manager assumes the responsibility of dynamically allocating memory and can provide a series of function interfaces for applying for memory or releasing memory to implement dynamic memory management functions. The first memory management function corresponds to the function interface. The first memory management function is used to implement the specific implementation process of memory management, and the function interface is used to call the first memory management function.
[0045] In the disclosed embodiment, the first memory management function is called to dynamically allocate memory, that is, the memory is dynamically allocated according to the program file stored at the first function address corresponding to the first memory management function. Therefore, in order to optimize memory management, it is necessary to change the program file corresponding to the first function address, or change the first function address to call the second memory management function.
[0046] Optionally, in the embodiment of the present disclosure, if Figure 3 As shown, step 201 can be implemented through steps 301 and 302.
[0047] Step 301, when the first memory management function is an export function of the system library, the memory management optimization device determines the address allocated to the first memory management function when the dynamic loading library is used to load the function library with the target character as the suffix as the first function address.
[0048] Step 302: When the first memory management function is not an exported function of the system library, the memory management optimization device determines the address that matches the function name of the first memory management function in the memory mapping area of the current process as the first function address.
[0049] In the embodiment of the present disclosure, although the first memory management function belongs to the system library, due to the different implementation methods thereof, different methods can be used to determine the first function address depending on whether the first memory management function is an exported function of the system library.
[0050] Exemplarily, the target character may be so. When the suffix of the function library is so, it can indicate that the function library set to which the first memory management function belongs can implement a certain specific function.
[0051] The technical solution provided by the present disclosure brings at least the following beneficial effects: according to the characteristics of the first memory management function, different solutions are adopted to determine the first function address, so that the first function address is more accurate, so as to improve the success rate of optimizing memory management.
[0052] S202: The device for optimizing memory management sets the memory permission corresponding to the first function address as write permission.
[0053] In the disclosed embodiment, the memory permission corresponding to the first function address is set to read-only permission by default, so as to avoid the situation where the first memory management function cannot be called due to modification of the first function address, so as to ensure that the memory manager can dynamically allocate memory.
[0054] In the disclosed embodiment, during the process of optimizing the memory management method by the memory management optimization device, the memory right corresponding to the first function address is set as write permission, and the modification status of the first function address is opened to provide a modification opportunity for optimizing the memory management, that is, optimized memory management can only be achieved when the memory permission corresponding to the first function is write permission.
[0055] S203: The memory management optimization device replaces the first function address with the second function address of the second memory management function.
[0056] In the embodiment of the present disclosure, the second memory management function is a preset memory management function. The second memory management function is a function written based on the first memory management function (system memory management function) to implement memory management, such as memory allocation, memory release, etc.
[0057] It is understandable that since the memory allocator is the underlying basic software, for the Android platform, the memory allocator is built into the system C library. All system libraries that the application depends on when running, such as the virtual machine library file libart.so, the page rendering library file libhwui.so, and the development code of the application itself will rely on the system C library. This kind of dependence means that the developer cannot directly modify the first memory management function, but can only rewrite the second memory management function to implement the functions of the memory management function.
[0058] Optionally, in the embodiment of the present disclosure, if Figure 4 As shown, step 203 can be implemented through steps 401 and 402.
[0059] Step 401: The optimized memory management device searches for a replacement instruction in an inline hook instruction set.
[0060] Step 402: The memory management optimization device replaces the first function address with the second function address of the second memory management function according to the replacement instruction.
[0061] In the disclosed embodiment, the replacement instruction is an instruction for replacing a function address. The instructions stored in the inline hook instruction set belong to the technology at the central processing unit CPU level and can intercept system library functions. Therefore, by replacing the instruction, the first function address can be replaced with the second function address.
[0062] In the disclosed embodiment, during the function interception replacement process, the replacement method is transparent to all service access parties, that is, the code does not need to be recompiled, and the first memory management function (for allocating and releasing memory) linked at compile time is still used, but is replaced at runtime.
[0063] The technical solution provided by the present disclosure brings at least the following beneficial effects: since the first memory management function corresponding to the first function address belongs to the system library function, the address can be replaced by replacing instructions in the inline hook instruction set, so that memory allocation can be performed according to the second memory management function.
[0064] S204: The memory management optimization device calls the second memory management function through the second function address to dynamically allocate memory.
[0065] In the embodiment of the present disclosure, calling the second memory management function refers to dynamic memory allocation at runtime, that is, when the process is started, memory information is collected, or memory usage exceeds a threshold, the second memory management function can be called to dynamically allocate memory.
[0066] Optionally, in the embodiment of the present disclosure, if Figure 5 As shown, this step 204 can be implemented through the following steps 501 to 503.
[0067] Step 501: The memory management optimization device captures a target memory management function for releasing allocated memory.
[0068] Step 502: When the target memory management function is the first memory management function, the memory management optimization device releases memory space in the heap memory area corresponding to the first memory management function.
[0069] Step 503: When the target memory management function is the second memory management function, the memory management optimization device releases memory space in the heap memory area corresponding to the second memory management function.
[0070] In the disclosed embodiment, the target memory management function is the first memory management function or the second memory management function. It should be noted that releasing memory can also be called memory forwarding. Depending on whether the target memory management function is the first memory management function or the second memory management function, memory space is released in the corresponding heap memory area.
[0071] In the disclosed embodiment, the interception and replacement of the system memory allocation function also needs to pay attention to the timing, that is, at what stage of the application process is the replacement performed. Ideally, the replacement is performed at the first moment of process startup, and such replacement is the most thorough. However, in reality, firstly, the system platform does not have a main function main entrance that can be controlled by the developer, and secondly, even if the replacement is performed at the main function main entrance, there is still memory allocation in the pre-execution stage that cannot be intercepted and replaced. On this basis, if only the first memory management function is replaced with the second memory management function, a real replacement cannot be achieved, because a thorough replacement cannot be performed at the first time, and there is a small amount of memory allocated by the first memory management function in the pre-execution stage. If a simple release replacement is performed directly on this part of the memory, it will cause the system to crash, and this part of the memory needs to be released by the system allocator.
[0072] The technical solution provided by the present disclosure brings at least the following beneficial effects: distinguishing the heap memory areas corresponding to the first memory management function and the second memory management function, and releasing memory space in the corresponding areas to facilitate better management of the memory space.
[0073] Further optionally, in an embodiment of the present disclosure, the above-mentioned step 501 also includes any of the following items: based on the dynamic tree length corresponding to the target memory management function, judging whether the target memory management function is the first memory management function or the second memory management function; based on the pointer mark of the memory to be released in the target memory management function, judging whether the target memory management function is the first memory management function or the second memory management function.
[0074] In the embodiment of the present disclosure, for the method based on the dynamic tree length corresponding to the target memory management function, it is necessary to determine the memory management scheme based on the built-in data structure, that is, to query the data structure of the memory block of the allocated memory to determine whether the memory block of the allocated memory is allocated by the first memory management function.
[0075] Exemplarily, the unit of the memory block of the allocated memory is the logical unit extent, and the structure for managing the extent is the spatial data index rtree; if the above-mentioned allocated memory is allocated by the second memory management function, then there is a corresponding extent in the second memory management function; query in the rtree whether the above-mentioned extent exists at the current address, if it exists, the above-mentioned allocated memory is allocated by the second memory management function, if not, the above-mentioned allocated memory is allocated by the first memory management function (system memory allocation function).
[0076] In the disclosed embodiment, for a processor with a 64-bit architecture, the actual effective addressing space is less than , only the lower 48 bits may be actually used, and the upper 8 bits are all 0. Therefore, when the CPU converts the virtual address to the physical address, the value of the upper 8 bits can be ignored. The above pointer mark can be stored in the upper 8 bits. For example, all the memory allocated by the second memory management function is marked with 0x55, and the memory is released to determine whether the upper 8 bits are 0x55.
[0077] The technical solution provided by the present disclosure brings at least the following beneficial effects: multiple methods are set to determine whether the target memory management function is the first memory management function or the second memory management function, so that when one judgment method is not sufficient to obtain a judgment result, another judgment method can be used to ensure that a judgment result of whether the target memory management function is the first memory management function or the second memory management function can be obtained.
[0078] In the embodiment of the present disclosure, by replacing the first function address with the second function address, and, through the second function address, calling the second memory management function to dynamically allocate memory, the two steps of intercepting and replacing the function are implemented, which can achieve the efficiency of optimizing memory management.
[0079] The technical solution provided by the present disclosure brings at least the following beneficial effects: first, obtain the first function address of the first memory management function, then set the memory permission corresponding to the first function address to write permission, then replace the first function address with the second function address of the second memory management function, and finally call the second memory management function through the second function address to dynamically allocate memory. In this way, memory is dynamically allocated at runtime, so that when managing memory, it is no longer limited to the limitation of memory allocation by the system library. It can dynamically allocate and manage memory according to actual operating conditions and needs within the process, and optimize memory application and release performance and memory usage performance. That is, it can reduce memory occupancy, use less memory to complete the same memory application and release requirements, and can also improve memory allocation and release performance, increase memory allocation and release speed, and can also improve memory usage efficiency and memory access rate.
[0080] Optionally, to maintain memory management stability, Figure 2 On the basis of Figure 6 As shown, after step 204, the embodiment of the present disclosure provides a method for optimizing memory management, which also includes the following step 601.
[0081] Step 601: The optimized memory management device uses a preset function to clear the first function address in the cache and set the memory permission corresponding to the first function address to read-only permission.
[0082] In the disclosed embodiment, the preset function may be the clear_cache function in the builtin function of the compiler. It should be noted that the builtin function is a series of functions preset by the compiler, which are non-C language standard contents, closely related to the hardware, and have high execution efficiency.
[0083] In the disclosed embodiments, cache refers to the high-speed cache on the CPU chip. The cache is composed of logic gate circuits like the CPU registers, and has the characteristics of fast access speed, which is much higher than the speed of accessing the memory. In computer architecture, since the CPU running speed is much higher than the memory running speed, and the program memory access has the characteristics of locality, in order to balance the speed difference between the two, the cache circuit is designed to save the value of some memory addresses. When the CPU reads the memory, it reads from the cache first. When the access memory address does not exist in the cache, it reads from the memory.
[0084] In the disclosed embodiment, the first function address in the cache is cleared by a preset function, that is, the preset function executes to clear the CPU cache, so that when the function address is accessed again, the cache is forcibly bypassed and only read from the memory.
[0085] Optionally, the parameter passed in the preset function clear_cache means only 16 bytes are cleared. The purpose of executing this function is to maintain the consistency of the CPU reading memory to avoid the situation where some instructions are read from the cache and another instruction is read from the memory. If this happens, undefined phenomena will occur, such as system crash.
[0086] The technical solution provided by the present disclosure brings at least the following beneficial effects: by clearing the first function address in the cache, only the second memory management function corresponding to the second function address can be called during access, so as to ensure that the customized first memory management function is called for memory allocation and management.
[0087] Optionally, to maintain memory management stability, Figure 2 On the basis of Figure 7 As shown, before step 201, the embodiment of the present disclosure provides a method for optimizing memory management, which also includes the following step 701.
[0088] Step 701: The memory management optimization device generates a second memory management function according to the function of the first memory management function.
[0089] In the disclosed embodiment, the function functions include at least one of the following: memory allocation, memory release, memory block allocation, memory alignment allocation. It should be noted that, since the first memory management function is delivered in binary form, for external callers, there is only a function call interface that can be called, and the caller cannot intervene in details such as memory allocation and release. In other words, it is impossible to perform deep customization based on the first memory management function, and the caller can only receive the first memory management function, or rewrite the second memory management function that can implement the function functions of the first memory management function.
[0090] It should also be noted that the above function functions can correspond to 5 function call interfaces, such as malloc, free, realloc, memalign and posix_memalign.
[0091] In the disclosed embodiment, the memory allocation function is a user-mode program function for managing the dynamic allocation and release of memory. The second memory management function is used to implement the dynamic memory allocation and release rules customized by the developer. It should be noted that the operating system platform does not provide a method for system memory allocation, and other memory allocation functions other than the system memory allocation function cannot be used to implement the function of associating dynamic memory allocation and release.
[0092] In the disclosed embodiment, the system memory allocation function is oriented to all application processes within the entire operating system. In the process of dynamically allocating and releasing memory, the goal is balance, and there is a lack of customization capabilities for individual application processes. For example, since the size of the mobile phone memory chip is limited, each application should use as little memory as possible. However, in actual use, the memory allocation and release method of the system memory allocation function may not be the preferred method for processing a certain application process. In order to optimize memory management, secondary development is carried out on the basis of the existing open source memory allocation function, and more customization is made for the memory allocation characteristics of the application process itself, so as to improve the performance of the application process itself.
[0093] For example, if an application thread (eg, main thread, audio thread, page rendering thread) needs to frequently call stored data during execution, more memory cache needs to be allocated to the application thread to achieve the purpose of trading space for time.
[0094] The technical solution provided by the present disclosure brings at least the following beneficial effects: before performing function address replacement, a second memory management function is generated to increase the memory allocation speed of dynamically allocated memory.
[0095] In the embodiments of the present disclosure, Figure 8 As shown, the first memory allocation function in the system allocator and the second memory allocation function in the replacement allocator are replaced by an inline hook. The key codes in the replacement process include:
[0096] / / LDR X16, 8;
[0097] / / BR X16;
[0098] / / ADDR (64 bits, i.e. 8 bytes);
[0099] / / arm64-v8a instructions;
[0100] BYTE instructions
[16] = {
[0101] 0x50, 0x00, 0x00, 0x58,
[0102] 0x00, 0x02, 0x1f, 0xd6}
[0103] The above code can be used to modify the first 16 bytes of the second memory management function header to jump to the first memory management function. Specifically, use LDR relative addressing to load the jump address from the memory to the X16 register, and then use the BR jump instruction to perform the replacement.
[0104] The above mainly introduces the scheme of the embodiment of the present disclosure from the perspective of the method. It can be understood that in order to realize the above functions, the optimized memory management device includes at least one of the hardware structure and software modules corresponding to the execution of each function. Those skilled in the art should easily realize that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present disclosure can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present disclosure.
[0105] The embodiments of the present disclosure may divide the functional units of the optimized memory management device according to the above method examples. For example, each functional unit may be divided corresponding to each function, or two or more functions may be integrated into one processing unit. The above integrated units may be implemented in the form of hardware or in the form of software functional units. It should be noted that the division of units in the embodiments of the present disclosure is schematic and is only a logical functional division. There may be other division methods in actual implementation.
[0106] Fig. 9 FIG. 1 is a schematic diagram showing a structure of a device for optimizing memory management according to an exemplary embodiment. Fig. 9 As shown, the optimized memory management device provided by the embodiment of the present disclosure includes an acquisition unit 91, a setting unit 92, a replacement unit 93 and a calling unit 94;
[0107] The acquisition unit 91 is used to acquire a first function address of a first memory management function, where the first memory management function is a system memory management function; for example, Figure 2 As shown, the acquisition unit 91 can be used to execute step 201.
[0108] The setting unit 92 is used to set the memory permission corresponding to the first function address obtained by the obtaining unit 91 to be a write permission; for example, Figure 2 As shown, the setting unit 92 can be used to perform step 202.
[0109] The replacing unit 93 is used to replace the first function address obtained by the obtaining unit 91 with the second function address of the second memory management function, where the second memory management function is a preset memory management function; for example, Figure 2 As shown, the replacement unit 93 can be used to perform step 203.
[0110] The calling unit 94 is used to call the second memory management function to dynamically allocate memory through the second function address replaced by the replacing unit 93. Figure 2 As shown, the calling unit 94 can be used to execute step 204 .
[0111] Optionally, the acquisition unit 91 is used to: when the first memory management function is an export function of the system library, determine the address allocated to the first memory management function when the function library with the target character as the suffix is loaded using the dynamic loading library as the first function address; when the first memory management function is not an export function of the system library, determine the address that matches the function name of the first memory management function in the memory mapping area of the current process as the first function address. For example, Figure 3 As shown, the acquisition unit 91 can be used to execute step 301 and step 302.
[0112] Optionally, the replacement unit 93 is used to: search for a replacement instruction in the inline hook instruction set, where the replacement instruction is an instruction for replacing a function address; and replace the first function address with the second function address of the second memory management function according to the replacement instruction. Figure 4 As shown, the replacement unit 93 can be used to perform step 401 and step 402.
[0113] Optionally, the above-mentioned optimized memory management device further includes a processing unit 95;
[0114] The processing unit 95 is configured to use a preset function to clear the first function address and set the memory permission corresponding to the first function address to read-only permission after the replacement unit 93 replaces the first function address with the second function address of the second memory management function. Figure 6 As shown, the processing unit 95 can be used to execute step 601.
[0115] Optionally, the above-mentioned optimized memory management device further includes a generating unit 96; the generating unit 96 is used to generate a second memory management function according to the function function of the first memory management function before the acquiring unit 91 acquires the first function address of the first memory management function; wherein the function function includes at least one of the following: memory allocation, memory release, memory block allocation, and memory alignment allocation. For example, Figure 7 As shown, the generating unit 96 can be used to execute step 701 .
[0116] Optionally, the above-mentioned optimized memory management device further includes a capture unit 97 and a release unit 98;
[0117] The capture unit 97 is used to capture the target memory management function for releasing the allocated memory after the replacement unit 93 replaces the first function address with the second function address of the second memory management function, where the target memory management function is the first memory management function or the second memory management function; for example, Figure 5 As shown, the grabbing unit 97 can be used to perform step 501 .
[0118] The release unit 98 is used to release memory space in the heap memory area corresponding to the first memory management function when the target memory management function captured by the capture unit 97 is the first memory management function; for example, Figure 5 As shown, the release unit 98 may be used to perform step 502 .
[0119] The release unit 98 is further configured to release memory space in the heap memory area corresponding to the second memory management function when the target memory management function captured by the capture unit 97 is the second memory management function. Figure 5 As shown, the release unit 98 can be used to perform step 503 .
[0120] Optionally, the above-mentioned optimized memory management device also includes a judgment unit 99; the judgment unit 99 is used to judge whether the target memory management function is the first memory management function or the second memory management function based on the dynamic tree length corresponding to the target memory management function after the capture unit 97 captures the target memory management function for releasing the allocated memory; the judgment unit 99 is also used to judge whether the target memory management function is the first memory management function or the second memory management function based on the pointer mark of the memory to be released in the target memory management function after the capture unit 97 captures the target memory management function for releasing the allocated memory.
[0121] Regarding the device in the above embodiment, the specific manner in which each unit performs the operation has been described in detail in the embodiment of the method, and will not be elaborated here.
[0122] Fig.10 Schematic diagram of the structure of an electronic device provided by the present disclosure. Fig.10 The electronic device may include a processor 1001 and a memory 1002 for storing executable instructions of the processor 1001; wherein the processor 1001 is configured to execute the instructions to implement the optimized memory management method in the above embodiment.
[0123] In addition, the electronic device may further include a communication bus 1003 and at least one communication interface 1004 .
[0124] The processor 1001 may be a central processing unit (CPU), a microprocessing unit, an application specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of the program of the disclosed solution.
[0125] The communication bus 1003 is a signal path for transmitting information between the above components.
[0126] The communication interface 1004 uses any transceiver or other device for communicating with other devices or communication networks, such as Ethernet, radio access network (RAN), wireless local area networks (WLAN), etc.
[0127] The memory 1002 may be a read-only memory (ROM) or other types of static storage devices that can store static information and instructions, a random access memory (RAM) or other types of dynamic storage devices that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical disc, laser disc, optical disc, digital versatile disc, Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 1002 may exist independently and be connected to the processor 1001 via the communication bus 1003. The memory 1002 may also be integrated with the processor 1001.
[0128] The memory 1002 is used to store instructions for executing the solution of the present disclosure, and the execution is controlled by the processor 1001. The processor 1001 is used to execute the program or instructions stored in the memory 1002, so as to realize the functions in the method of the present disclosure.
[0129] As an example, combining Fig. 9 , optimize the functions and Fig.10 The function of processor 1001 in is the same.
[0130] In a specific implementation, as an embodiment, the processor 1001 may include one or more CPUs, such as Fig.10 CPU0 and CPU1 in.
[0131] In a specific implementation, as an embodiment, the electronic device may include multiple processors 1001, each of which may be a single-CPU processor or a multi-CPU processor. The processor 1001 herein may refer to one or more devices, circuits, and / or processing cores for processing data (e.g., computer program instructions).
[0132] In a specific implementation, as an embodiment, the electronic device may further include an output device 1005 and an input device 1006. The output device 1005 communicates with the processor 1001 and may display information in a variety of ways. For example, the output device 1005 may be a liquid crystal display (LCD), a light emitting diode (LED) display device, a cathode ray tube (CRT) display device, or a projector. The input device 1006 communicates with the processor 1001 and may receive user input in a variety of ways. For example, the input device 1006 may be a mouse, a keyboard, a touch screen device, or a sensor device.
[0133] Those skilled in the art will understand that Fig.10 The structure shown in the figure does not constitute a limitation on the electronic device, and may include more or less components than shown in the figure, or combine certain components, or adopt a different component arrangement. Fig.10 The electronic device in the system may be a server, a client or other device.
[0134] In addition, the present disclosure also provides a readable storage medium, which stores a program or instruction. When the instructions in the readable storage medium are executed by the processor, the electronic device can execute the optimized memory management method provided in the above embodiment. Optionally, the 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.
[0135] In addition, the present disclosure also provides a computer program product, including a computer program / instructions, which is stored in a non-volatile readable storage medium. When the computer program product is executed by at least one processor, the electronic device executes the optimized memory management method provided in the above embodiment.
[0136] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure 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 description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the claims.
[0137] 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 method for optimizing memory management, characterized in that: include: Obtaining a first function address of a first memory management function, where the first memory management function is a system memory management function; Setting the memory permission corresponding to the first function address to write permission; Replacing the first function address with a second function address of a second memory management function, where the second memory management function is a preset memory management function; capturing a target memory management function for releasing the allocated memory, where the target memory management function is the first memory management function or the second memory management function; In a case where the target memory management function is the first memory management function, releasing memory space in a heap memory area corresponding to the first memory management function; In a case where the target memory management function is the second memory management function, releasing memory space in a heap memory area corresponding to the second memory management function; The second memory management function is called through the second function address to dynamically allocate memory.
2. The method for optimizing memory management according to claim 1, characterized in that: The obtaining of the first function address of the first memory management function comprises: In the case where the first memory management function is an export function of a system library, an address allocated to the first memory management function when a function library with a target character as a suffix is loaded using a dynamic loading library is determined as the first function address; In the case that the first memory management function is not an exported function of the system library, an address matching the function name of the first memory management function in the memory mapping area of the current process is determined as the first function address.
3. The method for optimizing memory management according to claim 1, characterized in that: The replacing the first function address with a second function address of a second memory management function comprises: In the inline hook instruction set, searching for a replacement instruction, where the replacement instruction is an instruction for replacing a function address; According to the replacement instruction, the first function address is replaced with a second function address of a second memory management function.
4. The method for optimizing memory management according to claim 1, characterized in that: After replacing the first function address with the second function address of the second memory management function, the method further includes: A preset function is used to clear the first function address in the cache, and the memory permission corresponding to the first function address is set to read-only permission.
5. The method for optimizing memory management according to claim 1, characterized in that: Before obtaining the first function address of the first memory management function, the method further includes: Generate the second memory management function according to the function function of the first memory management function; The function functions include at least one of the following: memory allocation, memory release, memory block allocation, and memory alignment allocation.
6. The method for optimizing memory management according to claim 5, characterized in that: After capturing the target memory management function for releasing the allocated memory, the method further includes any of the following: Based on the length of the dynamic tree corresponding to the target memory management function, determining whether the target memory management function is the first memory management function or the second memory management function; Based on the pointer mark of the memory to be released in the target memory management function, it is determined that the target memory management function is the first memory management function or the second memory management function.
7. An optimized memory management device, characterized in that: It includes an acquisition unit, a setting unit, a replacement unit, a grabbing unit, a releasing unit and a calling unit; The acquiring unit is configured to acquire a first function address of a first memory management function, where the first memory management function is a system memory management function; The setting unit is used to set the memory permission corresponding to the first function address obtained by the obtaining unit as write permission; The replacing unit is used to replace the first function address obtained by the obtaining unit with a second function address of a second memory management function, where the second memory management function is a preset memory management function; The capturing unit is used to capture a target memory management function for releasing the allocated memory after the replacement unit replaces the first function address with the second function address of the second memory management function, wherein the target memory management function is the first memory management function or the second memory management function; The release unit is used to release memory space in the heap memory area corresponding to the first memory management function when the target memory management function captured by the capture unit is the first memory management function; The release unit is further configured to release memory space in the heap memory area corresponding to the second memory management function when the target memory management function captured by the capture unit is the second memory management function; The calling unit is used to call the second memory management function to dynamically allocate memory through the second function address replaced by the replacing unit.
8. The optimized memory management device according to claim 7, characterized in that: The acquisition unit is used to: In the case where the first memory management function is an export function of a system library, an address allocated to the first memory management function when a function library with a target character as a suffix is loaded using a dynamic loading library is determined as the first function address; In the case that the first memory management function is not an exported function of the system library, an address matching the function name of the first memory management function in the memory mapping area of the current process is determined as the first function address.
9. The optimized memory management device according to claim 7, characterized in that: The replacement unit is used for: In the inline hook instruction set, searching for a replacement instruction, where the replacement instruction is an instruction for replacing a function address; According to the replacement instruction, the first function address is replaced with a second function address of a second memory management function.
10. The optimized memory management device according to claim 7, characterized in that: The device further comprises: a processing unit; The processing unit is used to, after the replacement unit replaces the first function address with the second function address of the second memory management function, use a preset function to clear the first function address in the cache and set the memory permission corresponding to the first function address to read-only permission.
11. The optimized memory management device according to claim 7, characterized in that: The device further comprises: a generating unit; The generating unit is configured to generate the second memory management function according to the function of the first memory management function before the acquiring unit acquires the first function address of the first memory management function; The function functions include at least one of the following: memory allocation, memory release, memory block allocation, and memory alignment allocation.
12. The optimized memory management device according to claim 11, characterized in that: The device further comprises any one of the following: a judgment unit; The judging unit is used to judge whether the target memory management function is the first memory management function or the second memory management function based on the length of the dynamic tree corresponding to the target memory management function after the capturing unit captures the target memory management function for releasing the allocated memory; The judgment unit is also used to judge whether the target memory management function is the first memory management function or the second memory management function based on the pointer mark of the memory to be released in the target memory management function after the capture unit captures the target memory management function for releasing the allocated memory.
13. 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 optimized memory management method according to any one of claims 1 to 6.
14. A readable storage medium, characterized in that: When the instructions in the readable storage medium are executed by a processor, the optimized memory management method according to any one of claims 1 to 6 can be implemented.
15. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the optimized memory management method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Method and device for realizing function jump and computer storage medium
CN112015491A
System memory processing method and device, electronic equipment and storage medium
CN113900798A