WO2009114985A1 - 内存泄漏检测装置及方法 - Google Patents

内存泄漏检测装置及方法 Download PDF

Info

Publication number
WO2009114985A1
WO2009114985A1 PCT/CN2008/073850 CN2008073850W WO2009114985A1 WO 2009114985 A1 WO2009114985 A1 WO 2009114985A1 CN 2008073850 W CN2008073850 W CN 2008073850W WO 2009114985 A1 WO2009114985 A1 WO 2009114985A1
Authority
WO
WIPO (PCT)
Prior art keywords
memory
log file
size
module
allocation
Prior art date
Application number
PCT/CN2008/073850
Other languages
English (en)
French (fr)
Inventor
刘攀
Original Assignee
中兴通讯股份有限公司
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 中兴通讯股份有限公司 filed Critical 中兴通讯股份有限公司
Publication of WO2009114985A1 publication Critical patent/WO2009114985A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/07Responding to the occurrence of a fault, e.g. fault tolerance
    • G06F11/0703Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation
    • G06F11/0751Error or fault detection not based on redundancy
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/07Responding to the occurrence of a fault, e.g. fault tolerance
    • G06F11/0703Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation
    • G06F11/0706Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation the processing taking place on a specific hardware platform or in a specific software environment
    • G06F11/073Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation the processing taking place on a specific hardware platform or in a specific software environment in a memory management context, e.g. virtual memory or cache management

Definitions

  • the present invention relates to the field of computer programming, and more particularly to an apparatus and method suitable for detecting program memory leaks.
  • C language A feature of the C language is the dynamic allocation of memory. Dynamic allocation of memory allows for flexible use of limited memory resources, but the problem is memory leaks, which often cause software crashes. For memory leaks, it is generally difficult to find unless a specific tool is used.
  • specific tools are: These software tools are highly dependent on the CPU chip, that is, if a different chip is replaced, a dedicated memory detection tool cannot be used for detection.
  • a memory leak detecting device is provided.
  • the memory leak detecting apparatus comprises: an initialization module for initializing a log file; a memory management module, configured to perform memory allocation after the initialization module initializes the log file, and perform memory release; The information about the memory allocation and the memory release is recorded in the log file; and the statistics module counts the number and size of the unreleased memory according to the information of the memory allocation and the memory release.
  • the foregoing initialization module is further configured to determine whether a log file exists, if not, Then create a log file.
  • the memory management module is further configured to determine whether the memory allocation is successful. If successful, the recording module is updated and records the number of memory allocations and records memory allocation information.
  • the foregoing memory allocation information includes: an address, a size, a calling module, a file, and a number of lines of the memory allocation.
  • the memory management module is further configured to determine whether the parameter pointer is empty, and if the parameter pointer is not empty, query whether the parameter pointer is allocated. If the parameter pointer is not allocated, record the parameter pointer and output the parameter pointer. If the parameter pointer is allocated, free the memory, update the number and size of memory releases, and delete the memory allocation information from the log file.
  • the foregoing statistics module is further configured to calculate a memory allocation number, a memory release number, an unreleased number, a total size of the memory allocation, a total size of the released memory, a size of the unreleased memory, and save the file to a log file and output.
  • a memory leak detecting method is provided.
  • the memory leak detection method includes the following steps: Step 1: After the memory is successfully allocated, the number of memory allocations is updated and recorded with the memory allocation information into the log file; Step 2, the parameter pointer is not Empty and the parameter pointer is allocated, frees memory, updates the number and size of memory releases and logs them to the log file, and removes memory allocation information from the log file; and step three, based on the memory recorded in the log file The number and size of allocations, as well as the number and size of memory releases, determine the number and size of memory that has not been freed.
  • the method before performing step one above, further comprises the steps of: setting a storage path of the log file; and determining whether a log file exists in the storage path, and if so, initializing the log file; if not, creating a log file , and initialize the created log file.
  • the foregoing memory allocation information includes: a memory allocation address, a memory allocation size, a memory allocation module, a file, and a number of rows.
  • the foregoing step 3 further includes the following steps: calculating a memory allocation number, a memory release number, a memory release number, a total memory allocation size, a total memory release size, an unreleased memory size, and recording to a log file and outputting them To the terminal; and record the address of the unreleased memory, the called module, file, and the number of rows to the log file, and output them to the terminal.
  • the memory space is allocated, released, and the memory usage is counted according to requirements, and the memory release detecting error and the memory leak information can be detected, thereby solving the memory leak existing in the related art.
  • FIG. 1 is a block diagram of a memory leak detecting apparatus according to an embodiment of the present invention
  • FIG. 2 is a flowchart of a memory leak detecting method according to an embodiment of the present invention
  • FIG. 3 is a flowchart according to the present invention.
  • FIG. 4 is a functional diagram of a memory leak detecting apparatus according to an embodiment of the present invention
  • FIG. 5 is a structure of a memory leak detecting apparatus according to an embodiment of the present invention
  • Figure 6 is a process flow diagram of a function call in accordance with one embodiment of the present invention
  • Figure 7 is a process flow diagram of a call interface lp_mck_set_log_path according to one embodiment of the present invention;
  • FIG. 9 is a flowchart of a process of calling the interface lp_mck_mem_aloc according to an embodiment of the present invention
  • Figure 11 is a process flow diagram of the call interface lp_mck_fmilize in accordance with one embodiment of the present invention.
  • embodiments of the present invention provide an improved memory leak detection scheme, specifically, a general-purpose, suitable for The apparatus and method for performing a memory leak check in a memory allocation system, in the embodiment of the present invention, the memory space can be allocated, released, and the memory usage is counted as needed, thereby detecting the memory leak information and detecting. Bad memory release.
  • the embodiments of the present invention are described in detail below with reference to the accompanying drawings. The features of the embodiments of the present invention and the embodiments may be combined with each other if they do not conflict.
  • a memory leak detecting apparatus is first provided.
  • a memory leak detecting apparatus 100 includes: an initialization module 102, a memory management module 104, a recording module 106, and statistics. Module 108.
  • the functions of the above modules are described in detail below.
  • the initialization module 102 is configured to initialize the log file.
  • the initialization module 102 is further configured to determine whether the log file exists, and if not, create a log file.
  • the memory management module 104 is configured to perform memory allocation after the initialization module initializes the log file, and perform memory release.
  • the memory management module 104 is further configured to determine whether the memory allocation is successful. If successful, the recording module 106 is caused to update and record the number of memory allocations and record the memory allocation information.
  • the memory allocation information includes: the address, size, calling module, file, and number of lines allocated by the memory.
  • the memory management module 104 is further configured to determine whether the parameter pointer is empty. If the parameter pointer is not empty, the query parameter pointer is allocated. If the parameter pointer is not allocated, the parameter pointer is recorded and Output, if the parameter pointer is allocated, release the memory, update the memory release times and large 'j, and delete the memory allocation information from the log file.
  • the recording module 106 is configured to record information about memory allocation and memory release into a log file.
  • the statistics module 108 calculates the number and size of the unreleased memory according to the information of the memory allocation and the memory release.
  • the statistics module 108 is further configured to calculate the number of memory allocations, the number of memory releases, the number of unreleased, the total size of the memory allocation, the total size of the released memory, the size of the unreleased memory, and save it to a log file and output. To the terminal, and save the address, calling module, file, and number of unreleased memory to the log file and output to the terminal.
  • FIG. 2 shows a flow of a method of detecting a memory leak according to an embodiment of the present invention.
  • the storage path of the log file may be set, and whether the log file exists in the storage path is determined, and if yes, the log file is initialized. If not, the log file is created, and the log file is created and created. The log file is initialized. Thereafter, the following steps (steps S202 to S206) shown in FIG. 2 are performed:
  • Step S202 after successfully allocating the memory, updating the number of times of memory allocation and recording the memory allocation information into the log file; wherein, the memory allocation information herein includes but is not limited to: a memory allocation address, a memory allocation size , memory allocation module, file, and number of lines, etc.
  • Step S204 When the parameter pointer is not empty and the parameter pointer is allocated, the memory is released, the number and size of the memory release are updated, the log is recorded in the log file, and the memory allocation information is deleted from the log file; (2) Step S206 Determines the number and size of memory not released according to the number and size of memory allocations recorded in the log file, and the number and size of memory releases.
  • step S206 can be implemented according to the following operations: calculating the number of memory allocations, the number of memory releases, the number of memory unreleased, the total memory allocation size, the total memory release size, the unreleased memory size, and logging to the log file and Output to the terminal; records the address of the unreleased memory, the called module, file, and the number of rows to the log file, and outputs them to the terminal.
  • the memory space can be allocated, released, and used for memory usage, and memory leak detection and error detection memory release can be realized.
  • FIG. 3 further illustrates a flow chart of a method of detecting a memory leak in accordance with one embodiment of the present invention. Referring to FIG.
  • the method for detecting a memory leak of the embodiment includes the following steps: Step S302, implementing an adaptation layer interface of the present invention; Step S304, setting a log storage path; Step S306, opening a memory check switch at compile time to compile; S308, exit after running the program once; Step S310, open the log, and view the memory statistics and memory leak modules, files, and the number of rows.
  • Step S302 implementing an adaptation layer interface of the present invention
  • Step S304 setting a log storage path
  • Step S306 opening a memory check switch at compile time to compile
  • S308 exit after running the program once
  • Step S310 open the log, and view the memory statistics and memory leak modules, files, and the number of rows.
  • Memory allocation log Record memory usage information by means of file recording
  • Memory allocation and release count statistics Count the memory allocation and release times of the program running once;
  • Memory allocation address and size record Record the specific address and size of the memory allocation;
  • Memory release error protection and recording When unallocated memory is released, error protection is performed, and output recording is performed;
  • FIG. 5 is a schematic structural diagram of a memory leak detecting apparatus according to an embodiment of the present invention.
  • the apparatus includes: an initialization module, a memory management module, and a termination module.
  • the device is capable of implementing the memory leak detection method shown in Figure 2 by calling three modules. In the specific implementation process, the above three modules can respectively call the following five interfaces: Interface
  • the initialization module can call interface 1 and interface 2, the memory management module can call interface 3 and interface 4, and the termination module can invoke interface 5. Tables 1 through 5 show the details of these five interfaces. Table 1
  • FIG. 6 is a process flow diagram of a function call according to an embodiment of the present invention.
  • the device shown in FIG. 5 can be used to call as shown in Tables 1 to 5. interface.
  • FIG. 6 is a process flow diagram of a function call according to an embodiment of the present invention.
  • the device shown in FIG. 5 can be used to call as shown in Tables 1 to 5. interface.
  • FIG. 6 is a process flow diagram of a function call according to an embodiment of the present invention.
  • the device shown in FIG. 5 can be used to call as shown in Tables 1 to 5. interface.
  • Step S602a and step S602b the interface 1 and the interface 2 are sequentially called in the place where the program is initialized, that is, lp_mck_set_log_path (int lp_mck_set_log_path(char *path) ) and lp mck init (
  • the interface 3 is called where the memory needs to be allocated, that is, lp_mck_mem_alloc(void * lp_mck_mem_alloc(size_z size, char* model_name));
  • step S606 Call interface 4 where you need to free up memory, ie, lp—mck— mem—free
  • Step S608 call interface 5 before the program terminates, that is, lp_mck_ finalize (int lp_mck_finilize (void)).
  • 7 is a flowchart of a process of calling the interface lp_mck_set_log_path according to an embodiment of the present invention, as shown in FIG. 7, calling the processing of the interface lp_mck_set_log_path (ie, the above interface 1)
  • the process includes: Step S702: Initialize a global string log path: lp_mck_path is NULL; Step S704, set a global string lp_mck_path as an incoming parameter.
  • FIG. 9 is a flowchart showing a process of calling the interface lp_mck_mem_alloc according to an embodiment of the present invention, as shown in FIG.
  • FIG. 10 is a flowchart of a process of the interface lp_mck_mem_free according to an embodiment of the present invention.
  • the processing flow of the call interface lp_mck_mem_free includes: Step S1002, The judgment parameter refers to whether 4 ⁇ is empty, if it is empty, it returns -1; otherwise, step S1004 is performed; step S1004, it is checked whether the pointer has been allocated; if not allocated, recording and outputting, returning -1; if assigned, Go to step S1006; Step S1006, release the memory (lp_mem_free()), update the number and size of memory releases, delete the memory allocation information from the record, and return 0.
  • FIG. 11 is a process flow diagram of an interface lp_mck_fmilize according to an embodiment of the present invention. As shown in FIG. 11, the interface int lp lm-mck-fmilize P is invoked, and the above interface 5) ⁇ ; Step S1102: Calculate and output the number of memory allocations, the number of releases, and the number of unreleased times to the log and the terminal. Step S1104: Calculate and output the total memory allocation size, the total release size, the unreleased size to the log, and the terminal; and step S1106. Output the address of the memory that was not released, the module called, the file, the number of rows to the file, and the terminal.
  • the memory space is allocated, released, and the memory usage is counted according to requirements, so that the memory of detecting the error is released, and the memory leak information is detected, thereby enabling It solves the problem of memory leaks in the related art, and is applicable to various systems that require programmers to allocate memory by themselves, and can also facilitate programmers to debug programs, and can enhance the stability and stability of the program.
  • the above is only the preferred embodiment of the present invention, and is not intended to limit the present invention, and various modifications and changes can be made to the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and scope of the present invention are intended to be included within the scope of the present invention.

Landscapes

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

Description

内存泄漏检测装置及方法
技术领域 本发明涉及计算机编程领域, 更具体地, 涉及一种适于检测程序内存泄 漏的装置和方法。 背景技术 目前, 嵌入式技术已经在多个领域得到应用。 在嵌入式领域, 由于资源 的有限性及性能要求, 该领域的编程语言一般均使用 C语言。 C语言的一个 特点就是内存的动态分配。 动态分配内存可以灵活地使用有限的内存资源, 但是带来的问题是内存的泄漏, 而内存泄漏经常会造成软件的崩溃等问题。 对于内存泄漏问题, 除非使用特定工具, 否则一般很难被发现。 但是, 使用特定工具的局限性在于: 这些软件工具对 CPU芯片的依赖 性很强, 即, 如果更换了不同的芯片, 就无法使用专用的内存检测工具来进 行检测。 而在嵌入式领域, 芯片的种类繁多, 内存检测工具却很少。 因此, 需要一种不依赖于特定工具的内存泄漏检测的解决方案。 发明内容 考虑到相关技术中需要一种不依赖于特定工具的内存泄漏检测的解决 方案的问题而提出本发明, 为此, 本发明的目的在于, 提供一种改进的内存 泄漏检测方案, 用以解决相关技术中的上述问题。 为了达到上述目的, 根据本发明的一个方面, 提供了一种内存泄漏检测 装置。 根据本发明的内存泄漏检测装置包括: 初始化模块, 用于对日志文件进 行初始化; 内存管理模块, 用于在初始化模块对日志文件进行了初始化之后 进行内存分配, 以及进行内存释放; 记录模块, 用于将关于内存分配和内存 释放的信息记录到日志文件中; 以及统计模块, 根据内存分配和内存释放的 信息统计未释放内存的次数和大小。 优选地, 上述初始化模块还用于判断日志文件是否存在, 如果不存在 , 则创建日志文件。 优选地, 上述内存管理模块还用于判断内存分配是否成功, 如果成功, 使记录模块更新并记录内存分配的次数以及记录内存分配信息。 优选地, 上述内存分配信息包括: 内存分配的地址、 大小、 调用模块、 文件、 和行数。 优选地, 上述内存管理模块还用于判断参数指针是否为空, 在参数指针 不为空的情况下查询参数指针是否被分配过, 如果参数指针未被分配过, 则 记录参数指针并将其输出, 如果参数指针被分配过, 则释放内存, 并更新内 存释放次数及大小, 并从日志文件中删除内存分配信息。 优选地, 上述统计模块还用于计算内存分配次数、 内存释放次数、 未释 放次数、 内存分配的总大小、 释放的内存的总大小、 未释放的内存的大小并 将其保存到日志文件以及输出至终端, 以及将未释放的内存的地址、 调用模 块、 文件、 行数保存到日志文件并输出至终端。 根据本发明的另一个方面, 提供了一种内存泄漏检测方法。 才艮据本发明的内存泄漏检测方法包括以下步骤: 步骤一, 在对内存成功 进行分配后,更新内存分配的次数并将其与内存分配信息记录到日志文件中; 步骤二, 在参数指针不为空且参数指针被分配过时, 释放内存, 更新内存释 放的次数及大小并将其记录到日志文件中, 以及从日志文件中删除内存分配 信息; 以及步骤三, 根据记录在日志文件中的内存分配的次数和大小、 以及 内存释放的次数和大小确定未释放内存的次数和大小。 优选地, 在执行上述步骤一之前, 该方法还包括以下步骤: 设置日志文 件的存储路径; 以及判断存储路径下是否存在日志文件, 如果有, 则对其进 行初始化, 如果没有, 则创建日志文件, 并对创建的日志文件进行初始化。 优选地, 上述内存分配信息包括: 内存分配地址、 内存分配大小、 内存 分配模块、 文件、 以及行数。 优选地, 上述步骤三还包括以下步骤: 计算内存分配次数、 内存释放次 数、 内存未释放次数、 内存分配总大小、 内存释放总大小、 未释放内存大小, 并将记录到日志文件以及将它们输出到终端; 以及将未释放内存的地址、 所 调用的模块、 文件、 以及行数记录到日志文件, 并将它们输出到终端。 通过发明上述技术方案之一, 根据需要对内存空间进行分配、 释放、 以 及对内存使用情况进行统计, 能够实现检测错误的内存释放、 检测出内存泄 漏信息, 从而能够解决相关技术中存在的内存泄漏的问题, 并且适用于各种 需要程序员自行分配内存的系统, 还能够方便程序员进行程序调试, 可以增 强程序的稳、定性。 本发明的其它特征和优点将在随后的说明书中阐述, 并且, 部分地从说 明书中变得显而易见, 或者通过实施本发明而了解。 本发明的目的和其他优 点可通过在所写的说明书、 权利要求书、 以及附图中所特别指出的结构来实 现和获得。 附图说明 此处所说明的附图用来提供对本发明的进一步理解,构成本申请的一部 分, 本发明的示意性实施例及其说明用于解释本发明, 并不构成对本发明的 不当限定。 在附图中: 图 1是根据本发明实施例的内存泄漏的检测装置的框图; 图 2是才艮据本发明实施例的内存泄漏的检测方法的流程图; 图 3是根据本发明的一个实施例的内存泄漏的检测方法的流程图; 图 4是根据本发明的一个实施例的内存泄漏检测装置的功能示意图; 图 5是才艮据本发明的一个实施例的内存泄漏检测装置的结构示意图; 图 6是根据本发明一个实施例的函数调用的处理流程图; 图 7是才艮据本发明一个实施例的调用接口 lp— mck— set— log_path的处理 流程图; 图 8是才艮据本发明一个实施例的调用接口 lp— mck— init的处理流程图; 图 9是才艮据本发明一个实施例的调用接口 lp— mck— mem— alloc的处理流 程图; 图 10是才艮据本发明一个实施例的调用接口 lp— mck— mem— free的处理流 程图; 图 11是才艮据本发明一个实施例的调用接口 lp— mck— fmilize的处理流程 图。 具体实施方式 功能相克述 考虑到相关技术中存在的内存泄漏的问题,本发明实施例提供了一种改 进的内存泄漏的检测方案, 具体地, 是一种通用的、 适用于才艮据需要在内存 分配的系统中进行内存泄漏检查的装置和方法, 在本发明实施例中, 能够根 据需要对内存空间进行分配、 释放、 以及对内存使用情况进行统计, 从而够 实现检测出内存泄漏信息、 检测错误的内存释放。 下面将结合附图来详细说明本发明的实施例, 应当理解, 此处所描述的 ύ选实施例仅用于说明和解释本发明, 并不用于限定本发明。 如果不沖突, 本发明实施例以及实施例中的特征可以相互组合。 装置实施例 根据本发明的实施例, 首先提供了一种内存泄漏的检测装置。 图 1是根据本发明实施例的内存泄漏的检测装置的框图, 参照图 1 , 根 据本发明装置实施例的内存泄漏检测装置 100 包括: 初始化模块 102 , 内存 管理模块 104 , 记录模块 106, 以及统计模块 108。 下面详细描述上述模块的 功能。 初始化模块 102 ,用于对日志文件进行初始化;优选地,初始化模块 102 还用于判断日志文件是否存在, 如果不存在, 则创建日志文件。 内存管理模块 104 , 用于在初始化模块对日志文件进行了初始化之后进 行内存分配, 以及进行内存释放。 优选地, 内存管理模块 104还用于判断内 存分配是否成功, 如果成功, 则使记录模块 106更新并记录内存分配的次数 以及记录内存分配信息。 其中, 内存分配信息包括: 内存分配的地址、 大小、 调用模块、 文件、 和行数。 优选地, 内存管理模块 104还用于判断参数指针 是否为空, 在参数指针不为空的情况下, 查询参数指针是否被分配过, 如果 参数指针未被分配过, 则记录参数指针并将其输出,如果参数指针被分配过, 则释放内存, 更新内存释放次数及大 'j、, 并从日志文件中删除内存分配信息。 记录模块 106, 用于将关于内存分配和内存释放的信息记录到日志文件 中。 统计模块 108 , 根据内存分配和内存释放的信息统计未释放内存的次数 和大小。 优选地, 统计模块 108还用于计算内存分配次数、 内存释放次数、 未释放次数、 内存分配的总大小、 释放的内存的总大小、 未释放的内存的大 小并将其保存到日志文件以及输出至终端, 以及将未释放的内存的地址、 调 用模块、 文件、 行数保存到日志文件并输出至终端。 通过本实施例提供的装置, 通过设置初始化模块, 内存管理模块, 记录 模块, 以及统计模块, 能够实现检测出内存泄漏信息、检测错误的内存释放。 方法实施例 根据本发明的实施例, 还提供了一种内存泄漏的检测方法。 优选地, 可 以采用上述装置实施例中提供的内存泄漏的检测装置来实现本实施例中提供 的内存泄漏的检测方法。 图 2示出了根据本发明实施例的内存泄漏的检测方法的流程。需要说明 的是, 在附图的流程图示出的步骤可以在诸如一组计算机可执行指令的计算 机系统中执行, 并且, 虽然在流程图中示出了逻辑顺序, 但是在某些情况下, 可以以不同于此处的顺序执行所示出或描述的步骤。 在执行本发明实施例时, 可选地, 可以设置日志文件的存储路径, 并判 断存储路径下是否存在日志文件, 如果有, 则对其进行初始化, 如果没有, 则创建日志文件, 并对创建的日志文件进行初始化。 之后, 进行到图 2所示 的以下步骤 (步骤 S202至步骤 S206 ):
(一) 步骤 S202 在对内存成功进行分配后,更新内存分配的次数并将其与内存分配信息 记录到日志文件中; 其中, 这里的内存分配信息包括但不限于: 内存分配地 址、 内存分配大小、 内存分配模块、 文件、 以及行数等。
(二) 步骤 S204 在参数指针不为空且参数指针被分配过时 , 释放内存, 更新内存释放的 次数及大小, 将其记录到日志文件中, 并从日志文件中删除内存分配信息; (二) 步骤 S206 根据记录在日志文件中的内存分配的次数和大小、以及内存释放的次数 和大小, 确定未释放内存的次数和大小。 优选地, 可以根据以下操作来实现步骤 S206: 计算内存分配次数、 内 存释放次数、 内存未释放次数、 内存分配总大小、 内存释放总大小、 未释放 内存大小, 并将记录到日志文件以及将它们输出到终端; 将未释放内存的地 址、 所调用的模块、 文件、 以及行数记录到日志文件, 并将它们输出到终端。 通过本实施例提供的技术方案, 能够根据需要对内存空间进行分配、释 放、 以及对内存使用情况进行统计, 能够实现检测出内存泄漏信息、 检测错 误的内存释放。 基于以上的描述,图 3进一步地示出了根据本发明的一个实施例的内存 泄漏的检测方法的流程图。 参照图 3 , 该实施例的内存泄漏的检测方法包括 以下步骤: 步骤 S302 , 实现本发明的适配层接口; 步骤 S304, 设置日志存储路径; 步骤 S306 , 编译时打开内存检查开关进行编译; 步骤 S308 , 运行一次程序后退出; 步骤 S310, 打开日志, 查看内存统计信息和内存泄漏模块、 文件、 行 数。 从上述的图 1至图 3可以看出,根据本发明实施例提供的内存泄漏的检 测装置和方法可以实现的功能包括但不限于如图 4所示的以下 9个方面:
( 1 ) 内存分配日志: 通过文件记录的方式记录内存的使用信息;
( 2 ) 内存分配、 释放次数统计: 统计程序运行一次的内存分配和释放 次数; ( 3 ) 内存分配地址、 大小纪录: 对内存分配具体地址、 大小进行记录;
( 4 ) 内存分配模块、 文件、 行数纪录: 通过参数将分配内存所在模块 进行记录;
( 5 ) 内存分配、 释放总大小统计: 统计内存分配、 释放总大小;
( 6 ) 未释放内存大小统计: 统计未释放内存大小;
( 7 ) 未释放内存地址、 所调模块、 文件、 行数记录; ( 8 ) 内存释放错误保护及纪录: 当释放未分配的内存时, 进行出错保 护, 并进行输出记录;
( 9 ) 通过宏开关控制是否进行内存检查, 实现 debug和 release版本: 通过宏进行控制是否进行内存检测统计, 以便进行 debug和 release版本。 以上分别通过装置实施例和方法实施例描述了 艮据本发明实施例的内 存泄漏的检测装置和方法, 以下将结合图 5至图 11 , 具体描述才艮据本发明实 施例的内存泄漏的检测装置和方法具体实施的实例。 图 5是根据本发明的一个实施例的内存泄漏检测装置的结构示意图,如 图 5所示, 该装置包括: 初始化模块、 内存管理模块、 以及终止化模块。 优 选地,该装置能够通过调用三个模块来实现如图 2所示的内存泄漏检测方法。 在具体实施的过程中, 上述三个模块可以分别调用以下 5个接口: 接口
1 : oid lp_mck_set_log_path、 接 口 2: int lp mck init、 接 口 3: lp—mck— mem— alloc、 接口 4: lp—mck— mem— free、 接口 5 : lp—mck— finalize。 其中, 初始化模块可以调用接口 1和接口 2 , 内存管理模块可以调用接 口 3和接口 4 , 终止化模块可以调用接口 5。 表 1至表 5分别示出了这五个 接口的详细信息。 表 1
名称 int lp mck set log_path
用途 设置日志路径
结构
Void lp mck set log_path(char *path)
序 分 元素填写说明
号 类 1 成功, 返回 0
失败, 返回— 1
2 I path: 曰志路径字符串 表 2
名称 lp mck init
用途 在程序初始化调用。用来根据 lp_mck_set_log_path所设置的路径来创 建日志。 _ _ _
结构
int lp mck init(void)
序 分 元素填写说明
号 类
1 成功, 返回 0
失败, 返回— 1
2 I Void 表 3
Figure imgf000010_0001
表 4
名称 lp mck mem free
用途 在释放内存时调用。 用来释放内存, 并对错误释放进行保护
结构
int lp mck mem free(void *p)
序号 分类 元素填写说明
1 成功, 返回 0
失败, 返回— 1 2 I p: 所要释放的内存指针 表 5
名称 lp mck finilize()
用途 在程序终止时调用。 用来进行统计计算及输出记录。
结构
int lp mck finilize (void)
序 分 元素填写说明
号 类
1 成功, 返回 0
失败, 返回— 1
2 I Void 图 6是根据本发明一个实施例的函数调用的处理流程图,在图 6所示的 处理中, 优选地, 可以使用图 5所示的装置调用如表 1至表 5所示的接口。 如图 6所示, 对于函数的调用顺序如下: 步骤 S602a和步骤 S602b, 在程序进行初始化的地方顺序调用接口 1和 接口 2 , 即, lp_mck_set_log_path ( int lp_mck_set_log_path(char *path) ) 和 lp mck init ( int lp mck init(void) ); 步骤 S604, 在需要分配内存的地方调用接口 3 , 即, lp— mck— mem— alloc ( void * lp_mck_mem_alloc(size_z size, char* model— name) ); 步骤 S606, 在需要释放内存的地方调用接口 4, 即, lp— mck— mem— free
( int lp— mck— mem— free(void *p) ); 步骤 S608 , 在程序终止化之前调用接口 5 , 即, lp— mck— finalize ( int lp— mck— finilize (void) )。 图 7是才艮据本发明一个实施例的调用接口 lp— mck— set— log_path的处理 流程图, 如图 7所示, 调用接口 lp— mck— set— log_path (即, 上述接口 1 ) 的 处理流程包括: 步骤 S702, 初始化全局的字符串日志路径: lp_mck_path为 NULL; 步骤 S704 , 设置全局的字符串 lp— mck_path为传入的参数。 图 8是才艮据本发明一个实施例的调用接口 lp— mck— init的处理流程图, 如图 8所示, 调用接口 lp— mck— init (即, 上述接口 2 ) 的处理流程包括: 步骤 S802 , 判断日志路径是否为空 (lp— mck_path ==NULL ), 如果为 空,设置初始化标志位为 - 1 ( lp— init— flag = 0 ) ); 并返回初始化标志位为 - 1 ; 否则执行下面的步骤 S804; 步骤 S804, 判断日志路径下日志文件是否存在, 如果不存在, 则创建, 创建失败返回初始 b标志位为 - 1 , 创建成功返回初始^ 示志位为 0; 如果存 在, 则返回初始化 志位为 0。 图 9是才艮据本发明一个实施例的调用接口 lp— mck— mem— alloc的处理流 程图, 如图 9所示, 调用接口 lp— mck— mem— alloc (即, 上述接口 3 ) 的处理 程包括: 步骤 S902, 判断是否初始化 (lp— init— flag == 1 ), 如果未初始化步骤, 调用初始化函数 ( lp— mck— init ); 否则执行步 3聚 S904; 步骤 S904, 判断是否初始化 (lp— init— flag == 1 ), 如果未初始化, 返回 NULL; 口果初:½化, 分 S己内存 ( lp— mem— alloc ( ) ); 步骤 S906, 判断分配内存是否成功, 如果失败, 返回 NULL;如果成功, 转入步骤 S908; 步骤 S908 , 更新分配内存的次数、 纪录所分配内存的地址、 大小、 调 用模块、 文件、 行数信息, 写入日志并输出。 图 10是才艮据本发明一个实施例的接口 lp— mck— mem— free的程序流程图, 如图 10所示, 调用接口 lp_mck_mem_free (即, 上述接口 4 ) 的处理流程包 括: 步 S 1002 , 判断参数指 4†是否为空, 如果为空, 返回- 1 ; 否则执行 步骤 S1004; 步骤 S1004 , 查询该指针是否曾经分配过; 如果未分配过,记录并输出, 返回- 1 ; 如果分配过, 转入步骤 S1006; 步骤 S1006 , 释放该内存 (lp_mem_free ( ) ), 更新内存释放次数及大 小, 从记录中删除内存分配信息, 并返回 0。 图 11是才艮据本发明一个实施例的接口 lp— mck— fmilize的处理流程图, ^口图 11所示, 调用接口 int lp— mck— fmilize P , 上述接口 5 ) ^;处理¾¾程包 括: 步骤 S1102 , 计算并输出内存分配次数、 释放次数、 未释放次数到日志 及终端; 步骤 S1104 , 计算并输出内存分配总大小、 释放总大小、 未释放大小到 日志及终端; 以及 步骤 S 1106 , 输出未释放内存的地址、 所调用模块、 文件、 行数到文件 及终端。 综上所述, 借助于本发明实施例提供的技术方案, 根据需要对内存空间 进行分配、 释放、 以及对内存使用情况进行统计, 能够实现检测错误的内存 释放、 检测出内存泄漏信息, 从而能够解决相关技术中存在的内存泄漏的问 题, 并且适用于各种需要程序员自行分配内存的系统, 还能够方便程序员进 行程序调试, 可以增强程序的稳、定性。 以上所述仅为本发明的优选实施例而已, 并不用于限制本发明, 对于本 领域的技术人员来说, 本发明可以有各种更改和变化。 凡在本发明的精神和 原则之内, 所作的任何修改、 等同替换、 改进等, 均应包含在本发明的保护 范围之内。

Claims

权 利 要 求 书
1. 一种内存泄漏检测装置, 其特征在于, 包括:
初始化模块, 用于对日志文件进行初始化;
内存管理模块,用于在所述初始化模块对所述日志文件进行了初始 化之后进行内存分配, 以及进行内存释放;
记录模块,用于将关于所述内存分配和所述内存释放的信息记录到 所述日志文件中;
统计模块,根据所述内存分配和所述内存释放的信息统计未释放内 存的次数和大小。
2. 根据权利要求 1所述的装置, 其特征在于, 所述初始化模块还用于判断 所述日志文件是否存在, 如果不存在, 则创建所述日志文件。
3. 根据权利要求 2所述的装置, 其特征在于, 所述内存管理模块还用于判 断所述内存分配是否成功, 如果成功, 使所述记录模块更新并记录内存 分配的次数以及记录内存分配信息。
4. 根据权利要求 3所述的装置, 其特征在于, 所述内存分配信息包括: 内 存分配的地址、 大小、 调用模块、 文件、 和行数。
5. 根据权利要求 4所述的装置, 其特征在于, 所述内存管理模块还用于判 断参数指针是否为空, 在所述参数指针不为空的情况下, 查询所述参数 指针是否被分配过, 如果所述参数指针未被分配过, 则记录所述参数指 针并将其输出, 如果所述参数指针被分配过, 则释放内存, 更新内存释 放次数及大' j、, 并从所述日志文件中删除所述内存分配信息。
6. 才艮据权利要求 5所述的装置, 其特征在于, 所述统计模块还用于执行如 下操作至少之一: 计算所述内存分配次数、 所述内存释放次数、 未释放 次数、 所述内存分配的总大小、 所述释放的内存的总大小、 未释放的内 存的大小并将其保存到所述日志文件以及输出至终端; 将未释放的内存 的地址、 所述调用模块、 所述文件、 所述行数保存到所述日志文件并输 出至所述终端。
7. 一种内存泄漏检测方法, 其特征在于, 包括以下步骤:
步骤一, 在对内存成功进行分配后, 更新内存分配的次数并将其与 内存分配信息记录到日志文件中;
步骤二,在参数指针不为空且所述参数指针被分配过时,释放内存, 更新内存释放的次数及大小并将其记录到所述日志文件中, 以及从所述 日志文件中删除所述内存分配信息;
步骤三, 根据记录在所述日志文件中的内存分配的次数和大小、 以 及所述内存释放的次数和大小确定未释放内存的次数和大小。
8. 根据权利要求 7所述的方法, 其特征在于, 在执行所述步骤一之前, 所 述方法还包括以下步骤:
设置所述日志文件的存储路径;
判断所述存储路径下是否存在日志文件, 如果有, 则对其进行初始 化, 如果没有, 则创建所述日志文件, 并对创建的所述日志文件进行初 始化。
9. 根据权利要求 8所述的方法, 其特征在于, 所述内存分配信息包括: 内 存分配地址、 内存分配大小、 内存分配模块、 文件、 以及行数。
10. 根据权利要求 9所述的方法, 其特征在于, 所述步骤三还包括以下处理: 计算内存分配次数、 内存释放次数、 内存未释放次数、 内存分配总 大小、 内存释放总大小、 未释放内存大小, 并将记录到所述日志文件以 及将它们输出到终端; 以及
将未释放内存的地址、 所调用的模块、 文件、 以及行数记录到所述 日志文件, 并将它们输出到所述终端。
PCT/CN2008/073850 2008-03-21 2008-12-30 内存泄漏检测装置及方法 WO2009114985A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN 200810084501 CN101539870A (zh) 2008-03-21 2008-03-21 内存泄漏检测装置及方法
CN200810084501.7 2008-03-21

Publications (1)

Publication Number Publication Date
WO2009114985A1 true WO2009114985A1 (zh) 2009-09-24

Family

ID=41090479

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2008/073850 WO2009114985A1 (zh) 2008-03-21 2008-12-30 内存泄漏检测装置及方法

Country Status (2)

Country Link
CN (1) CN101539870A (zh)
WO (1) WO2009114985A1 (zh)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107102922A (zh) * 2017-04-01 2017-08-29 北京三快在线科技有限公司 内存检测方法、装置及电子设备
CN113326120A (zh) * 2020-02-29 2021-08-31 杭州迪普科技股份有限公司 管理内存的装置和方法
CN113485846A (zh) * 2021-08-02 2021-10-08 北京字节跳动网络技术有限公司 一种内存处理方法及装置

Families Citing this family (24)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101763305B (zh) * 2009-12-29 2011-12-14 青岛海信宽带多媒体技术有限公司 一种嵌入式系统内存泄露的检测方法
CN102262582A (zh) * 2010-05-25 2011-11-30 芯讯通无线科技(上海)有限公司 移动终端及其内存泄露检测方法
CN102831068B (zh) * 2011-06-13 2016-04-06 阿里巴巴集团控股有限公司 一种内存操作记录的处理方法及装置
US8626993B2 (en) * 2011-11-18 2014-01-07 Apple Inc. Method for tracking memory usages of a data processing system
CN102866947A (zh) * 2012-08-29 2013-01-09 深圳市共进电子股份有限公司 一种Linux内核内存泄漏的检测方法
CN104182332B (zh) 2013-05-21 2017-09-29 华为技术有限公司 判断资源泄漏、预测资源使用情况的方法及装置
CN103268287B (zh) * 2013-06-05 2017-03-15 福州瑞芯微电子股份有限公司 检测内存泄露的方法与装置
CN103455424B (zh) * 2013-09-18 2015-12-02 哈尔滨工业大学 基于VxWorks操作系统的动态内存泄漏检测方法及装置
CN103546350B (zh) * 2013-11-06 2018-07-13 北京国双科技有限公司 日志生成的检测方法和装置
CN105302714B (zh) * 2014-07-24 2019-02-05 腾讯科技(深圳)有限公司 一种测试过程中内存泄漏的监控方法和装置
CN105446871B (zh) 2014-08-26 2018-08-17 华为技术有限公司 一种资源泄漏检测方法、装置及系统
WO2017035785A1 (zh) * 2015-09-01 2017-03-09 深圳好视网络科技有限公司 一种内存泄漏的定位方法和装置
CN105608017A (zh) * 2015-12-11 2016-05-25 中国航空工业集团公司西安航空计算技术研究所 一种存储器的控制电路及方法
CN106354627A (zh) * 2016-07-21 2017-01-25 努比亚技术有限公司 一种应用程序内存泄漏的检测方法及系统
CN106844207A (zh) * 2017-01-17 2017-06-13 上海斐讯数据通信技术有限公司 一种诊断内存泄漏的方法及设备
CN107436845A (zh) * 2017-07-27 2017-12-05 南京华飞数据技术有限公司 一种动态内存监测系统及其监测方法
CN107562586A (zh) * 2017-08-16 2018-01-09 努比亚技术有限公司 一种内存检测方法、设备及计算机存储介质
CN108182146B (zh) * 2017-12-27 2021-08-24 北京奇艺世纪科技有限公司 一种内存泄漏的检测方法及系统
CN108108258B (zh) * 2017-12-29 2020-11-06 杭州迪普科技股份有限公司 一种内存泄露的修复方法和装置
CN108733567A (zh) * 2018-05-24 2018-11-02 北京智芯微电子科技有限公司 软件资源泄露情况的监测方法及系统
CN109933525A (zh) * 2019-02-22 2019-06-25 深圳市吉祥腾达科技有限公司 一种通用的内存泄露检测方法
CN110704217A (zh) * 2019-08-27 2020-01-17 努比亚技术有限公司 一种内存泄露检测方法、终端及计算机可读存储介质
CN112000471B (zh) * 2020-08-10 2023-10-27 Vidaa(荷兰)国际控股有限公司 内存优化方法及装置
CN111984541A (zh) * 2020-08-31 2020-11-24 北京达佳互联信息技术有限公司 一种内存泄漏检测方法及装置、终端

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1286429A (zh) * 1999-11-20 2001-03-07 深圳市中兴通讯股份有限公司 一种嵌入处理机内存的检测和监控方法
CN1536788A (zh) * 2003-04-11 2004-10-13 华为技术有限公司 日志保留系统和方法
CN1979428A (zh) * 2005-12-08 2007-06-13 中兴通讯股份有限公司 自动释放嵌入式系统中应用程序模块泄漏内存的系统

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1286429A (zh) * 1999-11-20 2001-03-07 深圳市中兴通讯股份有限公司 一种嵌入处理机内存的检测和监控方法
CN1536788A (zh) * 2003-04-11 2004-10-13 华为技术有限公司 日志保留系统和方法
CN1979428A (zh) * 2005-12-08 2007-06-13 中兴通讯股份有限公司 自动释放嵌入式系统中应用程序模块泄漏内存的系统

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107102922A (zh) * 2017-04-01 2017-08-29 北京三快在线科技有限公司 内存检测方法、装置及电子设备
CN113326120A (zh) * 2020-02-29 2021-08-31 杭州迪普科技股份有限公司 管理内存的装置和方法
CN113326120B (zh) * 2020-02-29 2023-12-26 杭州迪普科技股份有限公司 管理内存的装置和方法
CN113485846A (zh) * 2021-08-02 2021-10-08 北京字节跳动网络技术有限公司 一种内存处理方法及装置

Also Published As

Publication number Publication date
CN101539870A (zh) 2009-09-23

Similar Documents

Publication Publication Date Title
WO2009114985A1 (zh) 内存泄漏检测装置及方法
US8326894B2 (en) Method and system to space-efficiently track memory access of object-oriented language in presence of garbage collection
CN101599046B (zh) 一种内存检测方法及装置
US10949342B2 (en) Persistent memory garbage collection
CN111324451B (zh) 一种基于lte协议栈的内存块越界的定位方法及其系统
US10354031B2 (en) Information processing by interpenetrating signal transmission channel in design for testability of chip
WO2010043176A1 (zh) 内存泄漏的检测方法和装置
CN107133144B (zh) 一种动态监测堆内存使用错误的内存监测装置及方法
US20110023052A1 (en) Compressed data managing system and method for circular buffer
US7434020B2 (en) Overwrite detection diagnostic for memory heap
CN103186468A (zh) 一种验证软件升级准确性的方法和装置
WO2021088423A1 (zh) 一种用于raid io的内存管理方法、系统、终端及存储介质
US8713538B2 (en) Monitoring heap in real-time by a mobile agent to assess performance of virtual machine
US9348723B2 (en) Method, system, and computer program product
KR102254159B1 (ko) 운영체제 커널 메모리의 실시간 오류 검출 방법
CN113987507A (zh) 堆内存漏洞检测方法、装置、存储介质及电子设备
US7350045B2 (en) Dynamic memory heap tagging
CN101266540A (zh) 信号处理算法中的动态内存处理操作的方法及装置
CN115576818A (zh) 一种内存越界检测方法、装置及电子设备和存储介质
CN103164290B (zh) 应用内存管理方法和装置
CN114442949A (zh) 一种垃圾数据回收方法及系统
CN103294606A (zh) 一种分配内存及检测内存溢出的方法和装置
US10514969B2 (en) Bit-accurate-tracing analysis with applied memory region lifetimes
US11010274B2 (en) Method and apparatus for trace data collection
CN116302560A (zh) 内存管理方法、电子设备及计算机存储介质

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 08873508

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 08873508

Country of ref document: EP

Kind code of ref document: A1