WO2017035785A1 - 一种内存泄漏的定位方法和装置 - Google Patents

一种内存泄漏的定位方法和装置 Download PDF

Info

Publication number
WO2017035785A1
WO2017035785A1 PCT/CN2015/088764 CN2015088764W WO2017035785A1 WO 2017035785 A1 WO2017035785 A1 WO 2017035785A1 CN 2015088764 W CN2015088764 W CN 2015088764W WO 2017035785 A1 WO2017035785 A1 WO 2017035785A1
Authority
WO
WIPO (PCT)
Prior art keywords
memory
information
program
recording space
application
Prior art date
Application number
PCT/CN2015/088764
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 深圳好视网络科技有限公司
Priority to PCT/CN2015/088764 priority Critical patent/WO2017035785A1/zh
Publication of WO2017035785A1 publication Critical patent/WO2017035785A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]

Definitions

  • the invention belongs to the field of software, and in particular relates to a method and device for locating a memory leak.
  • the code executed by the program and the data segment that needs to be used in the execution process are usually stored in the memory, thereby quickly executing the program code and efficiently reading the data in the data segment. .
  • the memory leak refers to a situation in which the program fails to release the memory that is no longer used due to negligence or error. Memory leak does not mean that the memory disappears physically. Instead, after the application allocates a certain amount of memory, it loses control of the memory due to programming errors or other reasons, thus causing memory waste.
  • Heap memory refers to the program is allocated from the heap, the size is arbitrary (the size of the memory block can be determined at the running time of the program), and the released memory must be displayed after use.
  • Application is generally used Malloc, realloc, new and other functions are allocated from the heap to a block of memory. After use, the program must be responsible for the corresponding call free or delete. The memory block is released. Otherwise, the memory cannot be used again, that is, the memory leak occurs.
  • the total memory occupied by the program is generally monitored in real time.
  • the current program is considered to have a memory leak, and the fault is located through the memory check.
  • This method requires a lot of effort in locating the location of the memory leak, and the location of the memory leak is inefficient.
  • an embodiment of the present invention provides a method for locating a memory leak, where the method includes:
  • the memory required to be requested according to the program code, and the preset recording space consecutive to the address of the memory to be applied, constitute the actually allocated memory, and the requested memory information is recorded in the recording space.
  • the memory information requested in the corresponding recording space is searched and updated according to the released memory, and the memory information of the application in the recording space is updated to the tracking file;
  • the memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the program code is configured by a plurality of different program files, and before the memory application step, the method further includes:
  • mapping result of the mapping operation is updated according to the mapping operation.
  • the step of recording the requested memory information and the code location information in the recording space includes:
  • Recording a program file name for requesting memory, a program line number for requesting memory, and a size of the application memory are recorded at a predetermined position in the memory segment of the preset length.
  • the step of recording the requested memory information and the code location information in the recording space includes:
  • the newly applied memory value is added to the memory information requested in the recording space.
  • the updating, in the memory space, the memory information in the recording space, and updating the memory information of the application in the recording space to the The steps to track files include:
  • the released memory is all the memory in the requested memory information in the recording space, the data of the recording space is deleted and the memory corresponding to the recording space is released, and the memory information requested in the updated tracking file is 0. Or deleting the record information of the program line number in the trace file;
  • the memory information requested in the recording space is subtracted from the released memory value, and the result of the subtraction is updated to the trace file.
  • an embodiment of the present invention provides a memory leak location device, where the device includes:
  • the memory application unit is configured to, in each memory application, a memory that needs to be applied according to the program code, and a preset recording space that is continuous with the address of the memory that needs to be applied, to constitute an actually allocated memory, in the recording space. Recording the memory information and code location information of the application, and writing the memory information and code location information of the application into the tracking file;
  • a memory release unit configured to: according to the released memory, find and update the memory information requested in the corresponding recording space, and update the memory information of the application in the recording space to the trace file.
  • a positioning unit configured to perform memory leak location according to the memory information of the application recorded by the trace file and the code location information.
  • the device further includes:
  • a location mapping unit configured to perform mapping operations on the program file, and establish a mapping relationship between the mapping result of the mapping operation and the storage location in the trace file;
  • the location mapping update unit is configured to update, according to the mapping operation, a correspondence between a mapping result of the mapping operation and a storage location of the trace file, after the program file is modified.
  • the memory application unit includes:
  • the memory application determining subunit is configured to determine whether the program line number of the current program file has applied for memory
  • a memory segment obtaining subunit configured to acquire a memory segment of a preset length, the address of the memory segment, and the program, if the program line number of the current program file does not apply for memory, before the program code needs to apply for the memory address
  • the address of the memory that the code needs to apply is continuous;
  • the program information recording subunit is configured to record, in a predetermined position in the memory segment of the preset length, a program file name that needs to apply for a memory, a program line number that needs to apply for a memory, and a size of the application memory.
  • the memory application unit includes:
  • the memory application determining subunit is configured to determine whether the program line number of the current program file has applied for memory
  • the memory value accumulates the subunit for accumulating the newly applied memory value into the memory information requested in the recording space if the program line number of the current program file has already applied for memory.
  • the memory release unit includes:
  • a memory release determining subunit configured to determine whether the released memory is all memory in the memory information of the application in the recording space
  • a recording space release subunit configured to: if the released memory is all memory in the requested memory information in the recording space, delete the data of the recording space and release the memory corresponding to the recording space, and update the tracking correspondingly
  • the memory information requested in the file is 0 or delete the record information of the program line number in the trace file;
  • a recording space update subunit configured to: if the released memory is not all the memory in the requested memory information in the recording space, subtract the memory information requested in the recording space from the released memory value, and subtract the same The result is updated to the trace file.
  • Space when the memory is released, according to the positional relationship between the recording space and the applied memory, the released memory information is updated to the recording space, and the information of the recording space is displayed by the trace file, so that the user can quickly find the leaked memory. Size and position corresponding to positioning, improve the positioning efficiency of memory leaks.
  • FIG. 1 is an implementation flow of a method for locating a memory leak according to a first embodiment of the present invention
  • FIG. 2 is a flowchart for implementing a method for establishing a correspondence relationship between a program file and a storage location in a trace file according to a second embodiment of the present invention
  • FIG. 3 is an implementation flow of a method for locating a memory leak according to a third embodiment of the present invention.
  • FIG. 4 is an implementation flow of a method for locating a memory leak according to a fourth embodiment of the present invention.
  • FIG. 5 is a flowchart showing an implementation process of a memory leak location method according to a fifth embodiment of the present invention.
  • FIG. 6 is a flowchart showing an implementation process of a memory leak location method according to a sixth embodiment of the present invention.
  • the purpose of the embodiments of the present invention is to solve the problem of low memory leak location efficiency when running an application in the prior art. Because the code in the application will call repeated memory or release, due to programmer negligence or other reasons, the program calls too much memory, but does not release the memory in time, causing memory leaks, which makes up too much system resources, affecting other The normal operation of the program.
  • the prior art positioning method In order to efficiently perform memory leak location, the prior art positioning method generally determines whether a memory leak occurs by monitoring the total memory usage of the application, and further locates the memory leak according to the running logic of the program. The positioning speed is slow and it is not easy to accurately locate the location of the memory leak. To overcome this problem, the following detailed description will be made with reference to the accompanying drawings.
  • Embodiment 1 is a diagrammatic representation of Embodiment 1:
  • FIG. 1 is a flowchart showing an implementation process of a memory leak location method according to a first embodiment of the present invention, which is described in detail as follows:
  • step S101 In each memory application, the memory required to be applied according to the program code, and the preset recording space continuous with the address of the memory to be applied, constitute the actually allocated memory, and the application is recorded in the recording space. Memory information and code location information, and write the memory information and code location information of the application to the trace file.
  • the memory application and the memory release described in the embodiment of the present invention can be understood as a memory application proposed by the application when executed.
  • a memory application proposed by the application when executed.
  • use malloc , realloc , new and other functions are allocated from the heap to a block of memory, after use, the program must be responsible for the corresponding call free or delete Release the memory block.
  • information including the size of the memory to be applied for, or information on whether or not to further increase the memory space is included.
  • the program code described in the embodiment of the present invention that is, the program statement of the application program.
  • the process of executing the statements constituting the application is executed repeatedly.
  • the memory application statement in the application is executed, the memory is allocated for the application, and when the execution of the memory release statement is executed, the application is released.
  • the specified location or the memory space of the specified data is not limited to the program statement of the application program.
  • the recording space described in the embodiment of the present invention refers to an additional memory segment in the embodiment of the present invention when the application program is executed to the code location under normal conditions, and the memory application is presented to the system and the memory allocated by the system is obtained.
  • the memory allocation is generally a contiguous address
  • the added memory address of the memory segment is continuous with the address of the requested memory.
  • the preferred implementation manner is: setting the address of the added memory segment before the address of the applied memory, so that the subsequent application memory can be continued after the requested memory.
  • the accumulation makes the application of memory continuous storage.
  • the address of the recording space is set before the address of the memory of the application, and when the memory is released later, the recording space can be found according to the starting position of the memory occupied by the program code.
  • the actually allocated memory includes two parts, that is, the memory occupied by the program code and the memory occupied by the recording space.
  • the size of the recording space is generally preset. For example, according to the type of information that needs to be recorded in the recording space, the memory size of the recording space can be set accordingly.
  • the actually allocated memory consists of two parts, the recording space and the requested memory, where the recording space includes the file name and the program line number.
  • the size of the memory required for the recording space can be determined based on the maximum space occupied by the name and the maximum space occupied by the program line number.
  • the recording space includes the requested memory information and code location information, where the memory information of the application may be the size of the applied memory, such as an application. 128 bytes of memory, if the size of the memory occupied by the record space is 64 bytes, then the system will return a continuous 192 bytes of memory space according to the request of the memory request, in the first 64
  • the memory information and code location information used as the recording space storage request in the bytes are used as data storage in the program code in the last 128 bytes.
  • Location information of the code an application composed of a plurality of program files (in practice, a general application is composed of a plurality of program files), may include the name of the program file, and be in the same application The names of any two program files are not the same.
  • the embodiment of the present invention is used for locating a memory leak, and the location information of the code further includes a line number of the application. In the same application, the line number corresponding to each line of the program code is unique.
  • the size of the memory used in the specific location of the application code can be accurately located.
  • code position information is only one of the better representations by the name of the program file and the program line number.
  • the representation of the code position information of the present application is not limited to the above one.
  • the application further includes updating the information of the record space record to the trace file, including information such as a program file name, a program line number, and a memory size corresponding to the memory.
  • the trace file may be a text file generated when the application is executed, and stored in a non-volatile memory, and the program file corresponding to the memory allocation in the file and the program may still be viewed after the program is restarted or powered off. Line number.
  • the generation of the trace file may be performed each time the memory application is executed in the application and step S102
  • the memory described in the memory is released into the information of the recording space before the memory address of the application is printed, and the information of the recording space is updated to the tracking file.
  • step S102 In each memory release, the memory information requested in the corresponding recording space is searched for and updated according to the released memory, and the memory information of the application in the recording space is updated to the trace file.
  • the memory release described in the embodiment of the present invention can be understood as a memory application proposed by the application when it is executed. For example, when the application is executing, use free or delete Release the memory block.
  • the start address of the released memory is received, and according to the start address and the size of the preset recording space, the start address and the end address of the record space can be determined correspondingly (all of the program line numbers are released)
  • the end address of the recording space is continuous with the start address of the address of the application. According to the address of the recording space, it is possible to view the information of the memory of the corresponding application recorded in the recording space and the line number corresponding to the statement of the application for memory.
  • the corresponding update of the information in the record space in the memory release, generally only need to update the memory information requested in the record space (of course, the record space will be deleted when all released)
  • the recorded information can be updated to the tracking file by printing or other means.
  • a step S101 and a step S102 Execution does not need to be performed in strict accordance with the sequence, and the corresponding execution steps can be selected according to the program statements required for execution. For example, if the memory release statement is encountered, step S102 is executed, and when the memory application statement is encountered, step S101 is performed. .
  • step S103 memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the information recorded in the trace file in the embodiment of the present invention can be found in each memory application and each memory release, and when the memory is released, the corresponding row position of the corresponding memory application statement can be found, thereby being fast. Effectively determine the specific location of the memory application and improve the positioning efficiency of the deposit leak.
  • Embodiment 2 is a diagrammatic representation of Embodiment 1:
  • figure 2 The implementation flow of the method for establishing a storage location correspondence relationship between the program file and the trace file provided by the second embodiment of the present invention is shown, which can be applied to the memory application step or the memory release step in the first embodiment.
  • the program code is composed of a plurality of different program files, and the method is detailed as follows:
  • step S201 The mapping operation is performed on the program file, and the mapping relationship between the mapping result of the mapping operation and the storage location in the trace file is established.
  • the present application further includes establishing a correspondence between the mapping result and the storage location of the trace file.
  • the mapping operation described in the embodiment of the present invention may be performed by using a hash algorithm.
  • the hash algorithm can map binary values of arbitrary length into shorter fixed-length binary values, and the shorter binary values are called hash values.
  • a hash value is a unique and extremely compact numerical representation of a piece of data. If you hash a plaintext and even change only one letter of the paragraph, subsequent hashes will produce different values. It is computationally impossible to find two different inputs that are hashed to the same value.
  • the mapping operation may further include: mapping the program file and the line number of the program, and when the program line number is used for the memory application, causing the memory operation of the same line of the same program file. The information is automatically updated to the same location.
  • step S202 After the program file is modified, the correspondence between the mapping result of the mapping operation and the storage location of the trace file is updated according to the mapping operation.
  • Embodiment 3 is a diagrammatic representation of Embodiment 3
  • FIG. 3 shows an implementation flow provided by a third embodiment of the present invention, which is described in detail as follows:
  • step S301 In each memory application, the memory that needs to be applied according to the program code, and the preset recording space that is continuous with the address of the memory that needs to be applied, constitute the actually allocated memory.
  • step S302 it is determined whether the program line number of the current program file has applied for memory.
  • the purpose of determining whether to apply for the line number for the first time is to determine whether it is necessary to allocate a memory segment corresponding to the recording space according to the judgment result.
  • step S303 If the program line number of the current program file does not apply for memory, before the program code needs to apply for the memory address, obtain a memory segment of a preset length, the address of the memory segment and the memory of the program code to be applied for. The address is continuous.
  • the memory segment corresponding to the recording space needs to be allocated, and the information about the memory application is recorded in the recording space, including the program file name, the program line number for which the memory needs to be applied, and The size of the application memory.
  • step S304 The program file name of the memory to be applied for, the program line number of the memory to be applied for, and the size of the application memory are recorded in a predetermined position in the memory segment of the preset length.
  • the acquisition needs to be released.
  • the record space is found according to the address, so that the program file and the program line number of the application memory can be found, and the information in the trace file is modified according to the location of the found memory application.
  • step S305 In each memory release, the memory information requested in the corresponding recording space is searched for and updated according to the released memory, and the memory information of the application in the recording space is updated to the trace file.
  • step 306 memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the embodiment of the present invention introduces a method for setting a recording space when a new memory is applied.
  • the program file name and the program line number of the corresponding memory application can be found when the memory is released, which can be more effective for each
  • the memory in the program line number of the memory application is managed and monitored.
  • Embodiment 4 is a diagrammatic representation of Embodiment 4:
  • FIG. 4 is a flowchart showing an implementation process of a memory leak location method according to a fourth embodiment of the present invention, which is described in detail as follows:
  • step S401 In each memory application, the memory that needs to be applied according to the program code, and the preset recording space that is continuous with the address of the memory that needs to be applied, constitute the actually allocated memory.
  • step S402 it is determined whether the program line number of the current program file has applied for memory.
  • step S403 If the program line number of the current program file has already applied for memory, the newly applied memory value is added to the memory information requested in the recording space.
  • the newly added memory is generally after the previously applied memory, and the previously applied memory and The newly added memory address is continuous. That is, when the first memory application is made, an expandable memory space is reserved for subsequent increased memory usage.
  • step S404 In each memory release, the memory information requested in the corresponding recording space is searched for and updated according to the released memory, and the memory information of the application in the recording space is updated to the trace file.
  • the memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the embodiment of the present invention and the third embodiment are descriptions of two different situations when the program code is executed.
  • the memory application is applied again to the program code that has applied for the recording space, only the data of the memory information in the recording space needs to be updated and updated. The data is updated accordingly to the trace file.
  • Embodiment 5 is a diagrammatic representation of Embodiment 5:
  • FIG. 5 is a flowchart showing an implementation process of a memory leak location method according to a fifth embodiment of the present invention, which is described in detail as follows:
  • step S501 In each memory application, the memory required to be applied according to the program code, and the preset recording space continuous with the address of the memory to be applied, constitute the actually allocated memory, and the application is recorded in the recording space. Memory information and code location information, and write the memory information and code location information of the application to the trace file.
  • step S502 it is determined whether the released memory is all of the memory in the requested memory information in the recording space.
  • memory release there are generally two cases, one is to release all the memory in the memory of the same program line number application, and the other is to release part of the memory when the same program line number is applied for memory multiple times.
  • step S503 If the released memory is all the memory in the memory information of the application in the recording space, delete the data of the recording space and release the memory corresponding to the recording space, and update the memory information applied in the tracking file accordingly. Is 0 Or deleting the record information of the program line number in the trace file.
  • the data of the recording space may be deleted, or the memory corresponding to the recording space may be directly released.
  • the data information corresponding to the program line number may be deleted, or the memory size in the data information corresponding to the program line number may be changed to 0.
  • step S504 If the released memory is not all of the memory in the requested memory information in the recording space, the memory information requested in the recording space is subtracted from the released memory value, and the subtracted result is updated to the tracking. file.
  • the method can obtain the data of the recording space according to the forwarded memory. And update the data value of the record space accordingly.
  • the memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the corresponding data manner for finding and updating the recording space is specifically introduced, and the search space is quickly and efficiently searched and updated for the manner of releasing the memory part and releasing all the memory.
  • Embodiment 4 is a diagrammatic representation of Embodiment 4:
  • FIG. 4 is a flowchart showing an implementation process of a memory leak location method according to a fourth embodiment of the present invention, which is described in detail as follows:
  • step S401 In each memory application, the memory that needs to be applied according to the program code, and the preset recording space that is continuous with the address of the memory that needs to be applied, constitute the actually allocated memory.
  • step S402 it is determined whether the program line number of the current program file has applied for memory.
  • step S403 If the program line number of the current program file has already applied for memory, the newly applied memory value is added to the memory information requested in the recording space.
  • the newly added memory is generally after the previously applied memory, and the previously applied memory and The newly added memory address is continuous. That is, when the first memory application is made, an expandable memory space is reserved for subsequent increased memory usage.
  • step S404 In each memory release, the memory information requested in the corresponding recording space is searched for and updated according to the released memory, and the memory information of the application in the recording space is updated to the trace file.
  • the memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the embodiment of the present invention and the third embodiment are descriptions of two different situations when the program code is executed.
  • the memory application is applied again to the program code that has applied for the recording space, only the data of the memory information in the recording space needs to be updated and updated. The data is updated accordingly to the trace file.
  • Embodiment 5 is a diagrammatic representation of Embodiment 5:
  • FIG. 5 is a flowchart showing an implementation process of a memory leak location method according to a fifth embodiment of the present invention, which is described in detail as follows:
  • step S501 In each memory application, the memory required to be applied according to the program code, and the preset recording space continuous with the address of the memory to be applied, constitute the actually allocated memory, and the application is recorded in the recording space. Memory information and code location information, and write the memory information and code location information of the application to the trace file.
  • step S502 it is determined whether the released memory is all of the memory in the requested memory information in the recording space.
  • memory release there are generally two cases, one is to release all the memory in the memory of the same program line number application, and the other is to release part of the memory when the same program line number is applied for memory multiple times.
  • step S503 If the released memory is all the memory in the memory information of the application in the recording space, delete the data of the recording space and release the memory corresponding to the recording space, and update the memory information applied in the tracking file accordingly. Is 0 Or deleting the record information of the program line number in the trace file.
  • the data of the recording space may be deleted, or the memory corresponding to the recording space may be directly released.
  • the data information corresponding to the program line number may be deleted, or the memory size in the data information corresponding to the program line number may be changed to 0.
  • step S504 If the released memory is not all of the memory in the requested memory information in the recording space, the memory information requested in the recording space is subtracted from the released memory value, and the subtracted result is updated to the tracking. file.
  • the method can obtain the data of the recording space according to the forwarded memory. And update the data value of the record space accordingly.
  • the memory leak location is performed according to the memory information of the application recorded by the trace file and the code location information.
  • the corresponding data manner for finding and updating the recording space is specifically introduced, and the search space is quickly and efficiently searched and updated for the manner of releasing the memory part and releasing all the memory.
  • FIG. 6 is a schematic structural diagram of a memory leak location device according to an embodiment of the present invention, which is described in detail as follows:
  • Memory application unit 601 For each memory application, the memory to be applied according to the program code, and the preset recording space continuous with the address of the memory to be applied, constitute the actually allocated memory, and the application is recorded in the recording space. Memory information and code location information, and writing the memory information and code location information of the application into the tracking file;
  • Memory release unit 602 And, for each memory release, searching and updating the memory information requested in the corresponding recording space according to the released memory, and updating the memory information of the application in the recording space to the tracking file;
  • Positioning unit 603 And performing memory leak location according to the memory information of the application recorded by the trace file and the code location information.
  • the device further comprises:
  • a location mapping unit configured to perform mapping operations on the program file, and establish a mapping relationship between the mapping result of the mapping operation and the storage location in the trace file;
  • the location mapping update unit is configured to update, according to the mapping operation, a correspondence between a mapping result of the mapping operation and a storage location of the trace file, after the program file is modified.
  • the memory application unit comprises:
  • the memory application determining subunit is configured to determine whether the program line number of the current program file has applied for memory
  • a memory segment obtaining subunit configured to acquire a memory segment of a preset length, the address of the memory segment, and the program, if the program line number of the current program file does not apply for memory, before the program code needs to apply for the memory address
  • the address of the memory that the code needs to apply is continuous;
  • the program information recording subunit is configured to record, in a predetermined position in the memory segment of the preset length, a program file name that needs to apply for a memory, a program line number that needs to apply for a memory, and a size of the application memory.
  • the memory application unit comprises:
  • the memory application determining subunit is configured to determine whether the program line number of the current program file has applied for memory
  • the memory value accumulates the subunit for accumulating the newly applied memory value into the memory information requested in the recording space if the program line number of the current program file has already applied for memory.
  • the memory release unit comprises:
  • a memory release determining subunit configured to determine whether the released memory is all memory in the memory information of the application in the recording space
  • a recording space release subunit configured to: if the released memory is all memory in the requested memory information in the recording space, delete the data of the recording space and release the memory corresponding to the recording space, and update the tracking correspondingly
  • the memory information requested in the file is 0 or delete the record information of the program line number in the trace file;
  • a recording space update subunit configured to: if the released memory is not all the memory in the requested memory information in the recording space, subtract the memory information requested in the recording space from the released memory value, and subtract the same The result is updated to the trace file.
  • the device for locating a memory leak according to the embodiment of the present invention corresponds to the method for locating a memory leak according to the first embodiment to the fifth embodiment, and details are not described herein.
  • the disclosed apparatus and method may be implemented in other manners.
  • the device embodiments described above are merely illustrative.
  • the division of the unit is only a logical function division.
  • there may be another division manner for example, multiple units or components may be combined or Can be integrated into another system, or some features can be ignored or not executed.
  • the mutual coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interface, device or unit, and may be in an electrical, mechanical or other form.
  • the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of the embodiment.
  • each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
  • the above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
  • the integrated unit if implemented in the form of a software functional unit and sold or used as a standalone product, may be stored in a computer readable storage medium.
  • the technical solution of the present invention which is essential or contributes to the prior art, or all or part of the technical solution, may be embodied in the form of a software product stored in a storage medium.
  • a number of instructions are included to cause a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the methods described in various embodiments of the present invention.
  • the foregoing storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory )
  • ROM read-only memory
  • RAM Random Access Memory

Landscapes

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

Abstract

一种内存泄漏的定位方法,包括:在每次内存申请时,根据程序代码需要申请的内存,以及预设的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息并写入跟踪文件(S101);在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件(S102);根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位(S103)。通过设置记录空间,并且在内存释放时查找对应的记录空间进行内存数据的更新,并相应的更新到跟踪文件,可以使得用户可以快速准确的定位内存泄漏的位置,提高内存泄漏的定位效率。

Description

一种内存泄漏的定位方法和装置 技术领域
本发明属于软件领域,尤其涉及一种内存泄漏的定位方法和装置。
背景技术
在计算机执行软件程序时,为了提高程序的运行速度,通常将程序执行的代码,以及执行过程中需要使用数据段存储在内存中,从而快速的执行程序代码以及高效的读取数据段中的数据。
在进行计算机编程时,由于编程者的疏忽或者程序运行的故障,可能会遇到内存泄漏的现象,当程序由于内存泄漏占用大量的内存时,不仅会影响到其它程序的正常运行,也会导致程序本身的运行速度受到影响,严重时甚至会出现死机现象。
其中,所述内存泄漏,是指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况。内存泄漏并非指内存在物理上的消失,而是应用程序将某段内存分配后,由于程序设计的错误或者其它原因,失去了对该段内存的控制,因而造成了内存的浪费。通常我们所说的内存泄漏是指堆内存的泄漏。堆内存是指程序从堆中分配的,大小任意的(内存块的大小可以在程序运行期决定),使用完后必须显示释放的内存。应用程序一般使用 malloc , realloc , new 等函数从堆中分配到一块内存,使用完后,程序必须负责相应的调用 free 或 delete 释放该内存块,否则,这块内存就不能被再次使用,即出现所述的内存泄漏。
目前,对于程序运行的内存泄漏监控,一般通过实时的对程序占用的总内存进行监控,当占用的内存总值超过预定的值时,则认为当前程序发生内存泄漏,通过内存排查进行故障定位,这种方式在定位内存泄漏位置时,需要消耗大量的精力,并且内存泄漏的定位效率低。
技术问题
本发明的目的在于提供一种内存泄漏的定位方法,以解决现有技术在定位内存泄漏位置时,需要消耗大量的精力,并且内存泄漏的定位效率低的问题。
技术解决方案
第一方面,本发明实施例提供了一种内存泄漏的定位方法,所述方法包括:
在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件;
在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件;
根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
结合第一方面,在第一方面的第一种可能实现方式中,所述程序代码由多个不同的程序文件构成,在所述内存申请步骤之前,所述方法还包括:
对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系;
当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
结合第一方面,在第一方面的第二种可能实现方式中,所述在所述记录空间中记录申请的内存信息及代码位置信息步骤包括:
判断当前程序文件的程序行号是否已经申请内存;
如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续;
在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
结合第一方面,在第一方面的第三种可能实现方式中,所述在所述记录空间中记录申请的内存信息及代码位置信息步骤包括:
判断当前程序文件的程序行号是否已经申请内存;
如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
结合第一方面,在第一方面的第四种可能实现方式中,所述根据释放的内存更新所述记录空间中的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件步骤包括:
判断释放的内存是否为记录空间中的申请的内存信息中的全部内存;
如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息;
如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
第二方面,本发明实施例提供了一种内存泄漏的定位装置,所述装置包括:
内存申请单元,用于在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件;
内存释放单元,用于在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件;
定位单元,用于根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
结合第二方面,在第二方面的第一种可能实现方式中,所述装置还包括:
位置映射单元,用于对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系;
位置映射更新单元,用于当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
结合第二方面,在第二方面的第二种可能实现方式中,所述内存申请单元包括:
内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
内存段获取子单元,用于如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续;
程序信息记录子单元,用于在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
结合第二方面,在第二方面的第三种可能实现方式中,所述内存申请单元包括:
内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
内存值累加子单元,用于如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
结合第二方面,在第二方面的第四种可能实现方式中,所述内存释放单元包括:
内存释放判断子单元,用于判断释放的内存是否为记录空间中的申请的内存信息中的全部内存;
记录空间释放子单元,用于如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息;
记录空间更新子单元,用于如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
有益效果
在本发明中,在每次内存申请时,除分配需要申请的内存外,还包括与需要申请的内存连续的记录空间的内存,将每次申请的内存信息以及代码位置信息写入所述记录空间,当内存释放时,根据记录空间与申请的内存的位置关系,将释放的内存信息更新至所述记录空间,通过跟踪文件显示所述记录空间的信息,可以方便用户快速的查找泄露内存的大小以及定位对应的位置,提高内存泄漏的定位效率。
附图说明
图 1 是本发明第一实施例提供的内存泄漏的定位方法的实现流程;
图 2 是本发明第二实施例提供的程序文件与跟踪文件内的存储位置对应关系建立方法的实现流程;
图 3 是本发明第三实施例提供的内存泄漏的定位方法的实现流程;
图 4 是本发明第四实施例提供的内存泄漏的定位方法的实现流程;
图 5 是本发明第五实施例提供的内存泄漏的定位方法的实现流程;
图 6 是本发明第六实施例提供的内存泄漏的定位方法的实现流程。
本发明的实施方式
为了使本发明的目的、技术方案及优点更加清楚明白,以下结合附图及实施例,对本发明进行进一步详细说明。应当理解,此处所描述的具体实施例仅仅用以解释本发明,并不用于限定本发明。
本发明实施例的目的在于解决现有技术中运行应用程序时,内存泄漏定位效率低的问题。由于应用程序中的代码会调用反复的内存或者释放,由于编程人员疏忽或者其它原因,导致程序调用过多内存,但并没有及时释放内存,造成内存泄漏时,使得占用过多***资源,影响其它程序的正常运行。为了高效的进行内存泄漏定位,现有技术中的定位方法一般通过监控应用程序的内存总使用量来判断是否出现内存泄漏,再进一步根据程序的运行逻辑进行内存泄漏的定位,这种内存泄漏的定位速度慢,而且不容易准确的内存泄漏的位置。为克服该问题,下面结合附图具体说明。
实施例一:
图 1 示出了本发明第一实施例提供的内存泄漏的定位方法的实现流程,详述如下:
在步骤 S101 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件。
具体的,本发明实施例中所述的内存申请以及内存释放,可以理解为应用程序在执行时所提出的内存申请。比如,应用程序在执行时,使用 malloc , realloc , new 等函数从堆中分配到一块内存,使用完后,程序必须负责相应的调用 free 或 delete 释放该内存块。在每个内存申请的程序代码中,包括需要申请的内存的大小的信息,或者还包括是否需要进一步增加内存空间的信息。
本发明实施例中所述的程序代码,即应用程序的程序语句。在应用程序运行过程中,即反复执行构成应用程序的语句的过程,当执行到应用程序中的内存申请语句,即为应用程序分配内存,在执行到内存释放语句时,则释放应用程序所占用的指定位置或者指定数据的内存空间。
在本发明实施例中所述的记录空间,是指应用程序在正常情况下执行到该代码位置,向***提出内存申请并获得***分配的内存时,本发明实施例中额外增加的一个内存段,根据内存分配时一般为连续地址的内存空间原则,所增加的这个内存段的内存地址与所述申请的内存的地址是连续的。
并且,为了方便同一个程序语句申请的内存的空间进行扩展,我们优选的实施方式为:将增加的内存段的地址设置在申请的内存的地址之前,从而可以在申请的内存之后继续后续申请内存的累加,使得申请的内存连续的存储。并且,将所述记录空间的地址设置在所述申请的内存的地址之前,当后续进行内存释放时,可以根据程序代码所占用的内存的起始位置查找到所述记录空间。
在本发明实施例中,实际分配的内存,包括两部分,即程序代码需要申请的内存和记录空间所占用的内存。记录空间的大小一般预先设定,比如根据记录空间需要记录的信息的类型,相应可以设定记录空间的内存大小。
如下为实际分配的内存的一种示意结构示意图:
文件名称 --- 行号 --- 内存大小 --- 申请的内存。
在该表格中,实际分配的内存包括两部分,分别为记录空间与申请的内存,其中记录空间包括文件名称以及程序行号。根据名称所占用的最大空间和程序行号占用的最大空间,可以确定记录空间所需要的内存的大小。
所述记录空间包括申请的内存信息及代码位置信息,其中,所述申请的内存信息可以为申请的内存的大小,比如申请 128 字节的内存,如果记录空间占有的内存的大小为 64 字节,那么***根据内存申请的请求,会返回一个连续的 192 字节的内存空间,在前 64 个字节中用作记录空间存储申请的内存信息以及代码位置信息,在后 128 个字节中用作程序代码中的数据存放。
所述代码的位置信息,对于多个程序文件构成的应用程序(在实际中一般的应用程序都是由多个程序文件构成),可以包括所述程序文件的名称,并且在同一个应用程序中,任意两个程序文件的名称是不相同的。另外,本发明实施例用于定位内存泄漏,所述代码的位置信息还包括应用程序的行号,在同一个应用程序中,每行程序代码对应的行号是唯一的。
因此,在记录空间中记录有程序文件的名称和程序的行号,即可准确的定位到应用程序代码的具***置使用的内存的大小。
可以理解的是,上述的代码位置信息通过程序文件的名称以及程序行号的表示方式只是其中一种较优的表示方式,本申请代码位置信息的表示方式不局限于上述一种。
在获取实际分配的内存后,本申请还包括记录空间记录的信息更新至跟踪文件,包括写入内存对应的程序文件名称、程序行号以及内存大小等信息。
所述跟踪文件,可以为应用程序执行时所生成的文本文件,并将其存储在非易失性存储器中,在程序重启或者断电后仍然可以查看文件中的内存分配对应的程序文件以及程序行号。
所述跟踪文件的生成,可以在应用程序执行每次的内存申请以及步骤 S102 中所述的内存释放进打印所述申请的内存地址之前的记录空间的信息,将记录空间的信息更新至所述跟踪文件。
在步骤 S102 中,在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件。
具体的,本发明实施例中所述的内存释放,可以理解为应用程序在执行时所提出的内存申请。比如,应用程序在执行时,使用 free 或 delete 释放该内存块。
在执行到内存释放语句,包括将分配的一段内存的一部分进行内存释放,以及对分配的一段内存的全部进行内存释放,对于这两种情况对于记录空间的更新操作也会有所区别,将在后面的实施例中具体进行介绍。
在执行内存释放时,接收到释放内存的起始地址,根据该起始地址以及预设的记录空间的大小,可以相应的确定记录空间起始地址和结束地址(在释放程序行号对应的全部的内存空间时,记录空间的结束地址与所述申请的地址的起始地址连续)。根据记录空间的地址,可以查看记录空间中记录的对应的申请的内存的信息以及申请内存的语句对应的行号。
在每执行一下内存释放后,相应的更新记录空间中的信息,在进行内存释放时,一般只需要更新记录空间中申请的内存信息即可(当然在全部释放时也会对记录空间进行删除),在记录空间的信息更新后,通过打印的方式或者其它方式,可以将记录究竟的信息更新至跟踪文件。
本发明实施例中一步骤 S101 与步骤 S102 执行时不需要严格按照先后执行,可以根据执行时需要的程序语句选择对应的执行步骤。比如遇到内存释放语句则执行步骤 S102 ,遇到内存申请语句则执行步骤 S101 。
在步骤 S103 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
由于本发明实施例中所述的跟踪文件中记录的信息,在每次的内存申请以及每次的内存释放时,并且在内存释放时可以查找到对应的内存申请语句对应行位置,从而可以快速有效的确定内存申请的具***置,提高定存泄漏的定位效率。
实施例二:
图 2 示出了本发明第二实施例提供的程序文件与跟踪文件内的存储位置对应关系建立方法的实现流程,其可应用于实施例一中的内存申请步骤或者内存释放步骤之前。
在本发明实施例中,所述程序代码由多个不同的程序文件构成,所述方法详述如下:
在步骤 S201 中,对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系。
具体的,在同一个应用程序调试运行或者测试过程中,可能会包括对应用程序中的程序文件修改多次,比如通过修改程序文件来实现应用程序漏洞的修补,或者增加新的功能等。为区分不同的程序文件对应的内存使用的情况,本申请还包括建立映射结果与跟踪文件的存储位置的对应关系。
其中,本发明实施例中所述的映射运算,可优先选用哈希算法完成。其中,所述哈希算法可以将任意长度的二进制值映射为较短的固定长度的二进制值,这个较短的二进制值称为哈希值。哈希值是一段数据唯一且极其紧凑的数值表示形式。如果散列一段明文而且哪怕只更改该段落的一个字母,随后的哈希都将产生不同的值。要找到散列为同一个值的两个不同的输入,在计算上是不可能的。
因此,通过对程序文件进行哈希运算,可以确定程序文件运行时的记录空间中的数据更新至跟踪文件的位置。
当然,作为本发明进一步的实施方式,所述映射运算还可以包括对程序文件以及程序的行号进行映射运算,当程序行号用于内存申请时,使得同一程序文件的同一行程序的内存操作信息自动更新至同一位置。
在步骤 S202 中,当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
由于程序文件修改后的映射运算的结果通常也会相应的改变,因此,根据预设的映射结果值与跟踪文件中的存储位置的对应关系,对于同一个程序文件修改后,其在跟踪文件中的存储位置也必然会相应的改变,从而可以方便的在同一个跟踪文件中比较不同版本的程序文件中的程序语句使用的内存,提高程序优化的便利性。
实施例三:
图 3 示出了本发明第三实施例提供的 的实现流程,详述如下:
在步骤 S301 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存。
在步骤 S302 中,判断当前程序文件的程序行号是否已经申请内存。
具体的,本发明实施例在程序运行时,运行到内存申请的程序行号时,需要判断该程序行号是否为第一次申请内存。
判断是否为第一次申请行号的目的在于,根据判断结果确定是否需要分配记录空间对应的内存段。
在步骤 S303 中,如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续。
当程序文件的程序行号第一次执行内存申请时,则需要分配对应的记录空间的内存段,并且在记录空间记录内存申请的相关信息,包括程序文件名称、需要申请内存的程序行号以及申请内存的大小。
在步骤 S304 中,在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
通过在内存中分配记录空间,并通过记录空间记录程序文件名称、需要申请内存的程序行号以及申请内存的大小的目的在于,当在内存释放时,根据预先的设定,在获取需要释放的内存段的地址时,根据该地址相应的查找到记录空间,从而可以查找到程序文件以及申请内存的程序行号,并根据查找到的内存申请的位置,相应的修改跟踪文件中的信息。
在步骤 S305 中,在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件。
在步骤 306 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
本发明实施例对新内存申请时设置记录空间的方法进行了介绍,通过设置记录空间,可使得内存释放时查找到对应的内存申请所在程序文件名称及程序行号,能够更为有效的对每个内存申请的程序行号中的内存进行管理监控。
实施例四:
图 4 示出了本发明第四实施例提供的内存泄漏的定位方法的实现流程,详述如下:
在步骤 S401 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存。
在步骤 S402 中,判断当前程序文件的程序行号是否已经申请内存。
在步骤 S403 中,如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
在本发明实施例中,对于已经至少申请过一次内存的程序行号,并且在后续对增加的内存的分配过程中,新增加的内存一般的在之前申请的内存后,且之前申请的内存与新增加的内存地址连续。即当第一次内存申请时,即预留有可扩展的内存空间供后续增加的内存使用。
由于之前已经分配有记录空间,因此在同一行号再次进行内存申请时,只需要将新申请的内存值累加到所述记录空间中申请的内存信息中。
比如,第一次程序需要申请的内存的大小为 128 字节,第二次内存申请增加 64 字节,那么在记录空间中对应的内存大小的位置处,修改其大小为 128+64=192 即可。
在步骤 S404 中,在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件。
在步骤 S405 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
本发明实施例与实施例三为程序代码执行时两种不同情况的说明,对于已申请有记录空间的程序代码再次进行内存申请时,只需要更新记录空间中的内存信息的数据,并将更新的数据相应的更新至跟踪文件。
实施例五:
图 5 为本发明第五实施例提供的内存泄漏的定位方法的实现流程,详述如下:
在步骤 S501 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件。
在步骤 S502 中,判断释放的内存是否为记录空间中的申请的内存信息中的全部内存。
具体的,对于内存释放,一般包括两种情况,一种是释放同一程序行号申请的内存中的全部内存,另一种是当同一程序行号多次申请过内存时,释放部分内存。
判断是否为释放全部内存或者部分内存的方法,可以根据释放的内存的头部是否有记录空间信息进行判断。当释放内存的头部有记录空间的信息,则为释放该程序行号对应的全部内存,否则为释放部分内存。
在步骤 S503 中,如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息。
当执行释放全部内存时,对于所述记录空间,可以对所述记录空间的数据删除,或者直接释放所述记录空间对应的内存。
与此对应的,在跟踪文件中可以删除所述程序行号对应的数据信息,或者将程序行号对应的数据信息中的内存大小修改为 0 。
在步骤 S504 中,如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
如果对内存进行部分释放时,则优选释放分配的内存后面部分,作为本发明一种查找对应的申请内存的程序行号的方法,可以根据释放的内存向前查找的方式,获取记录空间的数据,并相应的更新记录空间的数据值。
在步骤 S505 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
本发明实施例根据内存释放可能的情况,具体介绍说明了相应的查找更新记录空间的数据方式,针对对于内存部分释放以及内存全部释放的方式,快速有效的进行记录空间的查找与更新。
实施例四:
图 4 示出了本发明第四实施例提供的内存泄漏的定位方法的实现流程,详述如下:
在步骤 S401 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存。
在步骤 S402 中,判断当前程序文件的程序行号是否已经申请内存。
在步骤 S403 中,如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
在本发明实施例中,对于已经至少申请过一次内存的程序行号,并且在后续对增加的内存的分配过程中,新增加的内存一般的在之前申请的内存后,且之前申请的内存与新增加的内存地址连续。即当第一次内存申请时,即预留有可扩展的内存空间供后续增加的内存使用。
由于之前已经分配有记录空间,因此在同一行号再次进行内存申请时,只需要将新申请的内存值累加到所述记录空间中申请的内存信息中。
比如,第一次程序需要申请的内存的大小为 128 字节,第二次内存申请增加 64 字节,那么在记录空间中对应的内存大小的位置处,修改其大小为 128+64=192 即可。
在步骤 S404 中,在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件。
在步骤 S405 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
本发明实施例与实施例三为程序代码执行时两种不同情况的说明,对于已申请有记录空间的程序代码再次进行内存申请时,只需要更新记录空间中的内存信息的数据,并将更新的数据相应的更新至跟踪文件。
实施例五:
图 5 为本发明第五实施例提供的内存泄漏的定位方法的实现流程,详述如下:
在步骤 S501 中,在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件。
在步骤 S502 中,判断释放的内存是否为记录空间中的申请的内存信息中的全部内存。
具体的,对于内存释放,一般包括两种情况,一种是释放同一程序行号申请的内存中的全部内存,另一种是当同一程序行号多次申请过内存时,释放部分内存。
判断是否为释放全部内存或者部分内存的方法,可以根据释放的内存的头部是否有记录空间信息进行判断。当释放内存的头部有记录空间的信息,则为释放该程序行号对应的全部内存,否则为释放部分内存。
在步骤 S503 中,如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息。
当执行释放全部内存时,对于所述记录空间,可以对所述记录空间的数据删除,或者直接释放所述记录空间对应的内存。
与此对应的,在跟踪文件中可以删除所述程序行号对应的数据信息,或者将程序行号对应的数据信息中的内存大小修改为 0 。
在步骤 S504 中,如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
如果对内存进行部分释放时,则优选释放分配的内存后面部分,作为本发明一种查找对应的申请内存的程序行号的方法,可以根据释放的内存向前查找的方式,获取记录空间的数据,并相应的更新记录空间的数据值。
在步骤 S505 中,根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
本发明实施例根据内存释放可能的情况,具体介绍说明了相应的查找更新记录空间的数据方式,针对对于内存部分释放以及内存全部释放的方式,快速有效的进行记录空间的查找与更新。
实施例六:
图 6 为本发明实施例提供的内存泄漏的定位装置的结构示意图,详述如下:
本发明实施例所述内存泄漏的定位装置,包括:
内存申请单元 601 ,用于在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件;
内存释放单元 602 ,用于在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件;
定位单元 603 ,用于根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
优选的,所述装置还包括:
位置映射单元,用于对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系;
位置映射更新单元,用于当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
优选的,所述内存申请单元包括:
内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
内存段获取子单元,用于如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续;
程序信息记录子单元,用于在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
优选的,所述内存申请单元包括:
内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
内存值累加子单元,用于如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
优选的,所述内存释放单元包括:
内存释放判断子单元,用于判断释放的内存是否为记录空间中的申请的内存信息中的全部内存;
记录空间释放子单元,用于如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息;
记录空间更新子单元,用于如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
本发明实施例所述内存泄漏的定位装置,与实施例一至实施例五所述内存泄漏的定位方法对应,在此不作重复赘述。
在本发明所提供的几个实施例中,应该理解到,所揭露的装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个***,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。
另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例所述方法的全部或部分。而前述的存储介质包括: U 盘、移动硬盘、只读存储器( ROM , Read-Only Memory )、随机存取存储器( RAM , Random Access Memory )、磁碟或者光盘等各种可以存储程序代码的介质。
以上所述仅为本发明的较佳实施例而已,并不用以限制本发明,凡在本发明的精神和原则之内所作的任何修改、等同替换和改进等,均应包含在本发明的保护范围之内。

Claims (10)

  1. 一种内存泄漏的定位方法,其特征在于,所述方法包括:
    在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件;
    在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件;
    根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
  2. 根据权利要求 1 所述方法,其特征在于,所述程序代码由多个不同的程序文件构成,在所述内存申请步骤之前,所述方法还包括:
    对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系;
    当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
  3. 根据权利要求 1 所述方法,其特征在于,所述在所述记录空间中记录申请的内存信息及代码位置信息步骤包括:
    判断当前程序文件的程序行号是否已经申请内存;
    如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续;
    在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
  4. 根据权利要求 1 所述方法,其特征在于,所述在所述记录空间中记录申请的内存信息及代码位置信息步骤包括:
    判断当前程序文件的程序行号是否已经申请内存;
    如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
  5. 根据权利要求 1 所述方法,其特征在于,所述根据释放的内存更新所述记录空间中的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件步骤包括:
    判断释放的内存是否为记录空间中的申请的内存信息中的全部内存;
    如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息;
    如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
  6. 一种内存泄漏的定位装置,其特征在于,所述装置包括:
    内存申请单元,用于在每次内存申请时,根据程序代码需要申请的内存,以及预设的与所述需要申请的内存的地址连续的记录空间,构成实际分配的内存,在所述记录空间中记录申请的内存信息及代码位置信息,并将所述申请的内存信息及代码位置信息写入跟踪文件;
    内存释放单元,用于在每次内存释放时,根据释放的内存查找并更新对应的所述记录空间中申请的内存信息,并将所述记录空间中的申请的内存信息更新至所述跟踪文件;
    定位单元,用于根据所述跟踪文件记录的申请的内存信息以及代码位置信息进行内存泄漏定位。
  7. 根据权利要求 6 所述装置,其特征在于,所述装置还包括:
    位置映射单元,用于对程序文件进行映射运算,建立映射运算的映射结果与跟踪文件内的存储位置的对应关系;
    位置映射更新单元,用于当程序文件修改后,根据所述映射运算,更新所述映射运算的映射结果与所述跟踪文件的存储位置的对应关系。
  8. 根据权利要求 6 所述装置,其特征在于,所述内存申请单元包括:
    内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
    内存段获取子单元,用于如果当前程序文件的程序行号没有申请内存,在所述程序代码需要申请的内存地址之前,获取预设长度的内存段,所述内存段的地址与所述程序代码需要申请的内存的地址连续;
    程序信息记录子单元,用于在所述预设长度的内存段中的预定位置记录需要申请内存的程序文件名称、需要申请内存的程序行号以及申请内存的大小。
  9. 根据权利要求 6 所述装置,其特征在于,所述内存申请单元包括:
    内存申请判断子单元,用于判断当前程序文件的程序行号是否已经申请内存;
    内存值累加子单元,用于如果当前程序文件的程序行号已经申请内存,则将新申请的内存值累加到所述记录空间中申请的内存信息中。
  10. 根据权利要求 6 所述装置,其特征在于,所述内存释放单元包括:
    内存释放判断子单元,用于判断释放的内存是否为记录空间中的申请的内存信息中的全部内存;
    记录空间释放子单元,用于如果释放的内存为记录空间中的申请的内存信息中的全部内存,则删除所述记录空间的数据并释放所述记录空间对应的内存,相应的更新所述跟踪文件中申请的内存信息为 0 或者删除所述程序行号在所述跟踪文件中的记录信息;
    记录空间更新子单元,用于如果释放的内存不是记录空间中的申请的内存信息中的全部内存,则将所述记录空间中申请的内存信息与释放的内存值相减,并将相减的结果更新至所述跟踪文件。
PCT/CN2015/088764 2015-09-01 2015-09-01 一种内存泄漏的定位方法和装置 WO2017035785A1 (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
PCT/CN2015/088764 WO2017035785A1 (zh) 2015-09-01 2015-09-01 一种内存泄漏的定位方法和装置

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2015/088764 WO2017035785A1 (zh) 2015-09-01 2015-09-01 一种内存泄漏的定位方法和装置

Publications (1)

Publication Number Publication Date
WO2017035785A1 true WO2017035785A1 (zh) 2017-03-09

Family

ID=58186545

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2015/088764 WO2017035785A1 (zh) 2015-09-01 2015-09-01 一种内存泄漏的定位方法和装置

Country Status (1)

Country Link
WO (1) WO2017035785A1 (zh)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110618928A (zh) * 2019-06-25 2019-12-27 北京无限光场科技有限公司 一种内存泄露检测方法、装置、终端设备及介质
CN110908865A (zh) * 2019-11-15 2020-03-24 珠海豹趣科技有限公司 内存泄漏监控方法、装置及电子设备
CN113157477A (zh) * 2021-04-14 2021-07-23 北京字节跳动网络技术有限公司 内存泄漏的归因方法、装置、电子设备和存储介质
CN113448720A (zh) * 2020-03-27 2021-09-28 腾讯科技(深圳)有限公司 一种内存分配方法、装置、设备及存储介质
CN113535380A (zh) * 2021-05-31 2021-10-22 宁波三星医疗电气股份有限公司 一种基于智能用电终端的内存分配方法

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050235127A1 (en) * 2004-04-19 2005-10-20 Cisco Technology, Inc. Method and system for memory leak detection
CN101110044A (zh) * 2007-08-28 2008-01-23 中兴通讯股份有限公司 一种内存监控管理的方法及***
CN101539870A (zh) * 2008-03-21 2009-09-23 中兴通讯股份有限公司 内存泄漏检测装置及方法
CN101763305A (zh) * 2009-12-29 2010-06-30 青岛海信宽带多媒体技术有限公司 一种嵌入式***内存泄露的检测方法

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050235127A1 (en) * 2004-04-19 2005-10-20 Cisco Technology, Inc. Method and system for memory leak detection
CN101110044A (zh) * 2007-08-28 2008-01-23 中兴通讯股份有限公司 一种内存监控管理的方法及***
CN101539870A (zh) * 2008-03-21 2009-09-23 中兴通讯股份有限公司 内存泄漏检测装置及方法
CN101763305A (zh) * 2009-12-29 2010-06-30 青岛海信宽带多媒体技术有限公司 一种嵌入式***内存泄露的检测方法

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110618928A (zh) * 2019-06-25 2019-12-27 北京无限光场科技有限公司 一种内存泄露检测方法、装置、终端设备及介质
CN110908865A (zh) * 2019-11-15 2020-03-24 珠海豹趣科技有限公司 内存泄漏监控方法、装置及电子设备
CN113448720A (zh) * 2020-03-27 2021-09-28 腾讯科技(深圳)有限公司 一种内存分配方法、装置、设备及存储介质
CN113157477A (zh) * 2021-04-14 2021-07-23 北京字节跳动网络技术有限公司 内存泄漏的归因方法、装置、电子设备和存储介质
CN113157477B (zh) * 2021-04-14 2024-02-06 北京字节跳动网络技术有限公司 内存泄漏的归因方法、装置、电子设备和存储介质
CN113535380A (zh) * 2021-05-31 2021-10-22 宁波三星医疗电气股份有限公司 一种基于智能用电终端的内存分配方法

Similar Documents

Publication Publication Date Title
WO2017035785A1 (zh) 一种内存泄漏的定位方法和装置
WO2014208863A1 (ko) 비휘발성 버퍼 캐시와 비휘발성 스토리지를 위한 통합 데이터 관리 장치 및 방법
WO2014032262A1 (zh) 一种平台间迁移应用的实现方法及***
WO2018101640A1 (ko) 데이터베이스 이중화 무중단 정합성 복구 기법
WO2018076868A1 (zh) 一种数据同步方法、装置、***、存储介质和服务器
WO2015005634A1 (ko) 메모리 시스템 및 이의 제어 방법
WO2018076889A1 (zh) 数据备份的方法、装置、***、存储介质及服务器
WO2017082520A1 (ko) 데이터베이스 관리 시스템 및 그의 데이터 변경 및 복구 방법
WO2013017037A1 (zh) 一种soc芯片的验证方法及***
WO2015102220A1 (ko) 보안저장장치를 구비하는 저장 시스템 및 그 관리 방법
WO2018233369A1 (zh) 基于写时复制的虚拟磁盘写入方法及装置、存储介质
WO2018090823A1 (zh) 一种***分区关键数据的保护方法及***、终端
WO2018076867A1 (zh) 数据备份的删除方法、装置、***、存储介质和服务器
WO2014044136A1 (zh) 基于分布式数据的并发处理方法、***和计算机存储介质
WO2021012481A1 (zh) ***性能监控方法、装置、设备及存储介质
WO2015002481A1 (ko) 플래시 메모리 기반 세가지 상태를 가지는 버퍼 관리 장치 및 방법
WO2016056856A1 (ko) 무결성 검증 데이터 생성 방법 및 시스템
WO2017206885A1 (zh) 应用程序的关闭方法、装置、存储介质及电子设备
WO2019205272A1 (zh) 虚拟机服务提供方法、装置、设备及计算机可读存储介质
WO2017118170A1 (zh) 一种移动终端的桌面启动器的定制方法及***
WO2021080385A1 (en) Computing device and operating method thereof
WO2018076869A1 (zh) 一种数据备份方法、装置、存储介质和电子设备
WO2015180434A1 (zh) 一种数据库集群管理数据的方法、节点及***
WO2018076870A1 (zh) 数据处理方法、装置、存储介质、服务器及数据处理***
WO2017073957A1 (ko) 전자 장치 및 이의 메모리 관리 방법

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: 15902592

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: 15902592

Country of ref document: EP

Kind code of ref document: A1