U.S. patent application number 16/325175 was filed with the patent office on 2021-10-28 for method for detecting memory leak based on linux kernel.
The applicant listed for this patent is AMLOGIC (SHANGHAI) CO., LTD.. Invention is credited to Tao ZENG.
Application Number | 20210334192 16/325175 |
Document ID | / |
Family ID | 1000005895394 |
Filed Date | 2021-10-28 |
United States Patent
Application |
20210334192 |
Kind Code |
A1 |
ZENG; Tao |
October 28, 2021 |
METHOD FOR DETECTING MEMORY LEAK BASED ON LINUX KERNEL
Abstract
A method for detecting a memory leak based on Linux kernel,
applied to an detection of the memory leak, comprises: reading a
node, acquiring the return addresses of the allocation functions of
each of the plurality of memory pages and the number of the memory
pages thereof; releasing the return addresses of the allocation
functions and the number of the memory pages counted by the node;
reading the node again, acquiring the return address of each of the
allocation functions and the number of the memory pages thereof;
comparing the number in each case to calculate a difference value,
if the difference value is a positive value and monotonically
increases, it's determined that the memory leak occurs in the
memory pages allocated correspondingly by the allocation functions.
During the detection of the memory leak, the detection method
consumes less memory without affecting the efficiency in allocating
and releasing the memory.
Inventors: |
ZENG; Tao; (Shanghai,
CN) |
|
Applicant: |
Name |
City |
State |
Country |
Type |
AMLOGIC (SHANGHAI) CO., LTD. |
|
|
|
|
|
Family ID: |
1000005895394 |
Appl. No.: |
16/325175 |
Filed: |
September 27, 2018 |
PCT Filed: |
September 27, 2018 |
PCT NO: |
PCT/CN2018/108103 |
371 Date: |
February 12, 2019 |
Current U.S.
Class: |
1/1 |
Current CPC
Class: |
G06F 9/5016 20130101;
G06F 11/366 20130101 |
International
Class: |
G06F 11/36 20060101
G06F011/36; G06F 9/50 20060101 G06F009/50 |
Foreign Application Data
Date |
Code |
Application Number |
Nov 7, 2017 |
CN |
201711085297.6 |
Claims
1. A method for detecting a memory leak based on Linux kernel,
applied to an detection of the memory leak, wherein the memory
comprises a plurality of memory pages, wherein a node is created
through which return addresses of allocation functions to be called
in each of the plurality of memory pages and the number of the
memory pages allocated by each of the allocation functions are
counted; wherein the method comprises steps of: Step S1, reading
the node, acquiring the return addresses of the allocation
functions of each of the plurality of memory pages and the number
of the memory pages which are allocated correspondingly by the
allocation functions; Step S2, after reading of the node, releasing
the return addresses of the allocation functions counted by the
nodes and the number of the memory pages which are allocated
correspondingly by the allocation functions; Step S3, reading the
node again, acquiring the return address of each of the allocation
functions and the number of the memory pages which are allocated
correspondingly by the allocation functions; Step S4, acquiring the
number of the memory pages allocated respectively and
correspondingly by the identical allocation function in Step S3 and
Step S1, and comparing the number in each case to calculate a
difference value; if the difference value is a positive value and
it monotonically increases, it is determined that the memory leak
occurs in the memory pages allocated correspondingly by the
allocation functions.
2. The method for detecting a memory leak as claimed in claim 1,
wherein each of the plurality of memory pages comprises a data
structure, and an address return identifier, an allocation order
flag and a memory type allocation identifier are added to the data
structure; wherein the address return identifier is configured for
recording an address of a calling function when the memory is
allocated; and the allocation identifier is configured for
indicating the memory type allocated to the plurality of memory
pages.
3. The method for detecting a memory leak as claimed in claim 2,
wherein when the calling function information is saved each time
when the memory allocation is performed, a function calling chain
is checked by a kernel stack, and common allocation functions,
return addresses, allocation parameters and allocation orders
corresponding to the common allocation functions are sequentially
saved in the calling chain; acquiring the return addresses, the
allocation parameters and the allocation orders corresponding to
the allocation functions on top of the calling chain; saving the
return addresses to the address return identifier, and saving the
allocation parameters and the allocation orders to the data
structures of the plurality of memory pages.
4. The method for detecting a memory leak as claimed in claim 2,
wherein the memory comprises a plurality of memory areas, each of
the plurality of memory areas is formed by the plurality of memory
pages, and a method for acquiring the allocation function of each
of the plurality of memory pages comprises following steps of: Step
A1, reading each of the plurality of memory areas in sequence; Step
A2, reading the data structure corresponding to the each of the
plurality of memory pages in each of the plurality of memory areas
so as to acquire the return address of the allocation function
corresponding to each of the plurality of memory pages; and Step
A3, after reading of all the plurality of memory areas and the
plurality of memory pages in each of the plurality of memory areas,
the acquired return addresses corresponding to the plurality of
allocation functions and the number of the memory page allocated by
each of the allocation function together form a temporary
array.
5. The method for detecting a memory leak as claimed in claim 4,
wherein a printing module is provided, and each of the allocation
functions, the return address corresponding to each of the
allocation functions and the number of the memory pages allocated
correspondingly by the allocation functions, all of which are saved
in the temporary array, are printed and displayed in an interface
by the printing module.
6. The method for detecting a memory leak as claimed in claim 4,
wherein a temporary recording array is provided for storing the
return address of the allocation function corresponding to the read
memory page; wherein Step A2 further comprises following steps of:
Step A21: determining whether all the plurality of memory pages are
read; If the result shows "YES", it means that all the plurality of
memory pages are read, then exiting; Step A22: when the next memory
page is read, the return address corresponding to the read
allocation function is matched with the return address in the
temporary recording array; Step A23, if the return address
corresponding to the read allocation function is the same with the
return address in the temporary recording array, the number of the
memory pages allocated to the current allocation function is
increased by 1, and returning to Step A21; and Step A24: if the
return address corresponding to the read allocation function is not
the same with the return address in the temporary recording array,
saving the return address of the allocation function corresponding
to the memory page to the temporary recording array, and returning
to Step A21.
7. The method for detecting a memory leak as claimed in claim 3,
wherein when the plurality of memory pages are released, the return
addresses saved in the address return identifier, the allocation
parameters and the allocation orders saved in the data structure
are cleared.
8. The method for detecting a memory leak as claimed in claim 2,
wherein a macro configuration module is provided for forming the
address return identifier by configuration.
Description
RELATED APPLICATION INFORMATION
[0001] This patent claims priority from International PCT Patent
Application No. PCT/CN2018/108103, filed Sep. 27, 2018 entitled,
METHOD FOR DETECTING MEMORY LEAK BASED ON LINUX KERNEL, which
claims priority to Chinese Application No. 201711085297.6, filed
Nov. 7, 2017 entitled, METHOD FOR DETECTING MEMORY LEAK BASED ON
LINUX KERNEL, all of which are incorporated herein by reference in
their entirety.
NOTICE OF COPYRIGHTS AND TRADE DRESS
[0002] A portion of the disclosure of this patent document contains
material which is subject to copyright protection. This patent
document may show and/or describe matter which is or may become
trade dress of the owner. The copyright and trade dress owner has
no objection to the facsimile reproduction by anyone of the patent
disclosure as it appears in the Patent and Trademark Office patent
files or records, but otherwise reserves all copyright and trade
dress rights whatsoever.
BACKGROUND OF THE INVENTION
1. Field of the Invention
[0003] The invention relates to the field of communication
technologies, and more particularly, to a method for detecting a
memory leak based on Linux kernel.
2. Description of the Related Art
[0004] A memory leak refers to a situation where a heap memory in
an application program having been dynamically allocated is not
released or cannot be released for some reasons, resulting in a
waste of system memory, thus, the application slows down, or even
the system crashes and other serious consequences. In the existing
Linux system, the detection of the memory leak is specifically
performed through the default memleak detection technology of
Kernel. After executing the memleak detection method, the Kernel
will track its pointer at the return of the functions such as
kmalloc, vmalloc, kmem_cache_alloc, etc., then the pointer,
together with its size and stack trace information, will be stored
in PRIO search tree. When the memory is released by the Kernel, the
stored trace information will be deleted by kmemleak_free. The
Kernel will initiate a kernel thread of kmemleak_s, periodically
scanning the stored object information, and then determining
whether the object is referenced to determine if there is a memory
leak. However, problems listed as follows exist in the
above-mentioned memleak technology. A periodical scanning is
required during the detection process, which would increase the CPU
consumption burden; when the memory is allocated and released, the
slab allocation is performed again so as to record the information
of the memory object, which would prolong the time for the memory
to be allocated and released, thereby reducing the efficiency of
the system.
SUMMARY OF THE INVENTION
[0005] In view of the above-mentioned problems occurring in the
detection of a memory leak in the prior art, the present invention
intends to provide a method for detecting a memory leak based on
Linux kernel, which has the advantages of occupying less memory
without affecting the efficiency in allocating and releasing the
memory.
[0006] The technical solutions are as follows:
[0007] A method for detecting a memory leak based on Linux kernel,
applied to an detection of the memory leak, wherein the memory
comprises a plurality of memory pages, wherein a node is created
through which return addresses of allocation functions to be called
in each of the plurality of memory pages and the number of the
memory pages allocated by each of the allocation functions are
counted; wherein the method comprises steps of:
[0008] Step S1, reading the node, acquiring the return addresses of
the allocation functions of each of the plurality of memory pages
and the number of the memory pages which are allocated
correspondingly by the allocation functions;
[0009] Step S2, after reading of the node, releasing the return
addresses of the allocation functions and the number of the memory
pages which are allocated correspondingly by the allocation
functions counted by the node;
[0010] Step S3, reading the node again, acquiring the return
address of each of the allocation functions and the number of the
memory pages which are allocated correspondingly by the allocation
functions;
[0011] Step S4, acquiring the number of the memory pages allocated
respectively and correspondingly by the identical allocation
function in Step S3 and Step S1, and comparing the number in each
case to calculate a difference value; if the difference value is a
positive value and it monotonically increases, it is determined
that the memory leak occurs in the memory pages allocated
correspondingly by the allocation functions.
[0012] Preferably, each of the plurality of memory pages comprises
a data structure, and an address return identifier, an allocation
order flag and a memory type allocation identifier are added to the
data structure; wherein the address return identifier is configured
for recording an address of a calling function when the memory is
allocated; and the allocation identifier is configured for
indicating the memory type allocated to the plurality of memory
pages.
[0013] Preferably, when the calling function information is saved
each time when the memory allocation is performed, a function
calling chain is checked by a kernel stack, and common allocation
functions, return addresses, allocation parameters and allocation
orders corresponding to the common allocation functions are
sequentially saved in the calling chain; acquiring the return
addresses, the allocation parameters and the allocation orders
corresponding to the allocation functions on top of the calling
chain; saving the return addresses to the address return
identifier, and saving the allocation parameters and the allocation
orders to the data structures of the plurality of memory pages.
[0014] Preferably, the memory comprises a plurality of memory
areas, each of the plurality of memory areas is formed by the
plurality of memory pages, and a method for acquiring the
allocation function of each of the plurality of memory pages
comprises following steps of:
[0015] Step A1, reading each of the plurality of memory areas in
sequence;
[0016] Step A2, reading the data structure corresponding to the
each of the plurality of memory pages in each of the plurality of
memory areas so as to acquire the return address of the allocation
function corresponding to each of the plurality of memory pages;
and
[0017] Step A3, after reading of all the plurality of memory areas
and the plurality of memory pages in each of the plurality of
memory areas, the acquired return addresses corresponding to the
plurality of allocation functions and the number of the memory page
allocated by each of the allocation function together form a
temporary array.
[0018] Preferably, a printing module is provided, and each of the
allocation functions, the return address corresponding to each of
the allocation functions and the number of the memory pages
allocated correspondingly by the allocation functions, all of which
are saved in the temporary array, are printed and displayed in an
interface by the printing module.
[0019] Preferably, in Step A2, a temporary recording array is
provided for storing the return address of the allocation function
corresponding to the read memory page;
[0020] Step A2 further comprises following steps of:
[0021] Step A21: determining whether all the plurality of memory
pages are read;
[0022] If the result shows "YES", it means that all the plurality
of memory pages are read, then exiting;
[0023] Step A22: when the next memory page is read, the return
address corresponding to the read allocation function is matched
with the return address in the temporary recording array;
[0024] Step A23, if the return address corresponding to the read
allocation function is the same with the return address in the
temporary recording array, the number of the memory pages allocated
to the current allocation function is increased by 1, and returning
to Step A21; and
[0025] Step A24: if the return address corresponding to the read
allocation function is not the same with the return address in the
temporary recording array, saving the return address of the
allocation function corresponding to the memory page to the
temporary recording array, and returning to Step A21.
[0026] Preferably, when the plurality of memory pages are released,
the return addresses saved in the address return identifier, the
allocation parameters and the allocation orders saved in the data
structure are cleared.
[0027] Preferably, a macro configuration module is provided for
forming the address return identifier by configuration.
[0028] The above-mentioned technical solutions have the following
beneficial effects. During the detection of the memory leak, the
detection method consumes less memory without affecting the
efficiency in allocating and releasing the memory. In addition, the
present invention overcomes the above-mentioned problems that a
periodical scanning is required during the detection process
performed through the default memleak detection technology of
Kernel, which increases the CPU consumption burden; when the memory
is allocated and released, the slab allocation is performed again
to record the information of the memory object, which prolongs the
time for the memory to be allocated and released, thereby reducing
the efficiency of the system.
BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The accompanying drawings, together with the specification,
illustrate exemplary embodiments of the present disclosure, and,
together with the description, serve to explain the principles of
the present invention.
[0030] FIG. 1 is a flowchart of a method for detecting a memory
leak based on Linux kernel according to an embodiment of the
present invention;
[0031] FIG. 2 is a flowchart of a method for obtaining the
allocation function of each of the plurality of memory pages in a
method for detecting a memory leak based on Linux kernel according
to an embodiment of the present invention;
[0032] FIG. 3 is a flowchart of a method for obtaining the
allocation function of each of the plurality of memory pages in a
method for detecting a memory leak based on Linux kernel according
to an embodiment of the present invention; and
[0033] FIG. 4 is a calling graph showing the kernel memory
allocates API in a method for detecting a memory leak based on
Linux kernel according to an embodiment of the present
invention.
DETAILED DESCRIPTION
[0034] The present invention will now be described more fully
hereinafter with reference to the accompanying drawings, in which
exemplary embodiments of the invention are shown. This invention
may, however, be embodied in many different forms and should not be
construed as limited to the embodiments set forth herein. Rather,
these embodiments are provided so that this disclosure will be
thorough and complete, and will fully convey the scope of the
invention to those skilled in the art. Like reference numerals
refer to like elements throughout.
[0035] The terminology used herein is for the purpose of describing
particular embodiments only and is not intended to be limiting of
the invention. As used herein, the singular forms "a", "an" and
"the" are intended to include the plural forms as well, unless the
context clearly indicates otherwise. It will be further understood
that the terms "comprises" and/or "comprising," or "includes"
and/or "including" or "has" and/or "having" when used herein,
specify the presence of stated features, regions, integers, steps,
operations, elements, and/or components, but do not preclude the
presence or addition of one or more other features, regions,
integers, steps, operations, elements, components, and/or groups
thereof.
[0036] Unless otherwise defined, all terms (including technical and
scientific terms) used herein have the same meaning as commonly
understood by one of ordinary skill in the art to which this
invention belongs. It will be further understood that terms, such
as those defined in commonly used dictionaries, should be
interpreted as having a meaning that is consistent with their
meaning in the context of the relevant art and the present
disclosure, and will not be interpreted in an idealized or overly
formal sense unless expressly so defined herein.
[0037] As used herein, "around", "about" or "approximately" shall
generally mean within 20 percent, preferably within 10 percent, and
more preferably within 5 percent of a given value or range.
Numerical quantities given herein are approximate, meaning that the
term "around", "about" or "approximately" can be inferred if not
expressly stated.
[0038] As used herein, the term "plurality" means a number greater
than one.
[0039] Hereinafter, certain exemplary embodiments according to the
present disclosure will be described with reference to the
accompanying drawings.
[0040] The technical solutions set forth in the present invention
comprise a method for detecting a memory leak based on Linux
kernel.
[0041] An embodiment of a method for detecting a memory leak based
on Linux kernel, applied to an detection of the memory leak,
wherein the memory comprises a plurality of memory pages, wherein a
node is created through which return addresses of allocation
functions to be called in each of the plurality of memory pages and
the number of the memory page allocated by each of the allocation
functions are counted; as shown in FIG. 1, the method comprises
steps of:
[0042] Step S1, reading the node, acquiring the return addresses of
the allocation functions of each of the plurality of memory pages
and the number of the memory pages which are allocated
correspondingly by the allocation functions;
[0043] Step S2, after reading of the node, releasing the return
addresses of the allocation functions counted by the nodes and the
number of the memory pages which are allocated correspondingly by
the allocation functions;
[0044] Step S3, reading the node again, acquiring the return
address of each of the allocation functions and the number of the
memory pages which are allocated correspondingly by the allocation
functions;
[0045] Step S4, acquiring the number of the memory pages allocated
respectively and correspondingly by the identical allocation
function in Step S3 and Step S1, and comparing the number in each
case to calculate a difference value; if the difference value is a
positive value and it monotonically increases, it is determined
that the memory leak occurs in the memory pages allocated
correspondingly by the allocation functions.
[0046] For the existing Linux system, a periodical scanning is
required during the detection process performed through the default
memleak detection technology of Kernel, which would increase the
CPU consumption burden; when the memory is allocated and released,
the slab allocation is performed again to record the information of
the memory object, which would prolong the time for the memory to
be allocated and released, thereby reducing the efficiency of the
system; and when the kernel allocates more objects, the slab of the
kmemcache would consume a lot of memories; take a typical 2 GB
Android platform as an example, if you open the kmemleak
configuration, the kmemleak slab will consume about 82 MB of memory
after the boot is started, which is obviously a great waste.
[0047] In the present invention, a created node acquires the return
addresses of the allocation functions of each of the plurality of
memory pages and the number of the memory pages which are allocated
correspondingly by the allocation functions. It is assumed here
that the acquired allocation functions are A1, A2, A3 and A4, and
that the number of the memory pages corresponding to the functions
A1, A2, A3 and A4 are 100, 200, 300 and 400, respectively.
[0048] After releasing the return addresses of the allocation
functions and the number of the memory pages which are allocated
correspondingly by the allocation functions counted by the node,
reading the node again, and counting the acquired allocation
functions and the number of the memory pages which are allocated
correspondingly by the allocation functions. It is assumed here
that the acquired allocation functions are A1, A2, A3 and A4, and
that the number of the memory pages corresponding to the functions
A1, A2, A3 and A4 are 300, 400, 500 and 600, respectively.
[0049] Comparing the allocation function A1 acquired currently to
the allocation function A1 acquired previously to calculate a
difference value. If the difference value is greater than 0 and
monotonically increases, it means that a memory leak occurs in the
memory page allocated correspondingly by the current allocation
function; if the difference value is equal to or less than 0, and
it always tend to be very small, it means that the memory page
allocated correspondingly by the current allocation function is
normal.
[0050] In a preferred embodiment, as shown in FIG. 4, each of the
plurality of memory pages comprises a data structure, and an
address return identifier represented by _ret_ip, an allocation
order flag and a memory type allocation identifier represented by
alloc_mask are added to the data structure, wherein the address
return identifier is configured for recording an address of a
calling function when the memory is allocated, the name of the
calling function can be used to confirm the identity information of
a memory allocator, and the allocation identifier is configured for
indicating the memory type allocated to the plurality of memory
pages.
[0051] In the above-mentioned technical solution, for the address
return identifier and the memory type allocation identifier added
to the data structure (struct pag structure) in each of the
plurality of memory pages, only one long and int type member is
added, and 12 bytes are added to a 64-bit system, and only 6 MB of
memory are added to a specific 2 GB of memory system. Compared with
the increase of 82 MB of slab data of kmemleak, the memory
consumption is greatly reduced.
[0052] In a preferred embodiment, when the calling function
information is saved each time when the memory allocation is
performed, a function calling chain is checked by a kernel stack,
and common allocation functions, return addresses, allocation
parameters and allocation orders corresponding to the common
allocation functions are sequentially saved in the calling chain;
acquiring the return addresses, the allocation parameters and the
allocation orders corresponding to the allocation functions on top
of the calling chain; saving the return addresses to the address
return identifier, and saving the allocation parameters and the
allocation orders to the data structures of the plurality of memory
pages.
[0053] In the above-mentioned technical solution, the allocation
orders comprises the number of the memory pages to be allocated by
each of the allocation functions, which is usually represented by
2.sup.k, and the size of each of the plurality of memory pages is 4
KB, and the allocation parameters comprises the type of the memory
allocated by the current allocation parameter.
[0054] In a preferred embodiment, the memory comprises a plurality
of memory areas, each of the plurality of memory areas is formed by
the plurality of memory pages, and as shown in FIG. 2, a method for
acquiring the allocation functions of each of the plurality of
memory pages comprises following steps of:
[0055] Step A1, reading each of the plurality of memory areas in
sequence;
[0056] Step A2, reading the data structure corresponding to the
each of the plurality of memory pages in each of the plurality of
memory areas so as to acquire the return address of the allocation
function corresponding to each of the plurality of memory pages;
and
[0057] Step A3, after reading of all the plurality of memory areas
and the plurality of memory pages in each of the plurality of
memory areas, the acquired return addresses corresponding to the
plurality of allocation functions and the number of the memory page
allocated by each of the allocation function together form a
temporary array.
[0058] In a preferred embodiment, a printing module is provided,
and each of the allocation functions, the return address
corresponding to each of the allocation functions and the number of
the memory pages allocated correspondingly by the allocation
functions, all of which are saved in the temporary array, are
printed and displayed in an interface by the printing module.
[0059] In a preferred embodiment, a temporary recording array is
provided for storing the return address of the allocation function
corresponding to the read memory page; as shown in FIG. 3, Step A2
further comprises following steps of:
[0060] Step A21: determining whether all the plurality of memory
pages in the current memory area are read;
[0061] If the result shows "YES", it means that all the plurality
of memory pages are read, then exiting;
[0062] Step A22: when the next memory page is read, the return
address corresponding to the read allocation function is matched
with the return address in the temporary recording array;
[0063] Step A23, if the return address corresponding to the read
allocation function is the same with the return address in the
temporary recording array, the number of the memory pages allocated
to the current allocation function is increased by 1, and returning
to Step A21; and
[0064] Step A24: if the return address corresponding to the read
allocation function is not the same with the return address in the
temporary recording array, saving the return address of the
allocation function corresponding to the memory page to the
temporary recording array, and returning to Step A21.
[0065] In the above-mentioned technical solution, each of the
plurality of memory areas is represented by a zone, and the
plurality of memory pages are stored in the zone, and each of the
plurality of memory pages comprises a struct pag structure. When
reading the memory area, by starting from the start_pfn to end_pfn
of the zone, acquiring the struct page structure of the memory page
according to pfn; then finding _ret_ip (the return address
corresponding to the allocation function) from the struct page
structure, and looking up in an array which temporarily records
ret_ip according to _ret_ip.
[0066] If no match is made, it means that this is a new _ret_ip,
then recording it and increasing its allocation based on the
allocation order of this page. All the pages and zones are
subjected to the above-mentioned operation in the sequence listed
above.
[0067] A _ret_ip that records the return address of all the
allocators and a temporary array of the total number of allocations
can be obtained. Finally, printing out the _ret_ip and the
temporary array, and completing reading the node
(/proc/pagetrace).
[0068] In a preferred embodiment, when the plurality of memory
pages are released, the return addresses saved in the address
return identifier, the allocation parameters and the allocation
orders saved in the data structure are cleared.
[0069] In the above-mentioned technical solution, when the memory
allocated by the allocation function is released, if the return
addresses saved in the address return identifier, the allocation
parameters and the allocation orders saved in the allocation
identifier are cleared, it means that the current memory page is
released and is not called by the allocation function.
[0070] In a preferred embodiment, a macro configuration module is
provided for forming the address return identifier by
configuration.
[0071] In the above-mentioned technical solution, when operating
under the Linux, the return address of the function supported by
the compiler can be obtained through the macro configuration module
(i.e, the macro _RET_IP), that is to say, the caller of the
_alloc_pages_nodemask function is obtained.
[0072] The above descriptions are only the preferred embodiments of
the invention, not thus limiting the embodiments and scope of the
invention. Those skilled in the art should be able to realize that
the schemes obtained from the content of specification and drawings
of the invention are within the scope of the invention.
* * * * *