CN115794553A - Memory leak detection method, device, equipment and medium - Google Patents

Memory leak detection method, device, equipment and medium Download PDF

Info

Publication number
CN115794553A
CN115794553A CN202211535480.2A CN202211535480A CN115794553A CN 115794553 A CN115794553 A CN 115794553A CN 202211535480 A CN202211535480 A CN 202211535480A CN 115794553 A CN115794553 A CN 115794553A
Authority
CN
China
Prior art keywords
memory
data volume
determining
unreleased
leak detection
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.)
Pending
Application number
CN202211535480.2A
Other languages
Chinese (zh)
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.)
Zhejiang Dahua Technology Co Ltd
Original Assignee
Zhejiang Dahua 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 Zhejiang Dahua Technology Co Ltd filed Critical Zhejiang Dahua Technology Co Ltd
Priority to CN202211535480.2A priority Critical patent/CN115794553A/en
Publication of CN115794553A publication Critical patent/CN115794553A/en
Pending legal-status Critical Current

Links

Images

Abstract

The application provides a memory leak detection method, a device, equipment and a medium, which relate to the technical field of data processing, and the method comprises the following steps: receiving a query request of a user, and acquiring current memory state information from a memory use condition record file corresponding to a query process when determining that a current memory leak detection function is started; screening out a target memory segment related to memory leakage from the memory state information, and determining the data volume of the unreleased memory in the current memory based on the target memory segment; and comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leaks currently. The method can accurately detect the possible memory leakage problem in the query process on the premise of not influencing the normal operation of the query.

Description

Memory leak detection method, device, equipment and medium
Technical Field
The present application relates to the field of data processing technologies, and in particular, to a method, an apparatus, a device, and a medium for detecting memory leak.
Background
In order to better manage the use of memory resources, a database always realizes a set of memory management mechanisms based on memory related functions (such as malloc, realloc, free in a C standard library) of a system bottom layer, wherein a set of memory allocation mechanisms of memory context exist in a greenplus distributed database, and memory fragmentation and unexpected memory leakage conditions can be effectively reduced through the memory management mechanisms.
However, even if a memory management mechanism exists, there may be memory leaks. In the database development process, because one Memory Context in the Memory allocation mechanism of the Memory Context may have a plurality of associated sub-Memory contexts, after the query process corresponding to each query request in the query process is completed, a situation that a Memory associated with a certain Memory Context is not released easily occurs, and at this time, a Memory leak occurs.
At present, when a database connection pool is commonly applied, if there is a Memory leak, the Memory occupied by a long connection process which is continuously running will continuously increase, and finally, an OOM (Out-of-Memory) Killer may even be caused, which affects the stable running of the database. Therefore, a method for detecting Memory leaks by a Memory Context Memory allocation mechanism is needed.
Disclosure of Invention
The invention provides a Memory leak detection method, which is used for detecting the Memory leak problem possibly occurring in a Memory Context Memory allocation mechanism.
In a first aspect, an embodiment of the present application provides a memory leak detection method, which is applied to an inquiry process, and the method includes:
receiving a query request of a user, and acquiring current memory state information from a memory use condition record file corresponding to a query process when determining that a current memory leakage detection function is started;
screening out a target memory segment related to memory leakage from the memory state information, and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
and comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leaks currently, wherein the reference data volume is determined according to the data volume of the memory when the memory leaks.
In the method, a target memory segment related to memory leakage is screened out from the memory state information, and the data volume of the unreleased memory in the current memory is determined based on the target memory segment; and comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leakage exists currently, and accurately detecting the memory leakage problem possibly existing in the query process on the premise of not influencing the normal operation of the query.
An optional implementation manner is that, comparing the data amount of the unreleased memory with the reference data amount, and determining whether there is a memory leak currently, the method further includes:
determining whether the memory leak detection is carried out for the first time at present according to a first variable for recording the memory leak detection times in the query process;
when determining that memory leak detection is carried out for the first time, determining a reference data volume according to the data volume of the unreleased memory;
and when the memory leak detection is determined not to be carried out for the first time, the step of comparing the data volume of the unreleased memory with the reference data volume is carried out.
According to the method, whether the memory leak detection is carried out for the first time or not is determined, and when the memory leak detection is carried out for the first time, the reference data volume is determined according to the data volume of the unreleased memory, so that the accuracy of the determined reference data volume can be ensured, and the judgment result of the subsequent memory leak is more accurate.
An optional implementation manner is that, before obtaining the current memory state information from the memory usage record file corresponding to the query process, the method further includes:
determining the corresponding times of the currently received query request in the query process;
and determining that the preset memory leak detection period is reached according to the corresponding times of the currently received query request in the query process.
According to the method, the memory leak detection period is set, whether the memory leak detection period is reached is determined before the current memory state information is acquired from the memory use condition record file corresponding to the query process, and the memory leak detection is performed only when the memory leak detection period is reached, so that the execution of the memory leak detection is more flexible.
An optional implementation manner is that a global variable for recording the number of times of receiving a query request is set in a query process, an initial value of the global variable is 0, and a numerical value of the current global variable is increased by 1 each time a query request of a user is received.
According to the method, the global variable is set in the query process, and the times of the received query requests are recorded through the global variable, so that whether the preset memory leak detection period is reached or not can be conveniently determined according to the times of the query requests, and the scheme is more convenient to implement in real time.
An optional implementation manner is that, screening out a target memory segment related to memory leakage from the memory state information, and determining the data amount of the unreleased memory in the current memory based on the target memory segment, including:
screening a stack memory segment corresponding to a brk function and an anonymous memory segment corresponding to a mmap function from the memory state information, wherein the brk function and the mmap function are calling functions corresponding to the query request;
and summing the physical memory values respectively recorded in the stack memory segment and the anonymous memory segment, removing the cache data volume in the current memory, and determining the data volume of the unreleased memory in the current memory.
According to the method, the stack memory segment and the anonymous memory segment are screened from the memory state information, the physical memory values recorded in the stack memory segment and the anonymous memory segment are summed, the cache data volume in the current memory is removed, and the data volume of the unreleased memory in the current memory is determined, so that the scheme is more specific, and the accuracy of the determined data volume of the unreleased memory in the current memory is ensured.
An optional implementation manner is that the comparing the data amount of the unreleased memory with the reference data amount to determine whether there is a memory leak currently includes:
calculating the difference between the data quantity of the unreleased memory and the reference data quantity;
and when the difference is determined to be larger than the preset threshold value, determining that the memory leakage exists currently.
An optional implementation manner is that, when it is determined that there is a memory leak currently, the method further includes:
processing the memory leakage based on a preset processing strategy corresponding to the memory leakage;
wherein the processing strategy comprises at least one of log generation, reminding, alarming, error throwing and query process termination.
In a second aspect, an embodiment of the present application provides a memory leak detection apparatus, including:
the acquisition unit is used for receiving a query request of a user and acquiring current memory state information from a memory use condition record file corresponding to a query process when the current memory leak detection function is determined to be started;
the screening unit is used for screening out a target memory segment related to memory leakage from the memory state information and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
and the determining unit is used for comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leakage exists currently, and the reference data volume is determined according to the data volume of the memory when the memory leakage does not occur.
In a third aspect, an embodiment of the present application provides a memory leak detection apparatus, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements any step of the memory leak detection method in the first aspect when executing the computer program.
In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which computer program instructions are stored, where the computer program instructions, when executed by a processor, implement any step of the memory leak detection method in the first aspect.
In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, where the computer program is stored in a computer-readable storage medium;
when the processor of the memory access device reads the computer program from the computer-readable storage medium, the processor executes the computer program, so that the memory access device performs any one of the steps of the memory leak detection method in the first aspect described above.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
Fig. 1 is a schematic diagram of a Memory Context Memory allocation mechanism provided in an embodiment of the present application;
fig. 2 is a schematic diagram of an application scenario provided in an embodiment of the present application;
fig. 3 is a schematic flowchart of a memory leak detection method according to an embodiment of the present disclosure;
fig. 4 is a schematic flowchart illustrating a method for determining whether a memory leak detection period is satisfied according to an embodiment of the present disclosure;
fig. 5 is a schematic flowchart of a first memory leak detection and determination method according to an embodiment of the present disclosure;
fig. 6 is a schematic flowchart of a memory leak detection process according to an embodiment of the present disclosure;
fig. 7 is a schematic diagram of a memory leak detection apparatus according to an embodiment of the present disclosure;
fig. 8 is a schematic diagram of a memory leak detection apparatus according to an embodiment of the present disclosure.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention will be described in further detail with reference to the accompanying drawings.
The application scenario described in the embodiment of the present application is for more clearly illustrating the technical solution of the embodiment of the present application, and does not form a limitation on the technical solution provided in the embodiment of the present application, and it can be known by a person skilled in the art that with the occurrence of a new application scenario, the technical solution provided in the embodiment of the present application is also applicable to similar technical problems. In the description of the present application, the term "plurality" means two or more unless otherwise specified.
It should be noted that the terms "first," "second," and the like in the description and claims of this application and in the drawings described above are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the invention described herein are capable of operation in sequences other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims. All other embodiments obtained by a person of ordinary skill in the art based on the embodiments in the present application without making any creative effort belong to the protection scope of the present application.
In order to better manage the use of Memory resources, a database always realizes a set of Memory management mechanisms based on Memory related functions (such as malloc, realloc and free in a C standard library) of a system bottom layer, for example, a set of mem _ root/buffer _ pool mechanism is realized by opening a source database MySQL, and a set of Memory Context mechanism is realized by opening a source database PostgreSQL. However, even if a memory management mechanism exists, there may be a memory leak condition. At present, when a database connection pool is commonly applied, if there is a Memory leak, the Memory occupied by a long connection process which is continuously running will continuously increase, and finally, an OOM (Out-of-Memory) Killer may even be caused, which affects the stable running of the database.
The embodiment of the application provides a Memory leak detection method based on an open-source distributed database Greenplus, wherein the Greenplus database runs on a Linux platform and uses a PostgreSQL kernel, so that a Memory management mechanism used by the database is a Memory Context Memory allocation mechanism.
The working principle of the Memory Context Memory allocation mechanism in the greenplus database is described as follows:
in the Memory Context Memory allocation mechanism, one Memory Context is responsible for managing allocation and release of a data block and chunk data, wherein one Memory Context may be associated with a plurality of sub-Memory contexts. As shown in FIG. 1, a memory context 101 may be associated with a sub-memory context 111 and a sub-memory context 112.
The operations of the Memory are realized by calling related functions (such as palloc, pfree and the like) in a Greenplus database, and the functions are essentially encapsulation of Memory Context operations. The database has a global pointer Current Memory Context, which points to the Memory Context operated by the functions such as Current palloc (for calling a malloc function to allocate a new Memory block), pfree (for calling a free function to release the Memory block), and repalloc (). In implementation, the Memory Context to be operated by the current function is set by setting the pointing of the global pointer.
In the process of developing the database, a developer can freely Switch the Memory Context pointed by the global pointer Current Memory Context by using a Memory Context Switch To () function and freely create a new Memory Context. The database has a Memory Context structure which is pre-created and has a relatively large relation with query execution as follows:
under the Top Memory Context, a Message Context, a Portal Memory, a Cache Context and a Top Transaction Context are associated.
Portal Context is associated under the Portal Context, executo State Query Context is associated under the Portal Context, per-Row/other contexts are associated under the Executo State Query Context, and the like.
In one specific example, a developer may create a Test Context whose parent is a Top Memory Context. Then, calling a Memory Context Switch To () function To make the global pointer point To the Test Context, starting To allocate and use the Memory, after using, switching back To the original Context, and finally resetting (releasing) the Test Context.
When a Memory Context is released, it and all the Memory blocks associated with all its child Memory Contexts are released. By the method, even if the Memory is allocated but the pfree function is not used for releasing the Memory subsequently, the Memory cannot be influenced too much, because the Memory corresponding to the Memory Context associated with the Memory Context is automatically released when the Memory associated with the Memory Context is associated with the Memory Context.
Although, in a long connection of a Query process, the contexts, such as Message Context, top Transaction Context, error Context, and execute State Per-Query, are automatically released after each Query is completed, and the associated memory can be normally released. However, sometimes the Memory Context used in the query process is not limited to the above Context, but other types of contexts, such as Top Memory Context, may also be used, and the Memory allocated on these contexts is not automatically released, so the following situations may be encountered:
some database extension plug-ins or extension functions may create a child Memory Context from a Top Memory Context, but not actively reset it in time after use.
Some functions used in the query process internally use other Memory Context by default, such as a hash _ create () function may be used to create a hash table, but internally use Top Memory Context by default.
Because the pointing of the Current Memory Context is written by the developer, there may be unexpected coding errors that cause the Current Memory Context pointing to be inaccurate.
Part of developers may directly use the C-standard library malloc function in the writing process.
Therefore, even if a perfect Memory Context mechanism exists, databases such as greenplus/PostgreSQL can still suffer from Memory leakage, and in a long connection session of the query process, if the query process is not exited all the time, even if the valgrind Memory leakage detection support is built in, the database is difficult to use, and if only one Memory leakage exists, the leaked Memory will be expanded continuously after long-term operation is accumulated. The embodiment of the present application provides a method for detecting memory leak.
Fig. 2 is a schematic diagram of an application scenario provided by an embodiment of the present application, and as shown in fig. 2, the memory leak detection method provided by the embodiment of the present application is applied to a long session of an inquiry process, in the long session of the inquiry process, an inquiry request of a user is received and processed without stopping, and the inquiry process is cyclically and repeatedly located between a running state (that is, an inquiry is performed) and an idle state (that is, an inquiry operation is finished and a next inquiry request is waited to be received).
Fig. 3 is a schematic flowchart of a memory leak detection method according to an embodiment of the present disclosure; as shown in fig. 3, an embodiment of the present application provides a memory leak detection method, which is applied to a query process (for example, the query process long session shown in fig. 2), and the method specifically includes the following steps:
step 301, receiving a query request of a user, and acquiring current memory state information from a memory use condition record file corresponding to a query process when determining that a current memory leak detection function is started;
step 302, screening out a target memory segment related to memory leakage from the memory state information, and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
step 303, comparing the data amount of the unreleased memory with a reference data amount to determine whether a memory leak exists currently, wherein the reference data amount is determined according to the data amount of the memory when the memory leak does not occur.
In the method, a target memory segment related to memory leakage is screened out from the memory state information, and the data volume of the unreleased memory in the current memory is determined based on the target memory segment; and comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leaks currently. The method aims at the possible memory leakage of the long and medium connection running session of the open source distributed database Greenplus, and can accurately judge the possible memory leakage problem of the process on the premise of not influencing the normal running of the query through the memory use information provided by the operating system. When the method is used in a long-term running business system, the problem of memory leakage suffered can be timely found by combining with regular log inspection, and a database developer can conveniently locate the root cause.
In the implementation of step 301, after receiving a query request from a user and completing the query, first determining whether a current memory leak detection function is turned on, and executing a subsequent step of detecting memory leak when determining that the function is turned on; and when the function is determined not to be started, the subsequent step of detecting the memory leakage is not executed. The memory leak detection function may be turned on or off by the user, or may be set to be turned on for a long time or turned on for a fixed time period by default.
Specifically, the Configuration of the entire distributed cluster may be uniformly controlled using a GUC (Grand Unified Configuration, database Configuration parameter) variable. The GUC variable includes a variable mem _ monitor _ enable used for identifying whether a memory leak detection function is started, and specifically, the variable mem _ monitor _ enable may be set as follows: when the variable value of the variable mem _ monitor _ enable is off, the memory leak detection function is identified to be off, when the variable value of the variable mem _ monitor _ enable is on, the memory leak detection function is identified to be on, after receiving a query request of a user, whether the current memory leak detection function is on or not is judged by determining whether the variable value of the variable mem _ monitor _ enable is on or off, and when the function is determined to be on, a subsequent step of detecting the memory leak is executed.
In some embodiments, after it is determined that the current memory leak detection function is turned on, before the current memory state information is obtained from the memory usage record file corresponding to the query process, it is further required to determine whether the query request received at the present time meets a preset memory leak detection period, as shown in fig. 4, the determination process specifically includes the following steps:
step 401, determining the corresponding times of the currently received query request in the query process;
the query process is provided with a global variable mem _ monitor _ cnt for recording the number of times of receiving the query request, the initial value of the global variable mem _ monitor _ cnt is 0, the value of the current global variable mem _ monitor _ cnt is increased by 1 every time a query request of a user is received, and the currently received query request can be determined to be the request received in the query process for the second time through the variable value of the global variable mem _ monitor _ cnt.
Step 402, determining whether a preset memory leak detection period is reached; if yes, go to step 403, otherwise go to step 404.
In this embodiment of the present application, the GUC variables further include a variable mem _ monitor _ interval, where the variable is used to identify a detection interval of memory leak, and if a variable value of the variable is 0, it indicates that a memory leak check is performed every time the query process enters an idle state; if the number of times is larger than 0, the memory leak check is performed after the interval specified times enters the idle state. For example, when the variable value of the variable is 2, the memory leak check is performed after entering the idle state every 2 times.
Since the variable mem _ monitor _ interval records the detection interval of the memory leak, and the global variable also records the number of times of receiving the query request, it can be determined whether the preset memory leak detection period is reached simply by mod operation, and the specific calculation method is as follows: when mod (mem _ monitor _ cnt, mem _ monitor _ interval + 1) =0, it is determined that the preset memory leak detection period is reached, otherwise, it is determined that the preset memory leak detection period is not reached.
Step 403, acquiring current memory state information from the memory use condition record file corresponding to the query process;
in some embodiments, the memory usage record file is a maps file, and the file records a detailed memory usage of the query process, where the memory usage record file includes a plurality of records, and each record corresponds to a continuous area in a virtual memory space in the query process. The Smps file records a file mapping virtual memory segment of a binary system/dynamic library, a [ heap ] pile virtual memory segment, a [ stack ] stack virtual memory segment, an anonymous block data virtual memory segment and the like, the attribute of each virtual memory segment can be shared or private, and each virtual memory segment also comprises a start address and an end address.
Therefore, in the embodiment of the application, the current memory state information is obtained from the maps file, and the target memory segment related to the memory leakage is further screened out.
Step 404, the subsequent memory leak detection step is not performed.
In the step 302, after the current memory state information is obtained from the memory usage record file corresponding to the query process, since there are many types of memory state information, a target memory segment related to memory leakage needs to be screened from the memory state information, that is, a target memory segment related to memory leakage is screened from the memory state information by traversing the maps file.
The target memory segment refers to a stack memory segment private [ heap ] and an anonymous memory segment private anonymous, wherein the private [ heap ] memory segment basically corresponds to a brk function, the private anonymous memory segment basically corresponds to a mmap function, and the brk function and the mmap function are bottom layer functions which are called by the malloc function and are associated with memory allocation in a query process after a query request is received.
After the target memory segment is screened out, the data volume of the unreleased memory in the current memory is determined based on the target memory segment, namely, the physical memory values respectively recorded in the stack memory segment and the anonymous memory segment are summed, the cache data volume in the current memory is removed, and the data volume of the unreleased memory in the current memory is determined.
Specifically, since the value obtained by accumulating the Private _ Dirty values and the Private _ Clean values in the Private [ flap ] Memory segment and the Private anonymous Memory segment is approximately equal to the size of the physical Memory actually used in the Memory Context mechanism, after the Private [ flap ] Memory segment and the Private anonymous Memory segment are obtained, the Private _ Dirty values and the Private _ Clean values in the Memory segment are summed, and the current data size of the current Memory can be obtained.
However, in the obtained current data size, the data size corresponding to the partial Memory Context is required to be excluded. In the embodiment of the application, a Cache (Cache) Memory Context is used for storing a relcace and a Cache which are scanned by a current process for multiplexing, if a large number of tables are scanned by the process, the size of a Memory associated with the Cache Memory Context may be continuously increased, and on a certain program, the Memory associated with the Cache Memory Context, which is continuously increased and is not released, also belongs to Memory leakage.
It should be noted that the data amount mem _ total is only the data amount of the unreleased physical memory in the current memory, and does not include the data amount of the virtual memory. For example, if a memory applies for 1000 virtual memory pages and only 10 pages of the 1000 virtual memory pages are read and written, only 10 pages of physical memory are considered to be used.
In the step 303, after determining the data amount of the unreleased memory in the current memory, before comparing the data amount of the unreleased memory with the reference data amount, it is further required to determine whether to perform the memory leak detection for the first time, as shown in fig. 5, which specifically includes the following steps:
step 501, determining whether memory leak detection is performed for the first time at present according to a first variable of the memory leak detection times recorded in the query process; if yes, go to step 502, otherwise go to step 503.
In some embodiments, a first variable that records the number of times of memory leak detection is set in the query process, and different variable values of the first variable identify the number of times of memory leak detection, where the first variable may be set to correspond to different variable values when performing first memory leak detection and non-first memory leak detection, and the first variable may also be set to correspond to different variable values when performing different memory leak detection.
Step 502, determining a reference data volume according to the data volume of the unreleased memory;
and determining to perform memory leak detection for the first time according to the first variable, and determining the currently determined data quantity mem _ total of the unreleased memory as the reference data quantity mem _ monitor _ init _ use.
In step 503, the step of comparing the data amount of the unreleased memory with the reference data amount is performed.
Specifically, a difference between the data amount mem _ total of the unreleased memory and the reference data amount mem _ monitor _ init _ use is calculated, and when the difference is determined to be greater than a preset threshold, it is determined that the memory leak exists currently.
The preset threshold is an allowable error mem _ monitor _ delta between the data amount of the unreleased memory and the reference data amount, which is preset by the user, and the size of the allowable error mem _ monitor _ delta can be set by the user, for example, set to 150MB.
In the embodiment of the application, the data volume of the unreleased memory is compared with the reference data volume, and when the memory leakage is determined to exist currently, the memory leakage is processed based on a preset processing strategy (namely a variable value corresponding to a variable mem _ monitor _ alert _ level in a GUC variable) corresponding to the memory leakage;
the processing strategy comprises at least one of LOG LOG generation, NOICE reminding, WARNING alarming, ERROR throwing and FATAL terminating and inquiring.
The variable value corresponding to the variable mem _ monitor _ alert _ level can be set by a user before creating the query process long connection.
It should be noted that, although in normal conditions, the query process long connection running session only appears on the MASTER segment, the query process corresponding to the QE segment automatically exits if it is idle for a certain time. However, if the queries are very frequent, the query process on the QEs will not be idle for a sufficient amount of time, possibly resulting in the query process not being able to exit. Therefore, in the embodiment of the application, in the greenplus distributed database, an independently operating Memory Monitor module is added to each query process in all segments to execute the Memory leak detection method, so as to realize real-time detection of the Memory leak problem.
Fig. 6 is a schematic flow chart of a memory leak detection process provided in an embodiment of the present application, and a specific implementation process of the memory leak detection method is described in detail below with reference to fig. 6, as shown in fig. 6, the process includes the following steps:
step 601, receiving a query request.
Step 602, determining whether the current memory leak detection function is turned on; if yes, go to step 603, otherwise return to step 601.
Step 603, determining whether a preset memory leak detection period is reached; if yes, go to step 604, otherwise return to step 601.
Step 604, obtaining the current memory state information from the memory usage record file corresponding to the query process.
Step 605, screen out the Private [ heap ] and Private anonymous memory segments, and sum the Private _ Dirty value and the Private _ Clean value.
Step 606, removing the cache data amount in the current memory to obtain the data amount of the unreleased memory in the current memory.
Step 607, determining whether the memory leak detection is currently performed for the first time; if not, go to step 608, if yes, go to step 609.
In step 608, the data amount of the unreleased memory is compared with the reference data amount.
In step 609, a reference data amount is determined according to the data amount of the unreleased memory.
Step 610, determining whether a memory leak exists currently; if yes, go to step 611, otherwise return to step 601.
Step 611, the memory leak is processed based on a preset processing policy corresponding to the memory leak.
Based on the same disclosure concept, the embodiment of the present application further provides a memory leak detection apparatus, and since the apparatus is an apparatus in the method in the embodiment of the present application, and the principle of the apparatus to solve the problem is similar to that of the method, the implementation of the apparatus may refer to the implementation of the method, and repeated details are not repeated.
Fig. 7 is a schematic diagram of a memory leak detection apparatus according to an embodiment of the present application, please refer to fig. 7, which provides a memory leak detection apparatus according to an embodiment of the present application, the apparatus including:
an obtaining unit 701, configured to receive a query request from a user, and obtain current memory state information from a memory usage record file corresponding to a query process when determining that a current memory leak detection function is started;
a screening unit 702, configured to screen a target memory segment related to memory leakage from the memory state information, and determine, based on the target memory segment, a data amount of an unreleased memory in a current memory;
a determining unit 703 is configured to compare the data amount of the unreleased memory with a reference data amount, and determine whether a memory leak exists currently, where the reference data amount is determined according to the data amount of the memory when the memory leak does not occur.
Optionally, the determining unit 703 is configured to compare the data amount of the unreleased memory with the reference data amount, and determine whether there is a memory leak before the memory leak currently occurs, and further includes:
determining whether the memory leak detection is carried out for the first time at present according to a first variable of the memory leak detection times recorded in the query process;
when determining that memory leak detection is carried out for the first time, determining a reference data volume according to the data volume of the unreleased memory;
and when the memory leak detection is determined not to be carried out for the first time, the step of comparing the data volume of the unreleased memory with the reference data volume is carried out.
Optionally, the obtaining unit 701 is configured to, before obtaining the current memory state information from the memory usage record file, further include:
determining the corresponding times of the currently received query request in the query process;
and determining that the preset memory leak detection period is reached according to the corresponding times of the currently received query request in the query process.
Optionally, a global variable for recording the number of times of receiving the query request is set in the query process, an initial value of the global variable is 0, and a value of the current global variable is increased by 1 each time a query request of the user is received.
Optionally, the screening unit 702 is configured to screen a target memory segment related to memory leak from the memory state information, and determine, based on the target memory segment, a data size of an unreleased memory in the current memory, where the method includes:
screening a stack memory segment corresponding to a brk function and an anonymous memory segment corresponding to a mmap function from the memory state information, wherein the brk function and the mmap function are calling functions corresponding to the query request;
and summing the physical memory values respectively recorded in the stack memory segment and the anonymous memory segment, removing the cache data volume in the current memory, and determining the data volume of the unreleased memory in the current memory.
Optionally, the determining unit 703 is configured to compare the data amount of the unreleased memory with a reference data amount, and determine whether a memory leak exists currently, where the determining unit includes:
calculating the difference between the data quantity of the unreleased memory and the reference data quantity;
and when the difference is determined to be larger than the preset threshold value, determining that the memory leakage exists currently.
Optionally, when the determining unit 703 is configured to determine that there is a memory leak currently, the determining unit further includes:
processing the memory leakage based on a preset processing strategy corresponding to the memory leakage;
wherein the processing strategy comprises at least one of log generation, reminding, alarming, error throwing and query process termination.
Based on the same disclosure concept, the embodiment of the present application further provides a memory leak detection device, and since the device is a device in the method in the embodiment of the present application, and the principle of the device to solve the problem is similar to that of the method, the implementation of the device may refer to the implementation of the method, and repeated details are not repeated.
As will be appreciated by one skilled in the art, aspects of the present application may be embodied as a system, method or program product. Accordingly, various aspects of the present application may be embodied in the form of: an entirely hardware embodiment, an entirely software embodiment (including firmware, microcode, etc.), or an embodiment combining hardware and software aspects that may all generally be referred to herein as a "circuit," module "or" system.
In some possible embodiments, an apparatus according to the present application may include at least one processor, and at least one memory. The memory stores program code, and when the program code is executed by the processor, the program code causes the processor to execute the steps of the memory leak detection method according to the various exemplary embodiments of the present application described above in this specification.
An apparatus 800 according to this embodiment of the present application is described below with reference to fig. 8. The device 800 shown in fig. 8 is only an example and should not impose any limitation on the functionality and scope of use of embodiments of the present application.
As shown in fig. 8, the device 800 is in the form of a general purpose device. The components of device 800 may include, but are not limited to: the at least one processor 801, the at least one memory 802, and the bus 803 connecting the various system components (including the memory 802 and the processor 801), wherein the memory stores program code that, when executed by the processor, causes the processor to perform the steps of:
receiving a query request of a user, and acquiring current memory state information from a memory use condition record file corresponding to a query process when determining that a current memory leakage detection function is started;
screening out a target memory segment related to memory leakage from the memory state information, and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
and comparing the data volume of the unreleased memory with the reference data volume to determine whether the memory leaks currently, wherein the reference data volume is determined according to the data volume of the memory when the memory leaks.
Bus 803 represents one or more of any of several types of bus structures, including a memory bus or memory controller, a peripheral bus, a processor, or a local bus using any of a variety of bus architectures.
The memory 802 may include readable media in the form of volatile memory, such as Random Access Memory (RAM) 8021 and/or cache memory 8022, and may further include Read Only Memory (ROM) 8023.
Memory 802 may also include a program/utility 8025 having a set (at least one) of program modules 8024, such program modules 8024 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which, or some combination thereof, may comprise an implementation of a network environment.
Device 800 can also communicate with one or more external devices 804 (e.g., keyboard, pointing device, etc.), with one or more devices that enable a user to interact with device 800, and/or with any devices (e.g., router, modem, etc.) that enable device 800 to communicate with one or more other devices. Such communication may be through input/output (I/O) interfaces 805. Also, the device 800 may communicate with one or more networks (e.g., a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network such as the internet) via the network adapter 806. As shown, the network adapter 806 communicates with the other modules for the device 800 over the bus 803. It should be understood that although not shown in the figures, other hardware and/or software modules may be used in conjunction with the device 800, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, to name a few.
Optionally, the processor is configured to compare the data amount of the unreleased memory with the reference data amount, and determine whether there is a memory leak, and further configured to:
determining whether the memory leak detection is carried out for the first time at present according to a first variable for recording the memory leak detection times in the query process;
when determining that memory leak detection is carried out for the first time, determining a reference data volume according to the data volume of the unreleased memory;
and when the memory leak detection is determined not to be carried out for the first time, the step of comparing the data volume of the unreleased memory with the reference data volume is carried out.
Optionally, before the processor is configured to obtain the current memory state information from the memory usage record file corresponding to the query process, the processor is further configured to:
determining the corresponding times of the currently received query request in the query process;
and determining that the preset memory leak detection period is reached according to the corresponding times of the currently received query request in the query process.
Optionally, a global variable for recording the number of times of receiving the query request is set in the query process, an initial value of the global variable is 0, and a value of the current global variable is increased by 1 each time a query request of the user is received.
Optionally, the processor is configured to screen out a target memory segment related to memory leakage from the memory state information, and determine a data size of an unreleased memory in a current memory based on the target memory segment, where the step includes:
screening a stack memory segment corresponding to a brk function and an anonymous memory segment corresponding to a mmap function from the memory state information, wherein the brk function and the mmap function are calling functions corresponding to the query request;
and summing the physical memory values respectively recorded in the stack memory segment and the anonymous memory segment, removing the cache data volume in the current memory, and determining the data volume of the unreleased memory in the current memory.
Optionally, the processor is configured to compare the data amount of the unreleased memory with a reference data amount, and determine whether a memory leak exists currently, where the determining includes:
calculating the difference between the data quantity of the unreleased memory and the reference data quantity;
and when the difference is determined to be larger than the preset threshold value, determining that the memory leakage exists currently.
Optionally, when determining that there is a memory leak currently, the processor is further configured to:
processing the memory leakage based on a preset processing strategy corresponding to the memory leakage;
wherein the processing strategy comprises at least one of log generation, reminding, alarming, error throwing and query process termination.
In some possible embodiments, various aspects of a memory leak detection method provided by the present application may also be implemented in the form of a program product including program code for causing a computer device to perform the steps of a memory leak detection method according to various exemplary embodiments of the present application described above in this specification when the program product is run on the computer device.
The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium include: an electrical connection having one or more wires, a portable disk, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The program product for monitoring of embodiments of the present application may employ a portable compact disk read only memory (CD-ROM) and include program code, and may be run on a device. However, the program product of the present application is not so limited, and in this document, a readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
A readable signal medium may include a propagated data signal with readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A readable signal medium may also be any readable medium that is not a readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Program code for carrying out operations of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C + + or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user device, partly on the user device, as a stand-alone software package, partly on the user device and partly on a remote device, or entirely on the remote device or server. In the case of a remote device, the remote device may be connected to the user device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external device (e.g., through the internet using an internet service provider).
It should be noted that although several units or sub-units of the apparatus are mentioned in the above detailed description, such division is merely exemplary and not mandatory. Indeed, the features and functions of two or more of the units described above may be embodied in one unit, according to embodiments of the application. Conversely, the features and functions of one unit described above may be further divided into embodiments by a plurality of units.
Further, while the operations of the methods of the present application are depicted in the drawings in a particular order, this does not require or imply that these operations must be performed in this particular order, or that all of the illustrated operations must be performed, to achieve desirable results. Additionally or alternatively, certain steps may be omitted, multiple steps combined into one step execution, and/or one step broken down into multiple step executions.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flowchart illustrations and block diagrams, and combinations of flows and blocks in the flowchart illustrations and block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and block diagram block or blocks.
While the preferred embodiments of the present application have been described, additional variations and modifications of these embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all alterations and modifications as fall within the scope of the application.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.

Claims (10)

1. A memory leak detection method is applied to a query process, and comprises the following steps:
receiving a query request of a user, and acquiring current memory state information from a memory use condition record file corresponding to the query process when determining that the current memory leakage detection function is started;
screening out a target memory segment related to memory leakage from the memory state information, and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
and comparing the data volume of the unreleased memory with a reference data volume to determine whether the memory leaks currently, wherein the reference data volume is determined according to the data volume of the memory when the memory leaks.
2. The method of claim 1, wherein comparing the amount of unreleased memory data with a reference amount of data to determine whether a memory leak exists at present, further comprises:
determining whether the memory leak detection is carried out for the first time at present according to a first variable of the memory leak detection times recorded in the query process;
determining a reference data volume according to the data volume of the unreleased memory when determining that the memory leakage detection is carried out for the first time;
and when the memory leak detection is determined not to be carried out for the first time, the step of comparing the data volume of the unreleased memory with the reference data volume is carried out.
3. The method according to claim 1 or 2, wherein before acquiring the current memory state information from the memory usage record file corresponding to the query process, the method further comprises:
determining the corresponding times of the currently received query request in the query process;
and determining that a preset memory leak detection period is reached according to the corresponding times of the currently received query request in the query process.
4. The method according to claim 3, wherein a global variable for recording the number of times of receiving the query request is set in the query process, the global variable has an initial value of 0, and the value of the current global variable is increased by 1 every time a query request from a user is received.
5. The method of claim 1, wherein screening the memory status information for a target memory segment associated with a memory leak and determining an amount of unreleased memory data in current memory based on the target memory segment comprises:
screening a stack memory segment corresponding to a brk function and an anonymous memory segment corresponding to a mmap function from the memory state information, wherein the brk function and the mmap function are calling functions corresponding to the query request;
and summing the physical memory values respectively recorded in the stack memory segment and the anonymous memory segment, removing the cache data volume in the current memory, and determining the data volume of the unreleased memory in the current memory.
6. The method of claim 1, wherein comparing the amount of unreleased memory data to a reference amount of data to determine whether a memory leak currently exists comprises:
calculating a difference between the data amount of the unreleased memory and the reference data amount;
and when the difference is determined to be larger than a preset threshold value, determining that the memory leakage exists currently.
7. The method according to claim 1 or 6, wherein when determining that there is a memory leak currently, further comprising:
processing the memory leak based on a preset processing strategy corresponding to the memory leak;
wherein the processing strategy comprises at least one of log generation, reminding, alarming, error throwing and query process termination.
8. A memory leak detection apparatus, comprising:
the acquisition unit is used for receiving a query request of a user and acquiring current memory state information from a memory use condition record file corresponding to a query process when the current memory leak detection function is determined to be started;
the screening unit is used for screening out a target memory segment related to memory leakage from the memory state information and determining the data volume of the unreleased memory in the current memory based on the target memory segment;
and the determining unit is used for comparing the data volume of the unreleased memory with a reference data volume and determining whether the memory leakage exists currently, wherein the reference data volume is determined according to the data volume of the memory when the memory leakage does not occur.
9. A memory leak detection apparatus comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of the method of any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium having computer program instructions stored thereon, which, when executed by a processor, implement the steps of the method of any one of claims 1 to 7.
CN202211535480.2A 2022-11-30 2022-11-30 Memory leak detection method, device, equipment and medium Pending CN115794553A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211535480.2A CN115794553A (en) 2022-11-30 2022-11-30 Memory leak detection method, device, equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211535480.2A CN115794553A (en) 2022-11-30 2022-11-30 Memory leak detection method, device, equipment and medium

Publications (1)

Publication Number Publication Date
CN115794553A true CN115794553A (en) 2023-03-14

Family

ID=85444771

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211535480.2A Pending CN115794553A (en) 2022-11-30 2022-11-30 Memory leak detection method, device, equipment and medium

Country Status (1)

Country Link
CN (1) CN115794553A (en)

Similar Documents

Publication Publication Date Title
US7734666B2 (en) Method and system for inspecting memory leaks and analyzing contents of garbage collection files
US8326894B2 (en) Method and system to space-efficiently track memory access of object-oriented language in presence of garbage collection
US8141053B2 (en) Call stack sampling using a virtual machine
US8229979B2 (en) Method and system for inspecting memory leaks
US7904493B2 (en) Method and system for object age detection in garbage collection heaps
US7937625B2 (en) Evaluating effectiveness of memory management techniques selectively using mitigations to reduce errors
US8793289B2 (en) Method and system for detecting memory leaks and copying garbage collection files
US20170031814A1 (en) Scheme for determining data object usage in a memory region
CN110168508B (en) Method, computer system and medium for monitoring memory location where breakpoint occurs
US11782609B2 (en) Method and apparatus for auditing abnormality of block device in cloud platform, device, and storage medium
US9235478B2 (en) Classifying and monitoring database operations based on a cost of recovery
CN113448938A (en) Data processing method and device, electronic equipment and storage medium
US7725771B2 (en) Method and system for providing enhanced memory error messages
CN110018932B (en) Method and device for monitoring container magnetic disk
US9274946B2 (en) Pre-leak detection scan to identify non-pointer data to be excluded from a leak detection scan
EP2329384A2 (en) Memory management techniques selectively using mitigations to reduce errors
US8140892B2 (en) Configuration of memory management techniques selectively using mitigations to reduce errors
US9870400B2 (en) Managed runtime cache analysis
US20120266028A1 (en) Electronic device and method for debugging programs
US20150378799A1 (en) Automatic memory leak detection
CN115794553A (en) Memory leak detection method, device, equipment and medium
CN114860432A (en) Method and device for determining information of memory fault
EP2960798B1 (en) Automatic memory leak detection
CN112905422A (en) Alarm rule management method and equipment based on search server
JP2009217617A (en) Method and device for identifying memory leak place

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