# US 2021/0334192 A1 · Method for detecting memory leak based on linux kernel

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2021/0334192 A1
- Document ID: US20210334192A1
- Status: Published application
- Kind: A1
- Application number: 16/325,175
- Filed: 2018-09-27
- Published: 2021-10-28
- Earliest claimed: 2017-11-07
- Assignees: Amlogic (Shanghai) Co., Ltd.
- Applicants: Amlogic (Shanghai) Co., Ltd.
- Inventors: Tao ZENG
- Web page: https://patentgrep.com/patent/US20210334192A1

## CPC

- G06F 11/3636: {by tracing the execution of the program}
- G06F 11/366: {using diagnostics (G06F11/0703 takes precedence)}
- G06F 9/5016: {the resource being the memory}
- G06F 11/302: {where the computing system component is a software system}
- G06F 2201/865: Monitoring of software
- G06F 2201/88: Monitoring involving counting
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## 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.

## Claims

### Claim 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.

### Claim 2 (depends on 1)

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.

### Claim 3 (depends on 2)

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.

### Claim 4 (depends on 2)

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.

### Claim 5 (depends on 4)

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.

### Claim 6 (depends on 4)

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.

### Claim 7 (depends on 3)

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.

### Claim 8 (depends on 2)

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.
