Memory leak processing method, device and electronic device
By detecting preset frequency and adjusting memory usage status, combined with the transfer of stack information, the problem of existing memory leakage detection increasing the burden on electronic devices is solved, and efficient memory leakage detection and processing is achieved.
Patent Information
- Application Number
- CN202110805414.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-16
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2041-07-16
AI Technical Summary
Existing memory leak detection methods increase the burden and memory consumption of electronic devices.
The unrecoverable memory size in the target memory is detected by preset frequency, and the memory usage status is adjusted according to the detection results. When it is determined that a memory leak occurs, the stack information of all current mirror pools is transferred to the target memory.
No repeated detection is required, which greatly reduces the loss of detection memory leaks to electronic devices and improves detection efficiency and system performance.
Smart Images

Figure CN113448735B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of communication technology, and specifically relates to a memory leak processing method, device and electronic device. Background Art
[0002] Memory leak refers to the heap memory that has been dynamically allocated in the program, which is not released or cannot be released by the program for some reason, resulting in a waste of system memory, slowing down the program running speed or even causing serious consequences such as system crash. Memory leak defects are hidden and cumulative, and are more difficult to detect than other memory access errors. With the increasing demand for computer applications, the design and development of applications are becoming more and more complex. The number of variables that developers deal with in the process of program implementation has also increased significantly. How to effectively allocate and release memory and prevent memory leaks has become more and more prominent.
[0003] At present, the main method for detecting slub memory leaks is the kmemleak method, which records the call stack of the memory application function and starts a kernel thread to periodically scan whether the applied memory is referenced, so as to determine whether there is a suspicion of memory leak. However, this method will increase the burden on electronic devices and memory consumption. Summary of the invention
[0004] The purpose of the embodiments of the present application is to provide a memory leak processing method, device and electronic device, which can solve the problem that the existing memory leak detection increases the burden on electronic devices and memory consumption.
[0005] In a first aspect, an embodiment of the present application provides a method for handling a memory leak, comprising:
[0006] Detect the size of the non-reclaimable memory in the target memory according to the preset frequency;
[0007] According to the detection result, adjusting the usage state of the target memory;
[0008] When it is determined according to the usage status that the target memory leaks, the stack information of all current mirror pools is transferred to the target memory;
[0009] The preset frequency is associated with a memory span corresponding to a usage status of the target memory.
[0010] In a second aspect, an embodiment of the present application provides a memory leak processing device, including:
[0011] A detection module, used for detecting the size of the non-reclaimable memory in the target memory according to a preset frequency;
[0012] A first processing module, used for adjusting the usage state of the target memory according to the detection result;
[0013] A second processing module is used for transferring the stack information of all current mirror pools to the target memory when it is determined that the target memory leaks according to the usage status;
[0014] The preset frequency is associated with a memory span corresponding to a usage status of the target memory.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method described in the first aspect.
[0016] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.
[0017] In a fifth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.
[0018] In an embodiment of the present application, after detecting the size of the non-reclaimable memory in the target memory according to a preset frequency, the usage status of the target memory is adjusted according to the detection result, so that when it is determined that the target memory has a leak based on the usage status, the stack information of all current mirror pools is transferred to the target memory, without the need for repeated detection, which greatly reduces the loss of the electronic device caused by detecting memory leaks. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 It is a flowchart of a memory leak processing method according to an embodiment of the application;
[0020] Figure 2 This is a schematic diagram of the memory structure of the object;
[0021] Figure 3 It is the relationship between the Sunreclaim value and each usage status and detection frequency;
[0022] Figure 4 It is a schematic diagram of state changes;
[0023] Figure 5 It is a schematic diagram of the operation of entering the warning state;
[0024] Figure 6It is a processing diagram of the process of waiting for the state change in the warning state;
[0025] Figure 7 It is a schematic diagram of the operation of entering the leakage state;
[0026] Figure 8 It is a processing diagram during the leakage state waiting state change process;
[0027] Fig. 9 is a structural schematic diagram of a memory leak processing device according to an embodiment of the present application;
[0028] Fig.10 is a schematic diagram of the structure of an electronic device according to an embodiment of the present application;
[0029] Fig.11 It is a schematic diagram of the structure of an electronic device according to another embodiment of the present application. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present application to clearly describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments in the present application belong to the scope of protection of this application.
[0031] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.
[0032] The memory leak processing method provided by the embodiment of the present application is described in detail below through specific embodiments and application scenarios in conjunction with the accompanying drawings.
[0033] like Figure 1 As shown, a memory leak processing method of an embodiment of the present application includes:
[0034] Step 101: Detect the size of the non-reclaimable memory in the target memory according to a preset frequency, wherein the preset frequency is associated with the memory span corresponding to the usage status of the target memory.
[0035] Here, the preset frequency is the detection frequency of the non-recyclable memory size in the target memory. The preset frequency is associated with the memory span corresponding to the usage status of the target memory, that is, for the usage status of the target memory, a frequency will be pre-set based on its memory span, and the non-recyclable memory size in the target memory will be detected at this frequency to reflect the dynamic usage of the target memory and adapt to the needs of different usage statuses. It can be understood that the memory spans corresponding to different usage statuses are different, that is, the detection frequencies are also different.
[0036] Thus, in this step, the size of the irrecoverable memory in the target memory is detected according to the preset frequency.
[0037] Step 102: adjusting the usage status of the target memory according to the detection result.
[0038] Here, based on the detection result of the above step 101, the usage state of the target memory can be adjusted so as to handle the memory leak later.
[0039] Step 103: When it is determined according to the usage status that the target memory leaks, the stack information of all current mirror pools is transferred to the target memory.
[0040] Here, based on the usage status of the target memory adjusted in step 102, when a leak occurs in the target memory, the stack information of all current mirror pools can be transferred to the target memory.
[0041] In this way, according to the method of the embodiment of the present application, after the electronic device detects the size of the non-reclaimable memory in the target memory according to the preset frequency, it adjusts the usage status of the target memory according to the detection result, so that when it is determined that the target memory has a leak based on the usage status, the stack information of all current mirror pools is transferred to the target memory, without the need for repeated detection, which greatly reduces the loss of the electronic device caused by detecting memory leaks.
[0042] It should be known that in this embodiment, the target memory may be a slub memory, and the method of the embodiment of the present application is applied to process the slub memory leak. Of course, the target memory may also be an ion memory, a virtual memory, etc., and the method of the embodiment of the present application may also be applied to other memory leaks (such as an ion memory leak, a virtual memory leak, etc.). Below, the application of the method of the embodiment of the present application will be specifically described by taking the target memory being the slub memory as an example, and other memories are also applicable, and will not be listed one by one here.
[0043] It should also be known that in the embodiments of the present application, based on the possibility of leakage, the usage status of the target memory is divided into three types: normal state, warning state and leakage state. Among them, in order to clarify the usage status of the target memory, a warning threshold and a leakage threshold are set. The warning threshold and / or leakage threshold are configured according to the target memory size of the electronic device, and the warning threshold is higher than the maximum value reached by the normal use of the target memory of the electronic device, and the leakage threshold needs to be higher than the warning threshold. For example, when the electronic device is turned on, the value of Sunreclaim is 300MB, and during normal use, the value of Sunreclaim will fluctuate between 300MB and 1GB. In this case, the warning threshold can be set to 1.5GB, and the leakage threshold can be set to 1.8GB (that is, the maximum warning increment is 300MB).
[0044] In addition, in this embodiment, the preset frequency follows a criterion: in the corresponding use state, the larger the memory span, the lower the detection frequency. Of course, this criterion is used to determine the proportional coefficient of the detection frequency of each use state, and the specific detection frequency can be adjusted according to the performance requirements of the project. For example, the memory span of the normal state is 1200MB, and its corresponding preset frequency is 1200 seconds each time; the memory span of the warning state is 300MB, and its corresponding preset frequency is 300 seconds each time; the leakage state is 200MB, and its corresponding preset frequency is 200 seconds each time.
[0045] Optionally, in this embodiment, step 102 includes:
[0046] When the target memory is currently in a normal state and the size of the unreclaimable memory is greater than a warning threshold, switching the usage state of the target memory to a warning state;
[0047] Alternatively, when the target memory is currently in a normal state and the size of the unreclaimable memory is less than or equal to the warning threshold, the usage state of the target memory is maintained in the normal state.
[0048] That is to say, the electronic device will detect the value of Sunreclaim (i.e., the size of non-reclaimable memory) for the slub memory that is currently in a normal state. When the value of Sunreclaim is greater than the warning threshold, the electronic device will switch the usage state of the slub memory to the warning state; and when the value of Sunreclaim is less than or equal to the warning threshold, the usage state of the slub memory will remain in the normal state.
[0049] The Sunreclaim value is detected periodically at a first frequency, and the first frequency is a preset frequency corresponding to the memory span of the normal state of the slub memory. At this time, the slub memory is in a normal state waiting for a state change. In this way, in a normal state, except for an additional monitoring process (or kernel thread) in the system, no other consumption is brought to the system.
[0050] Optionally, step 102 includes:
[0051] When the target memory is currently in a warning state and meets a preset condition, the use state of the target memory is switched to a leakage state; wherein the preset condition is: the maintenance time of the warning state is less than a first duration, and the size of the irrecoverable memory is greater than a leakage threshold; or the maintenance time of the warning state is greater than or equal to the first duration;
[0052] Alternatively, when the target memory is currently in a warning state and the size of the irrecoverable memory drops to less than the warning threshold before the warning state is maintained for the first duration, the usage state of the target memory is switched to a normal state.
[0053] That is to say, the electronic device will, for the slub memory that is currently in the warning state, detect the value of Sunreclaim and / or obtain the maintenance time of the warning state. When the maintenance time of the warning state is less than the first duration and the value of Sunreclaim is greater than the leakage threshold, or when the maintenance time of the warning state is greater than or equal to the first duration, the use state of the slub memory will be switched to the leakage state; and when the value of Sunreclaim drops to less than the warning threshold before the maintenance time of the warning state reaches the first duration, the use state of the slub memory will be switched to the normal state.
[0054] The Sunreclaim value is detected periodically at a second frequency, and the second frequency is a preset frequency corresponding to the memory span of the warning state of the slub memory. At this time, the slub memory is in the warning state waiting for a state change.
[0055] Here, the first duration is also called the maximum warning duration, which is the time interval from the first detection to the n+1th inspection in the warning state. For example, if n is 36 and the second frequency is 300 seconds each time, the first duration is 300*36=10800 seconds=3 hours.
[0056] Among them, the method of entering the leakage state by reaching the leakage threshold is suitable for situations where the leakage speed is fast, such as a specific scenario triggering a continuous memory leakage. At this time, if the leakage threshold is not reached, and the state is switched after waiting for the first time, oom (out of memory) may occur before the state changes, causing the system to freeze or restart.
[0057] The method of entering the leak state by reaching the first duration is suitable for the case where the leak is slow or the leaked memory is not much. Since the top m mirror pools in the warning state are in working state, the memory requested through them will bring additional memory consumption, so it is not desired that the target memory remain in the warning state for a long time. Therefore, even if the leakage threshold is not reached, the target memory will enter the leak state when the first duration is reached, even if the leak may have stopped at this time.
[0058] It should be understood that in this embodiment, the target memory enters the warning state, that is, there may be a leak, and the leakage state, that is, the leak has occurred. Therefore, optionally, this embodiment also includes:
[0059] When the usage state of the target memory is switched to a first state, obtaining usage information of each buffer pool in the target memory, wherein the first state includes a warning state or a leakage state;
[0060] Determine a target cache pool according to the usage information of each cache pool;
[0061] Adding a mark to the mirror pool of the target cache pool, wherein the mark is used to record application information and release information;
[0062] Among them, when the first state is the warning state, the usage information of each cache pool includes the current usage information of each cache pool; when the first state is the leakage state, the usage information of each cache pool includes the usage information of each cache pool changed within the second time period.
[0063] Here, the usage information of each cache pool (slab_cache) can be obtained from slabinfo in the system memory information. Among them, slabinfo includes the name, set size, used size and number of uses of each cache pool, so the usage information can be the used size and number of uses of the cache pool, or the total amount of cache pool occupied (total memory usage) obtained by calculating the product of the used size and the number of uses.
[0064] After determining the target cache pool, add a tag to its mirror pool to record the request and release information. For example, add the SLAB_STORE_USER flag so that the mirror pool can record the stack of each object. After adding the tag, the memory structure of each object is as follows: Figure 2 As shown in the figure, applying for memory in the image pool will increase memory overhead. The alloc / free track is used to store the application information (application stack information) and release information (release stack information) of the corresponding object (the memory actually allocated to the applicant).
[0065] Of course, when the target memory of the electronic device enters the warning state for the first time, no mirror pool is created. Therefore, when entering the warning state for the first time, a mirror mark will be added to the target cache pool, and the corresponding mirror cache pool will be created at the same time. The memory originally applied for in the cache pool after the mirror mark is added will be proxied by the mirror pool, and the proxy will record the application information of the memory application while allocating the memory.
[0066] When the target memory enters the warning state, it only needs to obtain the current usage information of each cache pool, such as obtaining slabinfo once. Correspondingly, the target cache pool is the m cache pools with the greatest suspected leakage based on the usage information of each cache pool, that is, the top m cache pools with the largest total usage. Of course, if the system does not implement slabinfo data, you can traverse each slab_cache (slab cache pool) by yourself and filter out the top m cache pools with the largest total usage.
[0067] When the target memory enters the leak state, what needs to be obtained is the usage information of each cache pool in the second time period, such as the change between the usage information in the previous warning state and the usage information when entering the leak state, or the change between the usage information when entering the leak state and the usage information at a certain time after entering the leak state. Correspondingly, the target cache pool is the m cache pools with the greatest suspicion of leakage, that is, the top m cache pools with the largest increment, determined based on the usage information of each cache pool.
[0068] It should also be understood that the target memory entering a leak state may be understood as the target content being leaked. Therefore, optionally, step 103 includes:
[0069] When the usage state of the target memory is switched to the leak state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory.
[0070] That is, once the target memory enters the leak state, the proxy memory allocation rights of all mirror pools are revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory.
[0071] In addition, in this embodiment, the target memory enters a leakage state, and different scenarios may have different leakage degrees, so, optionally, step 103 includes:
[0072] In the case where the use state of the target memory is switched to a leakage state, determining that the leakage state is a fast leakage state or a slow leakage state according to a leakage increment threshold and a third duration; wherein the third duration is determined by a preset frequency and a preset number of detections corresponding to the leakage state;
[0073] In the case where the leakage state is a fast leakage state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory;
[0074] When the leakage state is a low-speed leakage state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are traversed when a preset condition is met, and the acquired stack information is transferred to the target memory.
[0075] Here, the leakage increment threshold can also be understood as the maximum leakage increment threshold, which is configured according to the target memory size of the electronic device together with the warning threshold and the leakage threshold. In the step of determining whether the leakage state is a fast leakage state or a slow leakage state according to the leakage increment threshold and the third time length, the electronic device will determine whether the leakage state is a fast leakage state or a slow leakage state by detecting the value of Sunreclaim for the slub memory currently in the leakage state and combining the third time length.
[0076] Among them, the value of Sunreclaim is detected regularly at the third frequency, and the third frequency is a preset frequency corresponding to the memory span of the leakage state of the slub memory. The third frequency can be adjusted according to the performance requirements of the project. For example, when the electronic device is turned on, the value of Sunreclaim is 300MB, and during normal use, the value of Sunreclaim will fluctuate between 300MB and 1GB, then the leakage increment threshold can be set to 200MB. Based on the change of the leakage increment threshold, the memory span of the leakage state, that is, the leakage increment threshold, has changed, and the third frequency will change accordingly.
[0077] For target memory in a fast leaking state, if the leak continues, the system may soon go out of service, resulting in a crash or reboot. In this case, the proxy memory allocation rights of all current mirror pools will be revoked, and all current mirror pools will be immediately traversed to transfer the acquired stack information to the target memory, such as the memory ROM, to avoid data loss after reboot. For target memory in a slow leaking state, the transfer is less urgent than that of a fast leak. In this case, the proxy memory allocation rights of all current mirror pools will be revoked, and all current mirror pools will be traversed when the preset requirements are met to transfer the acquired stack information to the target memory, such as the memory ROM.
[0078] The timing for meeting the preset requirement may be when the system load is less than the corresponding load threshold, so as to achieve staggered use of system resources. Moreover, since the traversal will not be performed immediately, the normally used memory in the mirror pool may be released, and new memory applications will not be in the mirror pool. Therefore, the stack information stored in the mirror pool is more likely to be the real leaked stack information.
[0079] Specifically, determining, according to the leakage increment threshold and the third time duration, that the leakage state is a fast leakage state or a slow leakage state includes:
[0080] If the size of the unrecoverable memory before the third time period is greater than the leakage threshold, the current leakage state is a fast leakage state; if the size of the unrecoverable memory before the third time period is less than or equal to the leakage threshold, the current leakage state is a slow leakage state.
[0081] In this embodiment, the stack information may be the application information and release information of each object. Of course, revoking the proxy memory allocation right of the mirror pool will clear the mirror mark of the corresponding cache pool, and the target memory will return to a normal state.
[0082] Optionally, the embodiment further includes:
[0083] When the target memory enters a leak state, if it is detected that the size of the irrecoverable memory drops to less than the warning threshold before the third time period arrives, the proxy memory allocation right of the mirror pool is revoked and the normal state is returned.
[0084] Since after entering the leakage state, the value of Sunreclaim drops to less than the warning threshold before the third duration arrives, that is, the leakage has not actually occurred, the proxy allocation memory right of the mirror pool can be revoked and return to the normal state.
[0085] Optionally, in this embodiment, after step 103, the following steps are further included:
[0086] Upload and delete log information, where the log information includes stack information of all current image pools.
[0087] Specifically, log information can be uploaded and deleted at an appropriate time. For example, the monitoring process uploads and deletes logs when it detects that the system is idle and connected to WiFi. Of course, developers can also pull log files from the device at any time.
[0088] After the upload is completed, this state can be maintained until the Sunreclaim value is detected to drop below the warning threshold and returns to the normal state.
[0089] Optionally, in this embodiment, after step 103, the following steps are further included:
[0090] Displaying a restart reminder message of the electronic device;
[0091] In response to a user input based on the restart reminder information, restart is executed or restart is exited.
[0092] Here, after the stack information of all current image pools is transferred to the target storage, the user triggers a restart or exits the restart by displaying a restart reminder message. After the restart, the system will be reset. Memory leaks will not cause problems to the system immediately. Generally, the leak will continue for a period of time before the problem will slowly appear. For example, if a user uses a mobile phone with a memory leak, if the leak is slow, there may be no problem in normal use for a week. Assuming that the leaked memory will reach a relatively large amount after two weeks of continuous use, the system will freeze or even oom. In this case, as long as the user restarts the device before serious freezes or ooms occur, that is, the system is restarted before the problem caused by the memory leak occurs, and it will take another two weeks for the problem to occur. Moreover, restarting the device will clear the created image pool and save a small amount of memory. In addition, since the dump has been completed, restarting the device does not affect the saving and uploading of abnormal logs. It can effectively avoid freezing in the middle of a game or freezing when editing an important file without saving.
[0093] Specifically, the restart reminder message may be "The abnormal log collection is complete. We hope you will restart the device once. Thank you for your support of xx. The abnormal information collected this time is xxx,xxx. The abnormal log does not involve any of your private information. Are you willing to upload the abnormal log to xx for analysis?"
[0094] Of course, you can input the user who triggered the restart, that is, the user who actively cooperates in providing log information, and you can also perform feedback operations, such as issuing coupons, mall points, etc., to encourage users to continue to cooperate in collecting logs and analyzing problems in the future.
[0095] In addition, in this embodiment, when the usage status of the target memory switches to the warning status, a pop-up reminder or status bar notification may be performed to remind the user that the system may be abnormal, and to ask the user's opinion whether to allow the recording of abnormal logs. After the user agrees, subsequent processing is performed. If the user does not agree, subsequent processing will not be performed. Among them, the content of the reminder to the user can be roughly set to: "System prompt: The current system may be abnormal. To ensure the user experience of you and other xx brands, we hope to collect system memory usage information for no more than x hours (x is the sum of the maximum warning duration and the maximum leakage duration) through your device. After the collection is completed, you will be reminded again. To thank you for your support in creating better products for us, we will also give you points / coupons..."
[0096] Similarly, when returning to the normal state from other states, the user can be reminded that the abnormal log recording is completed and whether to upload the abnormal log.
[0097] The following describes the application of the method of the embodiment of the present application in conjunction with an electronic device with 8GB of memory:
[0098] Assuming that the Sunreclaim value is 300MB when the electronic device is turned on, and the Sunreclaim value fluctuates between 300MB and 1GB during normal use, the warning threshold can be set to 1.5GB, the leakage threshold can be set to 1.8GB (that is, the maximum warning increment is 300MB), and the leakage increment can be set to 200MB. The detection frequency can be located at 1200 seconds each time (first frequency), 300 seconds each time (second frequency), and 200 seconds each time (third frequency). In this example, the relationship between the Sunreclaim value and each usage status and detection frequency is as follows: Figure 3 shown.
[0099] After the system is initialized, the state of the slub memory changes as follows Figure 4 As shown. First, as in step 1, enter the normal state and wait for the state change in the normal state. Here, the electronic device periodically detects the value of Sunreclaim in the system memory information at a first frequency, and determines whether Sunreclaim is greater than the warning threshold. If so, enter the warning state, otherwise remain in this state. Among them, when entering the warning state, Figure 5 As shown, the slabinfo in the current system memory information is obtained to determine the m cache pools with the greatest current leakage suspicion. Afterwards, the m cache pools are checked one by one to see if they have corresponding mirror pools. If not, the current cache pool is marked with a mirror, and the corresponding mirror pool is created and marked. If it exists, the mirror pool of the current cache pool is marked directly.
[0100] Step 2: Enter the warning state and wait for the state to change. Figure 6 As shown, the value of Sunreclaim in the system memory information is periodically detected at the second frequency. If the value of Sunreclaim is greater than the leakage threshold before the first duration is reached (i.e., the duration of the warning state is less than the first duration, and the value of Sunreclaim is greater than the leakage threshold), or the time of entering the warning state is greater than or equal to the first duration (i.e., the duration of the warning state is greater than or equal to the first duration), the system enters the leakage state; if the value of Sunreclaim drops below the warning threshold before reaching the first duration, the system returns to the normal state. When entering the leakage state, Figure 7 As shown, the slabinfo in the current system memory information is obtained, and the m cache pools with the largest increments are determined by determining the increments compared to the slabinfo obtained in the warning state. After that, the m cache pools are checked one by one to see if there is a corresponding mirror pool. If not, the current cache pool is marked with a mirror, and the corresponding mirror pool is created and marked. If it exists, the mirror pool of the current cache pool is marked directly.
[0101] Step 3, enter the leakage state, and wait for the state change in the leakage state. During the waiting process, according to whether the leakage state is a fast leakage state or a slow leakage state, the stack information of all current mirror pools is transferred to the memory ROM. Specifically, Figure 8 As shown, the value of Sunreclaim in the system memory information is periodically detected at the third frequency. If the value of Sunreclaim drops below the warning threshold before the third time period is reached, the proxy memory allocation right of the mirror pool is revoked and the system returns to the normal state. If the value of Sunreclaim is greater than the leakage increment threshold before the third time period is reached, the current leakage state is a fast leakage state. If the value of Sunreclaim is not greater than the leakage increment threshold before the third time period is reached, the current leakage state is a slow leakage state.
[0102] For the target memory in the fast leakage state, the proxy allocated memory rights of all current mirror pools are revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the memory ROM; for the target memory in the slow leakage state, the proxy allocated memory rights of all current mirror pools are revoked, and all current mirror pools are traversed when the preset requirements are met, and the acquired stack information is transferred to the memory ROM. After the transfer is completed, the log information is uploaded and deleted at an appropriate time. After the upload is completed, the leakage state continues until the value of Sunreclaim is detected to drop below the warning threshold and returns to the normal state.
[0103] In summary, the method of the embodiment of the present application only performs image pool stack information transfer when leakage occurs, and only detects the slab buffer pool with the largest suspicion of leakage, rather than detecting all buffer pools, which greatly reduces the loss of detecting memory leaks to the system. In addition, two rounds of buffer pool selection will be carried out, and it will be marked and mirror images will be created, which not only takes into account the overall memory usage, but also takes into account the memory increment during abnormal periods, ensuring that important suspicious leak sources are not missed. Finally, in the selection of slow leak dump and log upload time, the mode of selecting dump / selecting upload is adopted, and the peak use of system resources is staggered. Since the dump is after clearing the image mark, the memory normally applied for in the image buffer pool will be released, so the delayed dump can reduce the number of suspicious leak sources in the log, which is convenient for developers to troubleshoot problems, and can also reduce the memory required for dumping.
[0104] It should be noted that the memory leak processing method provided in the embodiment of the present application can be executed by a memory leak processing device, or a control module in the memory leak processing device for executing the memory leak processing method. In the embodiment of the present application, the memory leak processing device executing the memory leak processing method is taken as an example to illustrate the memory leak processing device provided in the embodiment of the present application.
[0105] like Fig. 9 As shown, a memory leak processing device according to an embodiment of the present application includes:
[0106] A detection module 910, configured to detect the size of the non-reclaimable memory in the target memory according to a preset frequency;
[0107] A first processing module 920, configured to adjust the usage state of the target memory according to the detection result;
[0108] A second processing module 930 is used to transfer the stack information of all current mirror pools to the target memory when it is determined that the target memory leaks according to the usage status;
[0109] The preset frequency is associated with a memory span corresponding to a usage status of the target memory.
[0110] Optionally, the first processing module includes:
[0111] A first processing submodule is used to switch the usage state of the target memory to a warning state when the target memory is currently in a normal state and the size of the unreclaimable memory is greater than a warning threshold;
[0112] The second processing submodule is used to keep the usage status of the target memory in the normal state when the target memory is currently in the normal state and the size of the irrecoverable memory is less than or equal to the warning threshold.
[0113] Optionally, the first processing module includes:
[0114] The third processing submodule is used to switch the use state of the target memory to the leakage state when the target memory is currently in the warning state and meets the preset condition; wherein the preset condition is: the maintenance time of the warning state is less than the first duration, and the size of the irrecoverable memory is greater than the leakage threshold; or the maintenance time of the warning state is greater than or equal to the first duration;
[0115] The fourth processing sub-module is used to switch the usage status of the target memory to a normal state when the target memory is currently in a warning state and the size of the irrecoverable memory drops to less than the warning threshold before the maintenance time of the warning state reaches the first duration.
[0116] Optionally, the device further comprises:
[0117] an acquisition module, configured to acquire usage information of each cache pool in the target memory when the usage state of the target memory is switched to a first state, wherein the first state includes a warning state or a leakage state;
[0118] A determination module, used for determining a target cache pool according to usage information of each cache pool;
[0119] A third processing module is used to add a mark to the mirror pool of the target cache pool, wherein the mark is used to record application information and release information;
[0120] Among them, when the first state is the warning state, the usage information of each cache pool includes the current usage information of each cache pool; when the first state is the leakage state, the usage information of each cache pool includes the usage information of each cache pool changed within the second time period.
[0121] Optionally, the second processing module is further used for:
[0122] When the usage state of the target memory switches to a leaked state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory.
[0123] Optionally, the second processing module includes:
[0124] A determination submodule, configured to determine, when the use state of the target memory switches to a leakage state, whether the leakage state is a fast leakage state or a slow leakage state according to a leakage increment threshold and a third duration; wherein the third duration is determined by a preset frequency and a preset number of detections corresponding to the leakage state;
[0125] a fifth processing submodule, configured to, when the leakage state is a fast leakage state, revoke the proxy memory allocation right of the image pool, and immediately traverse all current image pools to transfer the acquired stack information to the target memory;
[0126] The sixth processing submodule is used to revoke the proxy allocation memory right of the mirror pool when the leakage state is a low-speed leakage state, and traverse all current mirror pools when a preset condition is met, and transfer the acquired stack information to the target memory.
[0127] Optionally, the device further comprises:
[0128] The fourth processing module is used to upload and delete log information, wherein the log information includes stack information of all current image pools.
[0129] The device of the embodiment of the present application only transfers the image pool stack information when a leak occurs, and only detects the slab buffer pool with the largest suspicion of leaking, rather than detecting all buffer pools, which greatly reduces the loss of detecting memory leaks to the system. In addition, two rounds of buffer pool selection will be carried out, and it will be marked and mirror images will be created, which not only takes into account the overall memory usage, but also takes into account the memory increment during abnormal periods, ensuring that important suspicious leak sources are not missed. Finally, in the selection of slow leak dump and log upload timing, the method of selecting dump / selecting upload is adopted, and the system resources are used in a staggered manner. Since the dump is after clearing the image mark, the memory normally applied for in the image buffer pool will be released, so the delayed dump can reduce the number of suspicious leak sources in the log, which is convenient for developers to troubleshoot problems, and can also reduce the memory required for the dump.
[0130] The memory leak processing device in the embodiment of the present application can be a device, or a component, integrated circuit, or chip in an electronic device. The device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a laptop computer, a PDA, an in-vehicle electronic device, a wearable device, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc. The non-mobile electronic device can be a server, a network attached storage (NAS), a personal computer (PC), a television (TV), a teller machine or a self-service machine, etc., which is not specifically limited in the embodiment of the present application.
[0131] The memory leak processing device in the embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.
[0132] The memory leak processing device provided in the embodiment of the present application can achieve Figures 1 to 8 To avoid repetition, the various processes implemented by the method embodiment are not described here.
[0133] Alternatively, if Fig.10 As shown, an embodiment of the present application also provides an electronic device 1000, including a processor 1001, a memory 1002, and a program or instruction stored in the memory 1002 and executable on the processor 1001. When the program or instruction is executed by the processor 1001, each process of the above-mentioned memory leak processing method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0134] It should be noted that the electronic devices in the embodiments of the present application include the mobile electronic devices and non-mobile electronic devices mentioned above.
[0135] Fig.11 A schematic diagram of the hardware structure of an electronic device to implement an embodiment of the present application.
[0136] The electronic device 1100 includes but is not limited to: a radio frequency unit 1101, a network module 1102, an audio output unit 1103, an input unit 1104, a sensor 1105, a display unit 1106, a user input unit 1107, an interface unit 1108, a memory 1109, and a processor 1110 and other components.
[0137] Those skilled in the art will appreciate that the electronic device 1100 may also include a power source (such as a battery) for supplying power to each component, and the power source may be logically connected to the processor 1110 through a power management system, thereby implementing functions such as managing charging, discharging, and power consumption management through the power management system. Fig.11 The electronic device structure shown in the figure does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently, which will not be described in detail here.
[0138] The processor 1110 is configured to:
[0139] Detect the size of the non-reclaimable memory in the target memory according to the preset frequency;
[0140] According to the detection result, adjusting the usage state of the target memory;
[0141] When it is determined according to the usage status that the target memory leaks, the stack information of all current mirror pools is transferred to the target memory;
[0142] The preset frequency is associated with a memory span corresponding to a usage status of the target memory.
[0143] After the electronic device detects the size of the non-reclaimable memory in the target memory of the electronic device according to a preset frequency, the use status of the target memory is adjusted according to the detection result. Therefore, when it is determined that the target memory has leaked based on the use status, the stack information of all current mirror pools is transferred to the target memory, without the need for repeated detection, which greatly reduces the loss of the electronic device caused by detecting memory leaks.
[0144] Optionally, the processor 1110 is further configured to:
[0145] When the target memory is currently in a normal state and the size of the unreclaimable memory is greater than a warning threshold, switching the usage state of the target memory to a warning state;
[0146] Alternatively, when the target memory is currently in a normal state and the size of the unreclaimable memory is less than or equal to the warning threshold, the usage state of the target memory is maintained in the normal state.
[0147] Optionally, the processor 1110 is further configured to:
[0148] When the target memory is currently in a warning state and meets a preset condition, the use state of the target memory is switched to a leakage state; wherein the preset condition is: the maintenance time of the warning state is less than a first duration, and the size of the irrecoverable memory is greater than a leakage threshold; or the maintenance time of the warning state is greater than or equal to the first duration;
[0149] Alternatively, when the target memory is currently in a warning state and the size of the irrecoverable memory drops to less than the warning threshold before the warning state is maintained for the first duration, the usage state of the target memory is switched to a normal state.
[0150] Optionally, the processor 1110 is further configured to:
[0151] When the usage state of the target memory is switched to a first state, obtaining usage information of each buffer pool in the target memory, wherein the first state includes a warning state or a leakage state;
[0152] Determine a target cache pool according to the usage information of each cache pool;
[0153] Adding a mark to the mirror pool of the target cache pool, wherein the mark is used to record application information and release information;
[0154] Among them, when the first state is the warning state, the usage information of each cache pool includes the current usage information of each cache pool; when the first state is the leakage state, the usage information of each cache pool includes the usage information of each cache pool changed within the second time period.
[0155] Optionally, the processor 1110 is further configured to, when the usage state of the target memory switches to a leaked state, revoke the proxy memory allocation right of the mirror pool, and immediately traverse all current mirror pools to transfer the acquired stack information to the target memory.
[0156] Optionally, the processor 1110 is further configured to:
[0157] In the case where the use state of the target memory is switched to a leakage state, determining that the leakage state is a fast leakage state or a slow leakage state according to a leakage increment threshold and a third duration; wherein the third duration is determined by a preset frequency and a preset number of detections corresponding to the leakage state;
[0158] In the case where the leakage state is a fast leakage state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory;
[0159] When the leakage state is a low-speed leakage state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are traversed when a preset condition is met, and the acquired stack information is transferred to the target memory.
[0160] Optionally, the radio frequency unit 1101 of the electronic device is used to upload log information, and the processor 1110 is used to delete the log information, wherein the log information includes stack information of all current mirror pools.
[0161] In this way, the slab cache pool with the greatest suspicion of leakage is detected instead of all cache pools, which greatly reduces the loss of memory leak detection to the system. In addition, two rounds of cache pool selection will be carried out, and it will be marked and mirrored, taking into account both the overall memory usage and the memory increment during abnormal periods to ensure that important suspected leak sources are not missed. Finally, in the selection of slow leak dumps and log upload timing, the method of selective dumping / selective uploading is adopted to stagger the use of system resources. Since the dump is after the image mark is cleared, the memory normally requested in the image cache pool will be released. Therefore, delayed dumping can reduce the number of suspected leak sources in the log, making it easier for developers to troubleshoot problems, and at the same time it can also reduce the memory required for dumping.
[0162] It should be understood that in the embodiment of the present application, the input unit 1104 may include a graphics processor (Graphics Processing Unit, GPU) 11041 and a microphone 11042, and the graphics processor 11041 processes the image data of the static picture or video obtained by the image capture device (such as a camera) in the video capture mode or the image capture mode. The display unit 1106 may include a display panel 11061, and the display panel 11061 may be configured in the form of a liquid crystal display, an organic light emitting diode, etc. The user input unit 1107 includes a touch panel 11071 and other input devices 11072. The touch panel 11071 is also called a touch screen. The touch panel 11071 may include two parts: a touch detection device and a touch controller. Other input devices 11072 may include, but are not limited to, a physical keyboard, a function key (such as a volume control button, a switch button, etc.), a trackball, a mouse, and a joystick, which will not be repeated here. The memory 1109 can be used to store software programs and various data, including but not limited to applications and operating systems. The processor 1110 may integrate an application processor and a modem processor, wherein the application processor mainly processes an operating system, a user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the modem processor may not be integrated into the processor 1110.
[0163] An embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, each process of the above-mentioned memory leak processing method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0164] The processor is a processor in the electronic device described in the above embodiment. The readable storage medium includes a computer readable storage medium, such as a computer read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0165] An embodiment of the present application further provides a chip, which includes a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the various processes of the above-mentioned memory leak processing method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.
[0166] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.
[0167] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.
[0168] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a computer software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling an electronic device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0169] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A memory leak processing method, characterized in that: include: Detect the size of the non-reclaimable memory in the target memory according to the preset frequency; According to the detection result, adjusting the usage state of the target memory; When it is determined according to the usage status that the target memory leaks, the stack information of all current mirror pools is transferred to the target memory; Wherein, the preset frequency is associated with a memory span corresponding to a usage state of the target memory; The method further comprises: When the usage state of the target memory is switched to a first state, obtaining usage information of each buffer pool in the target memory, wherein the first state includes a warning state or a leakage state; Determine a target cache pool according to the usage information of each cache pool; Adding a mark to the mirror pool of the target cache pool, wherein the mark is used to record application information and release information; Among them, when the first state is the warning state, the usage information of each cache pool includes the current usage information of each cache pool; when the first state is the leakage state, the usage information of each cache pool includes the usage information of each cache pool changed within the second time period.
2. The method according to claim 1, characterized in that The step of adjusting the usage state of the target memory according to the detection result includes: When the target memory is currently in a normal state and the size of the unreclaimable memory is greater than a warning threshold, switching the usage state of the target memory to a warning state; Alternatively, when the target memory is currently in a normal state and the size of the unreclaimable memory is less than or equal to the warning threshold, the usage state of the target memory is maintained in the normal state.
3. The method according to claim 1, characterized in that The step of adjusting the usage state of the target memory according to the detection result includes: When the target memory is currently in a warning state and meets a preset condition, the use state of the target memory is switched to a leakage state; wherein the preset condition is: the maintenance time of the warning state is less than a first duration, and the size of the irrecoverable memory is greater than a leakage threshold; or the maintenance time of the warning state is greater than or equal to the first duration; Alternatively, when the target memory is currently in a warning state and the size of the irrecoverable memory drops to less than the warning threshold before the warning state is maintained for the first duration, the usage state of the target memory is switched to a normal state.
4. The method according to claim 1, characterized in that: The method of transferring the stack information of all current mirror pools to the target memory when it is determined according to the usage status that the target memory leaks comprises: When the usage state of the target memory switches to a leaked state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory.
5. The method according to claim 1, characterized in that: When it is determined according to the usage status that the target memory leaks, transferring the stack information of all current mirror pools to the target memory includes: In the case where the use state of the target memory is switched to a leakage state, determining that the leakage state is a fast leakage state or a slow leakage state according to a leakage increment threshold and a third time length; wherein the third time length is determined by a preset frequency and a preset number of detections corresponding to the leakage state; In the case where the leakage state is a fast leakage state, the proxy allocation memory right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory; When the leakage state is a low-speed leakage state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are traversed when a preset condition is met, and the acquired stack information is transferred to the target memory.
6. The method according to claim 1, characterized in that In the case where it is determined according to the usage status that the target memory leaks, after transferring the stack information of all current mirror pools to the target memory, the method further includes: Upload and delete log information, where the log information includes stack information of all current image pools.
7. A memory leak processing device, characterized in that: include: A detection module, used for detecting the size of the non-reclaimable memory in the target memory according to a preset frequency; A first processing module, used for adjusting the usage state of the target memory according to the detection result; A second processing module is used for transferring the stack information of all current mirror pools to the target memory when it is determined that the target memory leaks according to the usage status; Wherein, the preset frequency is associated with a memory span corresponding to a usage state of the target memory; The device also includes: an acquisition module, configured to acquire usage information of each cache pool in the target memory when the usage state of the target memory is switched to a first state, wherein the first state includes a warning state or a leakage state; A determination module, used to determine a target cache pool according to usage information of each cache pool; A third processing module is used to add a mark to the mirror pool of the target cache pool, wherein the mark is used to record application information and release information; Among them, when the first state is the warning state, the usage information of each cache pool includes the current usage information of each cache pool; when the first state is the leakage state, the usage information of each cache pool includes the usage information of each cache pool changed within the second time period.
8. The device according to claim 7, characterized in that The first processing module comprises: A first processing submodule, configured to switch the usage state of the target memory to a warning state when the target memory is currently in a normal state and the size of the unreclaimable memory is greater than a warning threshold; The second processing submodule is used to keep the usage state of the target memory in the normal state when the target memory is currently in the normal state and the size of the irrecoverable memory is less than or equal to the warning threshold.
9. The device according to claim 7, characterized in that The first processing module comprises: The third processing submodule is used to switch the use state of the target memory to the leakage state when the target memory is currently in the warning state and meets the preset conditions; wherein the preset conditions are: the maintenance time of the warning state is less than the first duration, and the size of the irrecoverable memory is greater than the leakage threshold; or the maintenance time of the warning state is greater than or equal to the first duration; The fourth processing sub-module is used to switch the usage status of the target memory to a normal state when the target memory is currently in a warning state and the size of the irrecoverable memory drops to less than a warning threshold before the maintenance time of the warning state reaches the first duration.
10. The device according to claim 7, characterized in that The second processing module is also used for: When the usage state of the target memory switches to a leaked state, the proxy memory allocation right of the mirror pool is revoked, and all current mirror pools are immediately traversed to transfer the acquired stack information to the target memory.
11. The device according to claim 7, characterized in that The second processing module comprises: A determination submodule, configured to determine, when the use state of the target memory switches to a leakage state, whether the leakage state is a fast leakage state or a slow leakage state according to a leakage increment threshold and a third duration; wherein the third duration is determined by a preset frequency and a preset number of detections corresponding to the leakage state; a fifth processing submodule, configured to, when the leakage state is a fast leakage state, revoke the proxy memory allocation right of the image pool, and immediately traverse all current image pools to transfer the acquired stack information to the target memory; The sixth processing submodule is used to revoke the proxy allocation memory right of the mirror pool when the leakage state is a low-speed leakage state, and traverse all current mirror pools when a preset condition is met, and transfer the acquired stack information to the target memory.
12. The device according to claim 7, characterized in that Also includes: The fourth processing module is used to upload and delete log information, wherein the log information includes stack information of all current mirror pools.
13. An electronic device, characterized in that: It includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the memory leak processing method as described in any one of claims 1 to 6.
14. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by the processor, the steps of the memory leak processing method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Memory leakage detecting method and device
CN102681938A
Memory leak monitoring method, device and electronic equipment
CN112035314A