A memory optimization method and system
By collecting and analyzing the application's memory level and node information, abnormal pages were located and repaired, resolving the lag and program killing issues caused by memory anomalies and optimizing memory usage.
Patent Information
- Application Number
- CN202311597726.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-27
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2043-11-27
AI Technical Summary
Existing technologies cannot effectively locate and repair application memory anomalies, leading to problems such as lag and system crashes when memory usage increases.
By collecting the application's memory level and memory node information, anomalies are detected using preset thresholds, abnormal pages and operation paths are analyzed, and the corresponding memory nodes are repaired.
It enables accurate location and repair of application memory anomalies, optimizes memory usage, and avoids the risk of the system killing the application.
Smart Images

Figure CN117520039B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of application programs, in particular to a memory optimization method and system. BACKGROUND
[0002] Memory is one of the basic performance management elements of an application. During the use of an application program, as the used memory increases, it may cause phenomena such as lagging and overheating. In addition, the operating system has an upper threshold value for the memory used by an application program. Once the memory usage reaches this threshold value, the system may kill the corresponding application program.
[0003] However, there is currently no effective means to investigate the memory scene. Through the running data of an application program, only the overall memory situation of the current version of the application program can be known. However, it is difficult to locate specific problems and even more difficult to confirm the repair effect. SUMMARY
[0004] The purpose of the present application is to provide a memory optimization method that can locate memory abnormalities of an application program and repair them.
[0005] To solve the above technical problems, the present application provides a memory optimization method for an application program, and the specific technical solutions are as follows:
[0006] Collecting memory water level information and memory node information of the application program; the memory water level information is an average occupied memory value of the application program on a plurality of terminals;
[0007] If the occupied memory in the memory water level information exceeds a preset threshold value, it is determined that the current version of the application program is abnormal;
[0008] According to the memory node information, an abnormal page of the application program is determined, as well as an operation path and a memory object distribution of the abnormal page;
[0009] According to the operation path and the memory object distribution, the corresponding memory node is repaired.
[0010] Optionally, collecting the memory water level information and the memory node information of the application program comprises:
[0011] Periodically collecting the memory water level information of the application program; the memory water level information includes any one or any combination of the following: a current page, the occupied memory, available memory, memory changes, and an operation path;
[0012] When the object creation function pointer is not empty, record the memory node information; the memory node information includes memory allocation information, page information, function library information, and node object information.
[0013] Optionally, after the periodically collecting the memory water level information of the application program, the method further comprises:
[0014] According to the runtime recorded page path, the user operation path is determined.
[0015] Optionally, after the determining the user operation path according to the runtime recorded page path, the method further comprises:
[0016] It is determined whether the user operation path is triggered for the first time.
[0017] If the user operation path is triggered for the first time, it is determined whether the current memory occupation satisfies the memory threshold.
[0018] If yes, the user operation path is reported.
[0019] Optionally, when the object creation function pointer is not empty, the recording the memory node information comprises:
[0020] When the specific object creation function pointer and the system object creation function pointer are both not empty, the memory node information is recorded through the specific object creation function pointer and the system object creation function pointer.
[0021] Optionally, when the memory node information is recorded through the specific object creation function pointer and the system object creation function pointer, the method further comprises:
[0022] It is determined that the operation type of the memory node information.
[0023] If the memory node information is an opening node, a newly opened node is recorded.
[0024] If the memory node information is a release node, the record corresponding to the release node is deleted.
[0025] Optionally, after the recording the memory node information when the object creation function pointer is not empty, the method further comprises:
[0026] The virtual address corresponding to the memory occupation of the application program is captured through stack backtracking, and the virtual address is stored by using a hash table.
[0027] The memory node corresponding to the virtual address is written into a splay tree; wherein the splay tree is a data structure for storing memory data, and the memory node with more occurrence times is closer to the top of the splay tree.
[0028] Optionally, after the collecting the memory water level information and the memory node information of the application program, the method further comprises:
[0029] The page information is recorded, and the association between the page information and the memory water level information is established.
[0030] Optionally, after the memory water level information and the memory node information of the application program are collected, the method further comprises:
[0031] The memory node information is symbolized, and dynamic linker image information is recorded; the dynamic linker image information is used to determine the corresponding symbol table address when the symbolization is performed.
[0032] The application further provides a memory optimization system, comprising:
[0033] An information collection module is configured to collect memory water level information and memory node information of the application program; the memory water level information is an average occupied memory value of the application program on a plurality of terminals;
[0034] An exception detection module is configured to determine that a current version of the application program is abnormal if the occupied memory in the memory water level information exceeds a preset threshold.
[0035] An exception analysis module is configured to determine an abnormal page of the application program, and an operation path and a memory object distribution of the abnormal page according to the memory node information.
[0036] A memory optimization module is configured to repair a corresponding memory node according to the operation path and the memory object distribution.
[0037] The application provides a memory optimization method for an application program, and the specific technical solutions are as follows: memory water level information and memory node information of the application program are collected; the memory water level information is an average occupied memory value of the application program on a plurality of terminals; if the occupied memory in the memory water level information exceeds a preset threshold, it is determined that a current version of the application program is abnormal; an abnormal page of the application program, and an operation path and a memory object distribution of the abnormal page are determined according to the memory node information; and a corresponding memory node is repaired according to the operation path and the memory object distribution.
[0038] The application collects memory water level information and memory node information of an application program, monitors and counts the memory of the application program when the application program runs, detects whether the memory occupation of the application program is normal by setting a preset threshold, analyzes the memory node information, accurately locates an abnormal page, and determines an operation path and a memory object distribution of the abnormal page, so that the memory is repaired, the memory exception can be effectively located and repaired, and the memory repair effect is optimized.
[0039] The application further provides a memory optimization system, which has the beneficial effects described above, and details are not repeated here. BRIEF DESCRIPTION OF DRAWINGS
[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the accompanying drawings needed to be used in the embodiments or prior art description will be briefly introduced as follows. Obviously, the accompanying drawings in the following description only are a part of the present application, and the other drawings can be obtained by those ordinarily skilled in the art without any creative work on the premise of the provided drawings.
[0041] Figure 1 A flow chart of a memory optimization method provided by an embodiment of the present application;
[0042] Figure 2 A structure schematic diagram of a spanning tree provided by an embodiment of the present application;
[0043] Figure 3 A structure schematic diagram of a spanning tree after being flipped provided by an embodiment of the present application;
[0044] Figure 4 A structure schematic diagram of a memory optimization system provided by an embodiment of the present application. DETAILED DESCRIPTION
[0045] In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all the other embodiments obtained by those ordinarily skilled in the art without any creative work on the premise of the present application are within the protection scope of the present application.
[0046] Reference will be made to Figure 1 , Figure 1 A flow chart of a memory optimization method provided by an embodiment of the present application, the method comprising:
[0047] S101: collecting memory water level information and memory node information of the application program; the memory water level information is an average occupied memory value of the application program in a plurality of terminals;
[0048] S102: if the occupied memory in the memory water level information exceeds a preset threshold, determining that the current version of the application program is abnormal;
[0049] S103: determining an abnormal page of the application program according to the memory node information, and an operation path and a memory object distribution of the abnormal page;
[0050] S104: repairing a corresponding memory node according to the operation path and the memory object distribution.
[0051] After the application is running, the memory water level information and the memory node information of the application are collected. The memory water level information is used for locating the problematic scene and subsequent reproduction verification, and the collection of the memory node information is mainly used for analyzing the occupied memory data distribution of the scene and locating the specific memory object.
[0052] The so-called memory water level information generally refers to the memory related information occupied by the application running on a plurality of terminals. In order to reduce the influence of different operation paths on the memory occupation during running and the influence of different terminal hardware configurations on the memory occupation, the memory water level information of the application can be collected periodically, and the memory water level information includes any one or any combination of the current page, the occupied memory, the available memory, the memory change and the operation path. It is easy to understand that different page elements are different, and the memory occupation is significantly different, for example, the memory occupied by the video playing page is much higher than that of the text display page. Herein, the period of time for collection is not limited, which can be set by the person skilled in the art, for example, it can be considered to collect once every 5 seconds.
[0053] In a feasible implementation, after the memory water level information of the application is collected, the page path can be recorded according to the running time to determine the user operation path. At the same time, it is determined whether the user operation path is triggered for the first time; if it is triggered for the first time, it is judged whether the current memory occupation meets the memory threshold, and if the memory threshold is met, the user operation path is reported. The user operation path includes the page path and the running time of each page path, that is, the data processed and the corresponding running time. The user operation path does not have to be reported, and is only reported when it is triggered for the first time to meet the memory threshold. Since the same page can be displayed multiple times, in order to avoid incomplete memory recycling in the page switching process, that is, memory residues caused by multiple triggers of the page, the memory threshold can be set in positive correlation with the number of page triggers. For example, starting from 800M, 100M is added after each trigger. That is, the initial memory threshold is 800M, and the memory threshold becomes 900M when triggered next time.
[0054] Meanwhile, in step S101, memory node information also needs to be collected. Specifically, when the object creation function pointer is not empty, the memory node information can be recorded. The memory node information includes memory allocation information, page information, function library information, and node object information. Here, the object creation function pointer is not specifically limited, and mainly includes a specific object creation function pointer malloc_logger and a system object creation function pointer syscall_logger. In this way, when the specific object creation function pointer and the system object creation function pointer are both not empty, the memory node information can be recorded through the specific object creation function pointer and the system object creation function pointer. When the object creation function pointer is empty, the allocation or release of memory is notified to the upper layer through the object creation function pointer, and the memory allocation information is recorded through the object creation function pointer. In this process, the page information, dyld_images information, and object information corresponding to the node can also be supplemented, which is used for subsequent symbolic analysis. The dyld_images information is a function library in an application.
[0055] The specific object creation function pointer malloc_logger is used to track the method of memory allocation and release, which can help developers find memory leaks and other memory-related problems. The system object creation function pointer syscall_logger is a function method for monitoring system calls. The same point of the two is that they will record each memory operation such as malloc, free, calloc, realloc, and record the stack information of the allocated and released memory. The difference is that the system object creation function pointer syscall_logger is mainly a system call. Among them, malloc is a dynamic memory allocation function, free is a dynamic memory release function, calloc is a dynamic memory allocation and zeroing function, and realloc is a dynamic memory adjustment function.
[0056] In a feasible implementation, after obtaining the memory node information, the operation type of the memory node information can be further judged. If the memory node information is an opening node, the newly opened node is recorded. If the memory node information is a release node, the record corresponding to the release node is deleted.
[0057] After obtaining the memory water level information and the memory node information, it is determined whether the application program is abnormal according to whether the occupied memory in the memory water level information exceeds a preset threshold. It should be noted that the preset threshold is not the same as the memory threshold in the foregoing. The memory threshold in the foregoing refers to the occupied memory of a certain operation or a certain page in the application program, rather than the overall occupied memory of the application program. In this step, the preset threshold is determined according to the occupied memory of the application program, and the occupied memory is not from the memory occupation data on a small number of terminals, and is usually determined according to the average of the memory occupation data of the application program on a plurality of terminals. In addition, the specific value of the preset threshold is not limited in this embodiment, and can be set by a person skilled in the art according to the test data or historical version of the application program.
[0058] If it is determined that the current version of the application program is abnormal, the abnormal page can be located according to the memory node information, and the operation path and the memory object distribution thereof are determined. Since the memory abnormal occupation may be caused by abnormal page jumping, only locating the abnormal page is not enough to determine the abnormal reason, and therefore, the operation path thereof also needs to be determined to detect whether the abnormal jumping between pages causes the memory occupation abnormality. The memory object distribution refers to the memory occupation corresponding to each object in the current application program. The object refers to an operation object, or the memory occupation distribution of each module in the application program.
[0059] After the operation path and the memory object distribution of the abnormal page are determined, the corresponding memory node can be repaired according to the operation path and the memory object distribution.
[0060] In the repair process, the obtained memory water level information and memory node information can be analyzed first. In the analysis process, mainly includes determining the page with greater memory fluctuation in the abnormal page as a suspicious memory node according to the memory change information, then obtaining the operation path of the page of the suspicious memory node, and reproducing the operation path to determine the abnormal point, and repairing the abnormal point.
[0061] The embodiments of the application monitor and count the memory of the application program when running by collecting the memory water level information and the memory node information of the application program, detect whether the memory occupation of the application program is normal by setting a preset threshold, and can accurately locate the abnormal page, the operation path thereof and the memory object distribution, so as to repair the memory, effectively locate and repair the memory abnormality, and optimize the memory repair effect.
[0062] On the basis of the above embodiments, in order to realize memory optimization, in a feasible repair mode, the virtual address of the memory occupied by the application program can also be captured through stack backtracking, and the virtual address is stored by using a hash table. Then, the memory nodes corresponding to the virtual address are written into the splay tree. The splay tree is a data structure for storing memory data, and the memory nodes with higher occurrence frequencies are closer to the top of the splay tree. In this way, the memory nodes with higher call frequencies are used more frequently, and they are closer to the top of the splay tree, so that the response efficiency is higher during subsequent calls, and the memory occupation of the application program caused by untimely memory call is avoided.
[0063] The splay tree is a way of storing data. When storing data, data is usually stored according to certain rules, in order to speed up reading. For example, there are five numbers as the scores of students: 78, 75, 80, 92, and 88. If the data is stored in a certain order, such as 75, 78, 80, 88, and 92, then the highest score and the lowest score can be easily obtained (at this time, only the first or the last one needs to be found). If the data is not stored in order, then the highest score and the lowest score can be obtained after comparison according to the sorting algorithm. In the splay tree, each time a node is accessed, the node is moved to the root of the tree, which is called "splay", which helps to find the same node faster in the subsequent search. The performance advantage of the splay tree is that it places the most frequently used nodes at the top of the tree through the splay operation, thereby improving the access speed of these nodes in the future. When the splay tree is applied, when memory opening is detected, the existing memory object is compared. If the memory object already exists, the memory object is directly flipped. If the object that occurs memory opening is a new memory object, a new memory object is added under the splay tree.
[0064] Referring to Figure 2 and Figure 3 , Figure 2 is a structural diagram of the splay tree provided by the embodiments of the present application, Figure 3 is a structural diagram of the splay tree after flipping provided by the embodiments of the present application. Figure 2 In the splay tree structure, five memory objects 20, 30, 40, 50, and 70 are stored. If 30 is detected, it will be flipped to the structure as shown in Figure 3 , and 30 will be moved to the upper layer of the splay tree. In this way, as the branches become more and more, the splay tree becomes deeper, which can ensure that the uppermost layer of the splay tree is the several objects with the highest occurrence frequencies of memory objects. In this way, when detecting memory exceptions, the several memory objects with the highest occurrence frequencies can be directly analyzed, and the memory exception can be more easily found.
[0065] In other embodiments of the present application, after the memory water level information and the memory node information of the application program are collected, page information can be recorded, and an association between the page information and the memory water level information is established.
[0066] The memory object corresponding to the memory node information can also be symbolized, and dynamic linker image information can be recorded. The dynamic linker image information is used to determine the corresponding symbol table address when the symbolization is performed. The dynamic linker image information, i.e., dyld_images information, can record the loading information of each image. In addition, the object type of the memory object can be recorded by Hooking the alloc method.
[0067] In other embodiments, the obtained memory water level information and memory node information can be stored in a database. Specifically, memory archiving can be performed first, i.e., historical memory data is statistically analyzed at regular intervals, and when the number of new versions of the application program exceeds a certain percentage of the total number of versions, the memory water level information and the memory node information can be archived in a database such as MongoDB.
[0068] Since the application market of some platforms can only obtain user usage data of the new version of the application program after a certain amount of download and use after the application is released online, but this process involves gray testing, application submission, online, and other processes, the entire process cycle is long, and the memory feedback cycle of the application program is also long. Therefore, after memory archiving, real-time queries of the memory water level information and the memory node information can be provided, so that the memory data of the version can be analyzed during the gray release of the application program, greatly shortening the memory data feedback time. Further, in order to ensure the query speed, the memory node information reported by the terminal can be stored in ClickHouse.
[0069] Next, a memory optimization system provided by an embodiment of the present application is introduced. The memory optimization system described below can be referred to in conjunction with the memory optimization method described above.
[0070] Referring to Figure 4 , Figure 4 FIG. 1 is a structural schematic diagram of a memory optimization system provided by an embodiment of the present application. The present application also provides a memory optimization system, which comprises:
[0071] An information collection module is configured to collect memory water level information and memory node information of the application program. The memory water level information is an average memory usage value of the application program on a plurality of terminals.
[0072] An anomaly detection module is configured to determine that the current version of the application program is abnormal if the memory usage in the memory water level information exceeds a preset threshold.
[0073] anomaly analysis module, configured to determine an abnormal page of the application program according to the memory node information, and an operation path and a memory object distribution of the abnormal page;
[0074] a memory optimization module, configured to repair a corresponding memory node according to the operation path and the memory object distribution.
[0075] Based on the above embodiment, as a preferred embodiment, the information collection module comprises:
[0076] a memory water level information collection unit, configured to periodically collect memory water level information of the application program; the memory water level information comprises any one or a combination of current pages, the occupied memory, available memory, memory changes, and operation paths;
[0077] a memory node information collection unit, configured to record memory node information when an object creation function pointer is not empty; the memory node information comprises memory allocation information, page information, function library information, and node object information.
[0078] Based on the above embodiment, as a preferred embodiment, the information collection module further comprises:
[0079] an operation path detection unit, configured to record a page path according to a running time, and determine a user operation path.
[0080] Based on the above embodiment, as a preferred embodiment, the information collection module further comprises:
[0081] a path reporting unit, configured to determine whether the user operation path is triggered for the first time; if so, determine whether current memory occupation meets a memory threshold; if so, report the user operation path.
[0082] Based on the above embodiment, as a preferred embodiment, the memory node information collection unit is configured to record the memory node information through a specific object creation function pointer and a system object creation function pointer when both the specific object creation function pointer and the system object creation function pointer are not empty.
[0083] Based on the above embodiment, as a preferred embodiment, the memory node information collection unit further comprises:
[0084] a node type detection unit, configured to determine an operation type of the memory node information; if the memory node information is a newly opened node, record the newly opened node; if the memory node information is a released node, delete a record corresponding to the released node.
[0085] Based on the above embodiment, as a preferred embodiment, the information collection module further comprises:
[0086] The memory optimization unit is configured to capture virtual addresses corresponding to memory occupation of the application program through stack backtracking, and store the virtual addresses by using a hash table.
[0087] Write the memory nodes corresponding to the virtual addresses into a splay tree; wherein the splay tree is a data structure for storing memory data, and the memory nodes with more occurrence times are closer to the top of the splay tree.
[0088] The application further provides a computer readable storage medium, which has a computer program stored thereon, and the computer program can implement the steps provided by the above embodiments when executed. The storage medium can include a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media capable of storing program codes.
[0089] The application further provides an electronic device, which can include a memory and a processor, the memory has a computer program stored therein, and the processor can implement the steps provided by the above embodiments when calling the computer program in the memory. Of course, the electronic device can further include various network interfaces, power supplies and other components.
[0090] The embodiments in the specification are described in a progressive manner, and each embodiment focuses on the difference from other embodiments. The same or similar parts of each embodiment can be referred to each other. For the system provided by the embodiments, since it corresponds to the method provided by the embodiments, the description is relatively simple, and the related parts can be referred to the method part.
[0091] The principles and implementation manners of the application are described by using specific examples in the specification, and the above embodiment descriptions are only used to help understand the method and core idea of the application. It should be noted that, for those skilled in the art, without departing from the principles of the application, the application can be improved and modified in several ways, and these improvements and modifications also fall within the protection scope of the claims of the application.
[0092] It also needs to be explained that in the present specification, the relational terms such as first and second and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises", "comprising", or any other variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or apparatus. An element proceeded by "comprises... a" does not, without more constraints, exclude the existence of additional identical elements in the process, method, article, or apparatus that comprises the element.
Claims
1. A memory optimization method of an application program, characterized by, The application comprises the following steps: Collecting memory water level information and memory node information of the application program; The memory water level information is an average occupied memory value of the application program on several terminals; If the occupied memory in the memory water level information exceeds a preset threshold, it is determined that the current version of the application program is abnormal; According to the memory node information, an abnormal page of the application program is determined, as well as an operation path and a memory object distribution of the abnormal page; According to the operation path and the memory object distribution, the corresponding memory node is repaired; Wherein, the collection of the memory water level information and the memory node information of the application program comprises: Periodically collecting the memory water level information of the application program; the memory water level information comprises any one or any combination of the following items: current page, occupied memory, available memory, memory change and operation path; When the object creation function pointer is not empty, record the memory node information; the memory node information comprises memory allocation information, page information, function library information and node object information; Wherein, when the object creation function pointer is not empty, recording the memory node information comprises: When both the specific object creation function pointer and the system object creation function pointer are not empty, record the memory node information through the specific object creation function pointer and the system object creation function pointer; the specific object creation function pointer is used to track memory allocation and release.
2. The memory optimization method of claim 1, wherein, After periodically collecting the memory water level information of the application program, it further comprises: According to the running time record page path, determine the user operation path.
3. The memory optimization method of claim 2, wherein, After determining the user operation path according to the running time record page path, it further comprises: Determine whether the user operation path is triggered for the first time; If it is triggered for the first time, judge whether the current memory occupation meets the memory threshold; If yes, report the user operation path.
4. The memory optimization method of claim 1, wherein, When recording the memory node information through the specific object creation function pointer and the system object creation function pointer, it further comprises: Determine the operation type of the memory node information; If the memory node information is a newly opened node, record the newly opened node; If the memory node information is a release node, delete the record corresponding to the release node.
5. The memory optimization method of claim 1, wherein, After recording the memory node information when the object creation function pointer is not empty, it further comprises: Capture the virtual address corresponding to the memory occupation of the application program through stack backtracking, and store the virtual address by using a hash table; Write the memory node corresponding to the virtual address into a splay tree; wherein, the splay tree is a data structure used to store memory data, and the memory node with more occurrence times is closer to the top of the splay tree.
6. The memory optimization method of claim 1, wherein, After collecting the memory water level information and the memory node information of the application program, it further comprises: Record the page information and establish the association between the page information and the memory water level information.
7. The memory optimization method of claim 1, wherein, After collecting the memory water level information and the memory node information of the application program, it further comprises: Symbolize the memory object corresponding to the memory node information, and record the dynamic linker image information; the dynamic linker image information is used to determine the corresponding symbol table address when executing the symbolization.
8. A memory optimization system, comprising: The application comprises the following steps: An information collection module is configured to collect memory water level information and memory node information of an application program; The memory water level information is an average occupied memory value of the application program on a plurality of terminals; An abnormality detection module is configured to determine that a current version of the application program is abnormal if the occupied memory in the memory water level information exceeds a preset threshold value; An abnormality analysis module is configured to determine an abnormal page of the application program according to the memory node information, and an operation path and a memory object distribution of the abnormal page; A memory optimization module is configured to repair a corresponding memory node according to the operation path and the memory object distribution. The information collection module includes: A memory water level information collection unit is configured to periodically collect memory water level information of the application program; the memory water level information includes any one or any combination of a current page, the occupied memory, available memory, memory change, and an operation path; A memory node information collection unit is configured to record the memory node information through a specific object creation function pointer and a system object creation function pointer when the specific object creation function pointer and the system object creation function pointer are both not empty; the specific object creation function pointer is used to track memory allocation and release; the memory node information includes memory allocation information, page information, function library information, and node object information.
Citation Information
Patent Citations
Upgrading method, device and terminal of application program
CN104239111A
Memory optimization method and device, equipment and storage medium
CN113360216A