CN118132404B - Memory leakage detection method and device and electronic equipment - Google Patents

Memory leakage detection method and device and electronic equipment Download PDF

Info

Publication number
CN118132404B
CN118132404B CN202410576360.XA CN202410576360A CN118132404B CN 118132404 B CN118132404 B CN 118132404B CN 202410576360 A CN202410576360 A CN 202410576360A CN 118132404 B CN118132404 B CN 118132404B
Authority
CN
China
Prior art keywords
memory
resource
detection
value
threads
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202410576360.XA
Other languages
Chinese (zh)
Other versions
CN118132404A (en
Inventor
许晶
王鑫
杜君
樊琳
丁娴
李腾浩
王晓东
聂睿
郭艳鹏
王爽
赵婷
杨艺宁
付利莉
梁召庆
王志强
赵世杰
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Electric Power Research Institute Co Ltd CEPRI
Beijing Smartchip Microelectronics Technology Co Ltd
Beijing Smartchip Semiconductor Technology Co Ltd
Original Assignee
China Electric Power Research Institute Co Ltd CEPRI
Beijing Smartchip Microelectronics Technology Co Ltd
Beijing Smartchip Semiconductor Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Electric Power Research Institute Co Ltd CEPRI, Beijing Smartchip Microelectronics Technology Co Ltd, Beijing Smartchip Semiconductor Technology Co Ltd filed Critical China Electric Power Research Institute Co Ltd CEPRI
Priority to CN202410576360.XA priority Critical patent/CN118132404B/en
Publication of CN118132404A publication Critical patent/CN118132404A/en
Application granted granted Critical
Publication of CN118132404B publication Critical patent/CN118132404B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/34Recording or statistical evaluation of computer activity, e.g. of down time, of input/output operation ; Recording or statistical evaluation of user activity, e.g. usability assessment
    • G06F11/3409Recording or statistical evaluation of computer activity, e.g. of down time, of input/output operation ; Recording or statistical evaluation of user activity, e.g. usability assessment for performance assessment
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention provides a memory leakage detection method and device and electronic equipment, and belongs to the technical field of computers. The method comprises the following steps: controlling a plurality of resource using threads to execute a first round switching; controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the following steps are repeatedly executed until the set time threshold is reached: controlling the plurality of resource-using threads to perform a second round robin handover; controlling a memory leakage detection thread to perform second memory detection to obtain a second memory detection value; and determining a memory leak detection result based on the comparison result of the first memory detection value and the second memory detection value. By repeatedly comparing memory detection values of the plurality of resource using threads after the rotation switching, the memory leakage can be accurately detected in a short time due to the fact that the plurality of resource using threads circularly acquire resources and release the resources through the short time slice switching.

Description

Memory leakage detection method and device and electronic equipment
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a memory leak detection method, a memory leak detection device, and an electronic apparatus.
Background
Memory leakage refers to the situation that dynamically allocated memory space is not properly released or recovered during program operation. When there is a memory leak in the program, the memory space allocated to the program is gradually exhausted, resulting in a decrease in system performance, program crash, or unexpected termination.
Therefore, memory leakage is a very serious system problem, and timely detecting and repairing memory leakage is critical to maintaining stability, performance and security of the program. Some common memory leak detection methods include: static code analysis, dynamic memory detection tools, memory analysis tools, and garbage collection mechanisms. However, the above methods have the problems of long detection time and possible omission of detection.
Disclosure of Invention
The embodiment of the invention aims to provide a memory leak detection method, a device and electronic equipment, which are used for solving the defects of long detection time and poor detection accuracy of the existing memory leak detection method.
In order to achieve the above object, an embodiment of the present invention provides a memory leak detection method, including:
Controlling a plurality of resource using threads to execute a first round switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource;
controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value;
the following steps are repeatedly executed until the set time threshold is reached:
Controlling a plurality of the resource using threads to execute second round switching;
Controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value;
and determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
Optionally, each of the resource usage threads has a thread time slice of 1-10ms or one system clock tick.
Optionally, the number of the plurality of resource-using threads is greater than or equal to 3.
Optionally, the thread time slices of each of the resource usage threads are the same, or the thread time slices of each of the resource usage threads are different.
Optionally, the operations of obtaining the memory resource and releasing the memory resource include any one of operations of memory application and memory release, operations of semaphore creation and semaphore deletion, operations of mutex creation and mutex deletion, operations of channel creation and channel deletion, operations of file system creation directory and file system deletion directory, operations of file system creation file and file system deletion file, operations of file system opening file and file system closing file, and operations of opening device and closing device.
Optionally, the multiple resource using threads perform reentry operation through the same pair of the resource obtaining interface and the resource releasing interface, or the multiple resource using threads perform reentry operation through different pairs of the resource obtaining interface and the resource releasing interface.
Optionally, the plurality of resource usage threads and the memory leak detection thread are respectively provided in an operating system, or the plurality of resource usage threads and the memory leak detection thread are respectively provided in an application program.
Optionally, the first memory detection value is a first memory usage value, the second memory detection value is a second memory usage value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
Optionally, the first memory detection value is a first memory remaining value, the second memory detection value is a second memory remaining value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
Optionally, the first memory detection value is a first memory usage rate, the second memory detection value is a second memory usage rate, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
Optionally, the method further comprises:
And executing any one of the operations of prompting abnormality, stopping the system operation and automatically restarting under the condition that the occurrence of the memory leakage condition is determined.
In another aspect, an embodiment of the present invention further provides a memory leak detection apparatus, including:
The first round-robin switching module is used for controlling a plurality of resource using threads to execute first round-robin switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource;
the first memory detection module is used for controlling the memory leakage detection thread to perform first memory detection to obtain a first memory detection value;
The second round-robin switching module is used for controlling a plurality of the resource using threads to execute second round-robin switching;
the second memory detection module is used for controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value;
and the memory leak detection result determining module is used for determining a memory leak detection result based on the comparison result of the first memory detection value and the second memory detection value.
The first round-robin switching module is used for controlling a plurality of resource using threads to execute first round-robin switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource;
the first memory detection module is used for controlling the memory leakage detection thread to perform first memory detection to obtain a first memory detection value;
The second round-robin switching module is used for controlling a plurality of the resource using threads to execute second round-robin switching;
the second memory detection module is used for controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value;
The memory leak detection result determining module is used for determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value;
and the control module is used for controlling the second rotation switching module, the second memory detection module and the memory leakage detection result determining module to repeatedly execute until reaching a set time threshold.
Optionally, each of the resource usage threads has a thread time slice of 1-10ms or one system clock tick.
Optionally, the number of the plurality of resource-using threads is greater than or equal to 3.
Optionally, the thread time slices of each of the resource usage threads are the same, or the thread time slices of each of the resource usage threads are different.
Optionally, the operations of obtaining the memory resource and releasing the memory resource include: the method comprises any one of operations of memory application and memory release, operations of semaphore creation and semaphore deletion, operations of mutex creation and mutex deletion, operations of channel creation and channel deletion, operations of file system creation and file system deletion, operations of file system opening and file system closing, and operations of opening and closing devices.
Optionally, the multiple resource using threads perform reentry operation through the same pair of the resource obtaining interface and the resource releasing interface, or the multiple resource using threads perform reentry operation through different pairs of the resource obtaining interface and the resource releasing interface.
Optionally, the plurality of resource usage threads and the memory leak detection thread are respectively provided in an operating system, or the plurality of resource usage threads and the memory leak detection thread are respectively provided in an application program.
Optionally, the first memory detection value is a first memory usage value, the second memory detection value is a second memory usage value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
Optionally, the first memory detection value is a first memory remaining value, the second memory detection value is a second memory remaining value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
Optionally, the first memory detection value is a first memory usage rate, the second memory detection value is a second memory usage rate, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
Optionally, the apparatus further includes:
And the post-processing module is used for executing any one of the operations of prompting abnormality, stopping the system operation and automatically restarting under the condition of determining that the memory leakage condition occurs.
In another aspect, the present invention further provides an electronic device, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor, where the processor implements the memory leak detection method described above when executing the program.
In another aspect, the present invention also provides a machine-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the above-described memory leak detection method.
In another aspect, the present invention further provides a computer program product, including a computer program, where the computer program when executed by a processor implements the above-mentioned memory leak detection method.
According to the technical scheme, the memory leakage detection is carried out by repeatedly comparing the memory detection values of the plurality of resource using threads after the rotation switching, and the plurality of resource using threads can circularly acquire and release the resources through the short-time slice switching.
Additional features and advantages of embodiments of the invention will be set forth in the detailed description which follows.
Drawings
The accompanying drawings are included to provide a further understanding of embodiments of the invention and are incorporated in and constitute a part of this specification, illustrate embodiments of the invention and together with the description serve to explain, without limitation, the embodiments of the invention. In the drawings:
FIG. 1 is a schematic flow chart of a memory leak detection method according to the present invention;
FIG. 2 is a second flow chart of the memory leak detection method according to the present invention;
FIG. 3 is a third flow chart of the memory leak detection method according to the present invention;
FIG. 4 is a flow chart of a memory leak detection method according to the present invention;
FIG. 5 is a flowchart of a memory leak detection method according to the present invention;
FIG. 6 is a schematic diagram of a memory leak detection device according to the present invention;
FIG. 7 is a second schematic diagram of a memory leak detection device according to the present invention;
Fig. 8 is a schematic structural diagram of an electronic device provided by the present invention.
Detailed Description
The following describes the detailed implementation of the embodiments of the present invention with reference to the drawings. It should be understood that the detailed description and specific examples, while indicating and illustrating the invention, are not intended to limit the invention.
Method embodiment
Referring to fig. 1, an embodiment of the present invention provides a memory leak detection method, including:
Step 100, controlling a plurality of resource using threads to execute first round switching; wherein each of the resource-using threads is configured to repeatedly perform operations of acquiring memory resources and releasing memory resources.
Referring to fig. 2, the electronic device may first obtain a plurality (n) of threads with the same priority, which are called resource usage threads. The plurality of resource-usage threads may be pre-created. Wherein each resource-using thread sets a very short thread time slice, e.g., one system clock tick (50 ms), according to the system switch time. Each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource. Referring to fig. 3, in particular, when a resource using thread of the embodiment of the present invention runs, paired operations of acquiring and releasing a resource are performed on a memory resource through an application programming interface in an endless loop. The acquisition and release of the memory resources may be various operations for implementing memory resource acquisition and memory resource release, for example, operations for opening an application program and closing an application program.
The embodiment of the invention controls a plurality of resource using threads to execute first round switching, which refers to a process that a thread dispatcher in an operating system of electronic equipment switches the use right of a CPU from one thread to another thread in the plurality of resource using threads according to a certain rule, so that each different thread in the plurality of resource using threads can be executed alternately. According to the embodiment of the invention, the resource using threads are switched in a rotating way within a very short time slice, so that the running code of the system can be switched out in any time, various thread switching scenes in the actual using condition are reproduced more densely, and the possible memory leakage at each time point can be detected accurately in a short time.
Step 200, controlling a memory leak detection thread to perform first memory detection to obtain a first memory detection value.
The electronic device may build an additional thread, referred to as a memory leak detection thread. The memory leak detection thread has a higher priority than the first plurality of resource usage threads. Referring to fig. 4, the memory leak detection thread is configured to perform memory detection after a plurality of resource usage threads perform round-robin switching. The memory leakage detection thread can detect the memory once every a period of time to obtain a first memory detection value as a memory reference value, and describes the initial memory use condition so as to judge whether the memory is leaked or not subsequently. The sleep time of the memory leak detection thread can be set, so that the memory leak detection thread can call the thread sleep interface for memory detection at intervals. It should be noted that, the first memory detection value may be obtained by single detection, or may be an average value of memory detection values obtained by multiple detection.
The following steps 300 to 500 are repeatedly performed until the set time threshold is reached:
step 300, controlling a plurality of the resource using threads to execute a second round of switching.
The electronic equipment controls a plurality of the resource using threads to repeatedly execute round-robin switching, namely second round-robin switching, and a thread dispatcher in an operating system of the electronic equipment switches the use right of the CPU from one thread to another thread in the plurality of resource using threads according to a certain rule again, so that different threads in the plurality of resource using threads can be executed alternately.
Step 400, controlling the memory leak detection thread to perform second memory detection to obtain a second memory detection value.
The memory leak detection thread is used for performing memory detection after the plurality of resource use threads perform the round-robin switching again. The memory leakage detection thread can realize the detection of the memory once every a period of time, and the combination of the obtained second memory detection value and the first memory detection value is used for subsequently judging whether the memory is leaked or not, and the real-time judgment of whether the memory is leaked or not is realized by comparing the continuously updated second memory detection value with the first memory detection value. It should be noted that the second memory detection value may be obtained by single detection, or may be an average value of memory detection values obtained by multiple detection.
Step 500, determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
And the electronic equipment determines a memory leakage detection result based on the comparison result of the first memory detection value and the second memory detection value. For example, when the difference between the second memory detection value and the second memory detection value is greater than or equal to a set threshold (the set threshold is greater than 0), it is indicated that the system memory is not effectively released or recycled at this time, and the memory leak is determined at this time.
According to the embodiment of the invention, the memory leakage detection is carried out by repeatedly comparing the memory detection values of the plurality of resource use threads after the rotation switching within the set time threshold, and the repeated switching is carried out within a short time through the threads, so that each instruction in the function can be possibly switched, and various switching scenes in the actual use condition are reproduced more densely. In reality, each thread is switched at different time points, and it is not known in which switching scenario memory leakage may occur. If the scene is hard to reproduce, for example, the occurrence probability is small, the memory leak can be found in a plurality of months or even a plurality of years. According to the embodiment of the invention, through the short-time sheet rotation switching of the threads, the resources are acquired and released, the possible leakage at each time point can be accurately detected in a short time, and the leakage condition of the device memory hung for a plurality of days (for example, 15 days) in the long-time on-site before can be reproduced in a short time (within one hour). Namely, the embodiment of the invention can quickly and accurately realize memory leak detection.
Specifically, in one embodiment, the first memory detection value is a first memory usage value, the second memory detection value is a second memory usage value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes: and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
The first memory use value indicates that the memory leak detection thread obtains the use condition of the system memory when running for the first time, if the memory used size is M Bytes; and the second memory detection value represents that after the memory leak detection thread sleeps for a period of time, the use condition of the system memory is acquired again, if the memory has been used to be L Bytes, whether the memory leaks or not is judged through the comparison of L and M.
When the memory leak occurs, the second memory usage value is far different from the first memory usage value, i.e. the difference between the second memory usage value and the first memory usage value reaches a larger value. Since there is a difference in memory resources required by the program at different points in time between the first round-robin and the second round-robin of the plurality of resource-using threads. In order to distinguish the difference between the second memory usage value and the first memory usage value caused by the memory leakage and the memory resource difference required by the program at different time points, in the embodiment of the invention, the memory leakage is determined to occur under the condition that the second memory usage value is greater than the first memory usage value. When the second memory usage value is larger than the first memory usage value by a times, the second memory usage value is considered to be far different from the first memory usage value, the influence of memory resource differences required by programs at different time points is eliminated, and the occurrence of memory leakage can be judged. Wherein a may be various integers greater than 1, such as 2,3, etc.
According to the embodiment of the invention, under the condition that the second memory use value is larger than the first memory use value, the occurrence of memory leakage is determined, the influence of memory resource differences required by programs at different time points is eliminated during memory leakage detection, and the accuracy of memory leakage detection is further improved.
In yet another embodiment, the first memory test value is a first memory remaining value, the second memory test value is a second memory remaining value, and determining the memory leak test result based on a comparison of the first memory test value and the second memory test value includes: and determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
The first memory use value indicates that the memory leakage detection thread obtains the residual condition of the system memory when running for the first time, and if the residual size of the memory is M Bytes; and the second memory detection value represents that after the memory leakage detection thread sleeps for a period of time, the residual condition of the system memory is obtained again, if the residual size of the memory is L Bytes, whether the memory is leaked or not is judged through the comparison of L and M.
Similarly, in order to distinguish the difference between the second memory residual value and the first memory residual value caused by the memory leakage and the memory resource difference required by the program at different time points, in the embodiment of the invention, the memory leakage is determined to occur under the condition that the second memory residual value is equal to the first memory residual value. When the first memory residual value is larger than the second memory residual value by a times, the second memory residual value is considered to be far different from the first memory residual value, the influence of memory resource differences required by programs at different time points is eliminated, and the occurrence of memory leakage can be judged at the moment. Wherein a may be various integers greater than 1, such as 2, 3, etc.
According to the embodiment of the invention, under the condition that the second memory residual value is less than the first memory residual value, the occurrence of memory leakage is determined, the influence of memory resource differences required by programs at different time points is eliminated during the memory leakage detection, and the accuracy of the memory leakage detection is further improved.
In yet another embodiment, the first memory detection value is a first memory usage rate, the second memory detection value is a second memory usage rate, and determining the memory leak detection result based on a comparison of the first memory detection value and the second memory detection value includes: and determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
The first memory utilization rate represents the utilization rate of the system memory obtained when the memory leak detection thread runs for the first time, and if the memory utilization rate is M%; and the second memory detection value represents that after the memory leak detection thread sleeps for a period of time, the utilization rate of the system memory is acquired again, if the residual size of the memory is L, whether the memory leaks or not is judged through the comparison of L and M.
Similarly, in order to distinguish the difference between the second memory usage rate and the first memory usage rate caused by the memory leakage and the memory resource difference required by the program at different time points, in the embodiment of the present invention, the memory leakage is determined to occur when the second memory usage rate > a is equal to the first memory usage rate. Namely, when the first memory usage rate is greater than A times of the second memory usage rate, the second memory usage rate is considered to be far away from the first memory usage rate, the influence of memory resource differences required by programs at different time points is eliminated, and the occurrence of memory leakage can be judged at the moment. Wherein a may be various integers greater than 1, such as 2, 3, etc.
According to the embodiment of the invention, under the condition that the second memory utilization rate is larger than the first memory utilization rate, the occurrence of memory leakage is determined, the influence of memory resource differences required by programs at different time points is eliminated during memory leakage detection, and the accuracy of memory leakage detection is further improved.
In summary, the embodiment of the invention repeatedly performs memory leakage detection on the memory detection values of the plurality of resource using threads after the rotation switching, and the embodiment of the invention can accurately detect the memory leakage possibly generated at each time point in a short time as the plurality of resource using threads circularly acquire and release the resources through the short time slice switching.
In other aspects of embodiments of the invention, the thread time slices for each of the resource-using threads are 1-10ms or one system clock tick.
In the embodiment of the invention, in order to realize the repeated switching of threads in a short time slice, each instruction in the function can be possibly switched, so that various switching scenes in the actual use condition are densely reproduced, and the memory leakage detection is convenient to comprehensively carry out.
In other aspects of embodiments of the invention, the number of the plurality of resource-using threads is greater than or equal to 3. In order to reproduce various switching scenarios in real use cases as much as possible, the number of the plurality of resource-use threads should be set to 3 or more. The resource using threads with the value more than or equal to 3 are set to repeatedly switch in a short time slice, so that the scene that each thread is switched at different time points in the actual situation is simulated, and the memory leakage detection can be more accurately carried out.
In other aspects of embodiments of the invention, the thread time slices for each of the resource-using threads are the same, or the thread time slices for each of the resource-using threads are different. For example, the resource-using threads include 3, and the thread time slices of the resource-using thread 1, the resource-using thread 2, and the resource-using thread 3 are each 1ms. Or the resource using threads comprise 3, the thread time slice of the resource using thread 1 is 1ms, the thread time slice of the resource using thread 2 is 3ms, and the thread time slice of the resource using thread 3 is 5ms. By setting that the thread time slices of each resource using thread are the same or the thread time slices of each resource using thread are different, the scene that each thread is switched at different time points in the actual situation is simulated, and memory leak detection is more accurately carried out.
In other aspects of the embodiments of the present invention, the operations for obtaining memory resources and releasing memory resources include any one of operations for memory application and memory release, operations for semaphore creation and semaphore deletion, operations for mutex creation and mutex deletion, operations for channel creation and channel deletion, operations for file system creation and file system deletion, operations for file system opening and file system closing, and operations for opening and closing devices.
I.e., the operations to acquire and release memory resources include operations to apply for memory and release memory (e.g., memory application uses a malloc or new function, and memory release uses a free or delete function), operations to create and delete semaphores (e.g., semaphores may be created by a seminit function, semaphores may be deleted by a semdist function), operations to create and delete mutex (e.g., mutex may be created by a pthread mutex init function), operations to create and delete channels (e.g., channels are created by a mq open function, delete channels are deleted by a mq_unlink function), operations to create directories and delete file systems (e.g., file system create directories are created by a mkdir command, file system delete commands are used), operations to create files and file system files (e.g., mutex is created by a mutex command), operations to open and file system delete devices (e.g., file system open and file system provide the files by a closed system provided by a function provided by an open function, or the open function provided by a closed system provided by a device). According to the embodiment of the invention, through setting the operations of acquiring the memory resources and releasing the memory resources of each resource using thread, any operation can be included, and various switching scenes in the actual situation can be simulated, so that the memory leakage detection can be more comprehensively carried out.
In other aspects of the embodiments of the present invention, the multiple resource-using threads perform the reentry operation through the same pair of the resource acquisition interface and the resource release interface, or the multiple resource-using threads perform the reentry operation through different pairs of the resource acquisition interface and the resource release interface.
The reentrant operation may be to set the entry functions of the n resource-using threads to be the same processing function, and the processing function calls the memory allocation and release operation pair. I.e. n resource usage threads repeatedly operate the resource acquisition interface and the resource release interface of the same pair. Or n resource using threads repeatedly operate the resource acquisition interfaces and the resource release interfaces of different pairs. For example, the resource usage thread 1 repeatedly operates the resource acquisition interface A1 and the resource release interface B1. The resource use thread 2 repeatedly operates the resource acquisition interface A2 and the resource release interface B2. It should be noted that, the modification of the application call to different resources may be selected by writing different application codes or using a manner of transferring parameters.
According to the embodiment of the invention, n resource using threads repeatedly operate the resource acquisition interfaces and the resource release interfaces of the same pair, so that whether the memory leakage exists in one interface can be checked most rapidly; the n threads are used for repeatedly operating the resource acquisition and release interfaces of different pairs, so that the actual use scene can be comprehensively simulated, and the situation of whether memory leakage exists when the memory resource is cross-called can be comprehensively detected.
In other aspects of embodiments of the present invention, the plurality of resource usage threads and the memory leak detection thread are respectively provided in an operating system, or the plurality of resource usage threads and the memory leak detection thread are respectively provided in an application program.
In the embodiment of the invention, the plurality of resource using threads and the memory leakage detecting threads can be used in the operating system of the electronic equipment to perform self-detection and self-recovery of the memory leakage of the operating system, and can also be used as an external application program to be downloaded into the operating system of the electronic equipment to perform the memory leakage detection, thereby realizing the widening of the use scene.
In other aspects of the embodiments of the present invention, referring to fig. 5, the method further includes:
step 600, executing any one of the operations of prompting exception, stopping system operation and automatic restarting under the condition that the occurrence of the memory leak condition is determined.
Under the condition that the memory leakage condition is determined, the electronic equipment can analyze and prompt abnormality by using a display tool (a display screen and the like), and can stop the system operation or execute automatic restarting so as to enable an external analysis personnel to analyze and judge the reason of the memory leakage, thereby realizing quick detection and memory leakage elimination.
In other embodiments, it is determined that there is a leak in the memory, the result may be printed out in order, the problem may be reported out, and the test may be ended, or the situation of each memory use may be analyzed and displayed by a display tool for analysis and determination by an external analyst.
Device embodiment
Referring to fig. 6, in another aspect, an embodiment of the present invention further provides a memory leak detection apparatus, including:
a first round robin switching module 601, configured to control a plurality of resource usage threads to perform a first round robin switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource;
The first memory detection module 602 is configured to control the memory leak detection thread to perform first memory detection, so as to obtain a first memory detection value;
a second round robin switching module 603, configured to control a plurality of the resource using threads to perform a second round robin switching;
a second memory detection module 604, configured to control the memory leak detection thread to perform a second memory detection, to obtain a second memory detection value;
a memory leak detection result determining module 605, configured to determine a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value;
the control module 606 is configured to control the second round robin switching module 603, the second memory detection module 604, and the memory leak detection result determining module 605 to repeatedly execute until a set time threshold is reached.
According to the embodiment of the invention, the memory leakage detection is carried out by repeatedly comparing the memory detection values of the plurality of resource using threads after the rotation switching, and the plurality of resource using threads can circularly acquire the resources and release the resources through the short-time slice switching.
Optionally, each of the resource usage threads has a thread time slice of 1-10ms or one system clock tick.
Optionally, the number of the plurality of resource-using threads is greater than or equal to 3.
Optionally, the thread time slices of each of the resource usage threads are the same, or the thread time slices of each of the resource usage threads are different.
Optionally, the operations of obtaining the memory resource and releasing the memory resource include: the method comprises any one of operations of memory application and memory release, operations of semaphore creation and semaphore deletion, operations of mutex creation and mutex deletion, operations of channel creation and channel deletion, operations of file system creation and file system deletion, operations of file system opening and file system closing, and operations of opening and closing devices.
Optionally, the multiple resource using threads perform reentry operation through the same pair of the resource obtaining interface and the resource releasing interface, or the multiple resource using threads perform reentry operation through different pairs of the resource obtaining interface and the resource releasing interface.
Optionally, the plurality of resource usage threads and the memory leak detection thread are respectively provided in an operating system, or the plurality of resource usage threads and the memory leak detection thread are respectively provided in an application program.
Optionally, the first memory detection value is a first memory usage value, the second memory detection value is a second memory usage value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
Optionally, the first memory detection value is a first memory remaining value, the second memory detection value is a second memory remaining value, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
Optionally, the first memory detection value is a first memory usage rate, the second memory detection value is a second memory usage rate, and determining the memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value includes:
And determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
Optionally, the apparatus further includes:
And the post-processing module is used for executing any one of the operations of prompting abnormality, stopping the system operation and automatically restarting under the condition of determining that the memory leakage condition occurs.
The memory leak detection apparatus includes a processor and a memory, where the first round switching module 601, the first memory detection module 602, the second round switching module 603, the second memory detection module 604, the memory leak detection result determination module 605, the control module 606, and the like are stored as program units in the memory, and the processor executes the program units stored in the memory to implement corresponding functions. Referring to fig. 7, the first round robin switching module and the second round robin switching module may be combined to be used as a resource obtaining module to repeatedly perform the memory resource obtaining and the memory resource releasing operations from the resource pool; the first memory detection module, the second memory detection module and the memory leakage detection result determining module can be combined to serve as a memory leakage detection module to realize memory leakage detection.
The processor includes a kernel, and the kernel fetches the corresponding program unit from the memory. The kernel may be provided with one or more.
The memory may include volatile memory, random Access Memory (RAM), and/or nonvolatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM), among other forms in computer readable media, the memory including at least one memory chip.
Fig. 8 illustrates a physical structure diagram of an electronic device, as shown in fig. 8, which may include: processor 810, communication interface (Communications Interface) 820, memory 830, and communication bus 840, wherein processor 810, communication interface 820, memory 830 accomplish communication with each other through communication bus 840. Processor 810 may invoke logic instructions in memory 830 to perform a memory leak detection method comprising: controlling a plurality of resource using threads to execute a first round switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource; controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the following steps are repeatedly executed until the set time threshold is reached: controlling a plurality of the resource using threads to execute second round switching; controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value; and determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
Further, the logic instructions in the memory 830 described above may be implemented in the form of software functional units and may be stored in a computer-readable storage medium when sold or used as a stand-alone product. Based on this understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a usb disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
In another aspect, the present invention also provides a computer program product comprising a computer program storable on a machine-readable storage medium, the computer program, when executed by a processor, is capable of performing a memory leak detection method, the method comprising: controlling a plurality of resource using threads to execute a first round switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource; controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the following steps are repeatedly executed until the set time threshold is reached: controlling a plurality of the resource using threads to execute second round switching; controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value; and determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
In yet another aspect, the present invention provides a machine-readable storage medium having stored thereon a computer program which when executed by a processor is implemented to perform a memory leak detection method, the method comprising: controlling a plurality of resource using threads to execute a first round switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource; controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the following steps are repeatedly executed until the set time threshold is reached: controlling a plurality of the resource using threads to execute second round switching; controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value; and determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
The apparatus embodiments described above are merely illustrative, wherein the elements illustrated as separate elements may or may not be physically separate, and the elements shown as elements may or may not be physical elements, may be located in one place, or may be distributed over a plurality of network elements. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art will understand and implement the present invention without undue burden.
From the above description of the embodiments, it will be apparent to those skilled in the art that the embodiments may be implemented by means of software plus necessary general hardware platforms, or of course may be implemented by means of hardware. Based on this understanding, the foregoing technical solution may be embodied essentially or in a part contributing to the prior art in the form of a software product, which may be stored in a computer readable storage medium, such as ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method described in the respective embodiments or some parts of the embodiments.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and are not limiting; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims (23)

1. A memory leak detection method, comprising:
Controlling a plurality of resource using threads to execute a first round switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource; the thread time slice of each resource using thread is 1-10ms or one system clock beat;
Controlling a memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the first memory detection value is used as a memory reference value to describe the most initial memory use condition;
the following steps are repeatedly executed until the set time threshold is reached:
Controlling a plurality of the resource using threads to execute second round switching;
Controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value;
and determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value.
2. The memory leak detection method of claim 1, wherein the number of the plurality of resource-using threads is greater than or equal to 3.
3. The memory leak detection method of claim 1, wherein the thread time slices for each of the resource usage threads are the same or the thread time slices for each of the resource usage threads are different.
4. The memory leak detection method as recited in claim 1, wherein the operations of obtaining memory resources and releasing memory resources include any one of operations of memory application and memory release, operations of semaphore creation and semaphore deletion, operations of mutex creation and mutex deletion, operations of channel creation and channel deletion, operations of file system creation directory and file system deletion directory, operations of file system creation file and file system deletion file, operations of file system opening file and file system closing file, and operations of opening device and closing device.
5. The memory leak detection method according to claim 1, wherein the plurality of resource usage threads perform a reentry operation through a same pair of the resource acquisition interface and the resource release interface, or the plurality of resource usage threads perform a reentry operation through different pairs of the resource acquisition interface and the resource release interface.
6. The memory leak detection method of claim 1, wherein the plurality of resource usage threads and the memory leak detection thread are provided in an operating system, respectively, or the plurality of resource usage threads and the memory leak detection thread are provided in an application, respectively.
7. The memory leak detection method of claim 1, further comprising:
And executing any one of the operations of prompting abnormality, stopping the system operation and automatically restarting under the condition that the occurrence of the memory leakage condition is determined.
8. The memory leak detection method of claim 1, wherein the first memory detection value is a first memory usage value and the second memory detection value is a second memory usage value, wherein determining the memory leak detection result based on a comparison of the first memory detection value and the second memory detection value comprises:
and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
9. The memory leak detection method of claim 1, wherein the first memory detection value is a first memory remaining value and the second memory detection value is a second memory remaining value, wherein determining the memory leak detection result based on a comparison of the first memory detection value and the second memory detection value comprises:
And determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
10. The memory leak detection method of claim 1, wherein the first memory detection value is a first memory usage rate and the second memory detection value is a second memory usage rate, and wherein determining the memory leak detection result based on the comparison of the first memory detection value and the second memory detection value comprises:
And determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
11. A memory leak detection apparatus, comprising:
the first round-robin switching module is used for controlling a plurality of resource using threads to execute first round-robin switching; each resource using thread is used for repeatedly executing the operations of acquiring the memory resource and releasing the memory resource; the thread time slice of each resource using thread is 1-10ms or one system clock beat;
the first memory detection module is used for controlling the memory leakage detection thread to perform first memory detection to obtain a first memory detection value; the first memory detection value is used as a memory reference value to describe the most initial memory use condition;
The second round-robin switching module is used for controlling a plurality of the resource using threads to execute second round-robin switching;
the second memory detection module is used for controlling the memory leakage detection thread to perform second memory detection to obtain a second memory detection value;
The memory leak detection result determining module is used for determining a memory leak detection result based on a comparison result of the first memory detection value and the second memory detection value;
and the control module is used for controlling the second rotation switching module, the second memory detection module and the memory leakage detection result determining module to repeatedly execute until reaching a set time threshold.
12. The memory leak detection apparatus of claim 11, wherein the number of the plurality of resource-using threads is greater than or equal to 3.
13. The memory leak detection apparatus of claim 11, wherein the thread time slices for each of the resource usage threads are the same or the thread time slices for each of the resource usage threads are different.
14. The memory leak detection apparatus of claim 11, wherein the operations of acquiring memory resources and releasing memory resources comprise: the method comprises any one of operations of memory application and memory release, operations of semaphore creation and semaphore deletion, operations of mutex creation and mutex deletion, operations of channel creation and channel deletion, operations of file system creation and file system deletion, operations of file system opening and file system closing, and operations of opening and closing devices.
15. The memory leak detection apparatus of claim 11, wherein the plurality of resource usage threads perform a reentry operation through a same pair of resource acquisition interface and resource release interface, or wherein the plurality of resource usage threads perform a reentry operation through a different pair of resource acquisition interface and resource release interface.
16. The memory leak detection apparatus of claim 11, wherein the plurality of resource usage threads and the memory leak detection thread are provided in an operating system, respectively, or the plurality of resource usage threads and the memory leak detection thread are provided in an application, respectively.
17. The memory leak detection apparatus of claim 11, wherein the apparatus further comprises:
And the post-processing module is used for executing any one of the operations of prompting abnormality, stopping the system operation and automatically restarting under the condition of determining that the memory leakage condition occurs.
18. The memory leak detection apparatus of claim 11, wherein the first memory detection value is a first memory usage value and the second memory detection value is a second memory usage value, wherein determining the memory leak detection result based on a comparison of the first memory detection value and the second memory detection value comprises:
and determining that the memory leak occurs under the condition that the second memory use value is larger than A and is equal to the first memory use value, wherein A is an integer larger than 1.
19. The memory leak detection apparatus of claim 11, wherein the first memory detection value is a first memory remaining value and the second memory detection value is a second memory remaining value, wherein determining the memory leak detection result based on a comparison of the first memory detection value and the second memory detection value comprises:
And determining that the memory leak occurs under the condition that the second memory residual value is A < the first memory residual value, wherein A is an integer greater than 1.
20. The memory leak detection apparatus of claim 11, wherein the first memory detection value is a first memory usage rate and the second memory detection value is a second memory usage rate, wherein determining the memory leak detection result based on the comparison of the first memory detection value and the second memory detection value comprises:
And determining that the memory leak occurs under the condition that the second memory utilization rate is larger than A and equal to the first memory utilization rate, wherein A is an integer larger than 1.
21. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the memory leak detection method of any one of claims 1 to 10 when the program is executed by the processor.
22. A machine readable storage medium having stored thereon a computer program, which when executed by a processor implements the memory leak detection method of any of claims 1 to 10.
23. A computer program product comprising a computer program, characterized in that the computer program, when executed by a processor, implements the memory leak detection method of any one of claims 1 to 10.
CN202410576360.XA 2024-05-10 2024-05-10 Memory leakage detection method and device and electronic equipment Active CN118132404B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410576360.XA CN118132404B (en) 2024-05-10 2024-05-10 Memory leakage detection method and device and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410576360.XA CN118132404B (en) 2024-05-10 2024-05-10 Memory leakage detection method and device and electronic equipment

Publications (2)

Publication Number Publication Date
CN118132404A CN118132404A (en) 2024-06-04
CN118132404B true CN118132404B (en) 2024-07-23

Family

ID=91242212

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410576360.XA Active CN118132404B (en) 2024-05-10 2024-05-10 Memory leakage detection method and device and electronic equipment

Country Status (1)

Country Link
CN (1) CN118132404B (en)

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105373479A (en) * 2015-12-14 2016-03-02 厦门美图移动科技有限公司 Automatic test method and device of memory leakage and mobile terminal

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060129992A1 (en) * 2004-11-10 2006-06-15 Oberholtzer Brian K Software test and performance monitoring system
US9454454B2 (en) * 2014-09-09 2016-09-27 Microsoft Technology Licensing, Llc Memory leak analysis by usage trends correlation
CN104866409A (en) * 2015-05-29 2015-08-26 小米科技有限责任公司 Method and apparatus for monitoring memory leakage
CN111240954A (en) * 2018-11-29 2020-06-05 华为技术有限公司 Memory leak detection method, device and equipment
US11169837B2 (en) * 2019-08-23 2021-11-09 Red Hat, Inc. Fast thread execution transition
CN111209194B (en) * 2019-12-30 2023-12-19 杭州安恒信息技术股份有限公司 Test case design method for finding memory leakage bug
CN116680161B (en) * 2022-10-10 2024-06-07 荣耀终端有限公司 Memory leak detection method, readable medium and electronic device

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105373479A (en) * 2015-12-14 2016-03-02 厦门美图移动科技有限公司 Automatic test method and device of memory leakage and mobile terminal

Also Published As

Publication number Publication date
CN118132404A (en) 2024-06-04

Similar Documents

Publication Publication Date Title
US7908521B2 (en) Process reflection
CN106681811B (en) Multithreading scheduling method and device based on thread pool
KR101174751B1 (en) Malware auto-analysis system and method using kernel call-back mechanism
US7991961B1 (en) Low-overhead run-time memory leak detection and recovery
US10599558B1 (en) System and method for identifying inputs to trigger software bugs
CN110363004B (en) Code vulnerability detection method, device, medium and equipment
CN108508874B (en) Method and device for monitoring equipment fault
CN112445686A (en) Memory leak detection method, device and computer-readable storage medium
CN110704313A (en) Memory leakage detection method and device for JAVA virtual machine
CN111258591A (en) Program deployment task execution method and device, computer equipment and storage medium
CN113157347A (en) Automatic probe deployment method, electronic device and storage medium
CN113672273B (en) Data processing method, system and equipment
CN118132404B (en) Memory leakage detection method and device and electronic equipment
CN116414722B (en) Fuzzy test processing method and device, fuzzy test system and storage medium
CN106909509A (en) A kind of virtual machine process code without acting on behalf of paging type gauging system and method
CN118567971A (en) Method for testing a computer program
KR102141620B1 (en) Method and Apparatus for detecting atomicity violation for shared memory used in multi-process/multi-thread
CN115934276A (en) Memory management method and device and electronic equipment
CN106407016B (en) Method and device for simulating multithreading contention and robbery of resources
Santhanakrishnan et al. Memory leak detection in android applications based on code patterns
US20120054723A1 (en) Dynamic instrumentation method and apparatus thereof
CN114253825A (en) Memory leak detection method and device, computer equipment and storage medium
US9122796B2 (en) Recreating timing issues during program debug
CN111858075A (en) Deadlock detection method, system, equipment and readable storage medium
KR101306656B1 (en) Apparatus and method for providing dynamic analysis information of malignant code

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant