Method, device and equipment for detecting graphic memory leakage, storage medium and program product
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- MOORE THREADS TECH CO LTD
- Filing Date
- 2026-04-24
- Publication Date
- 2026-06-26
Smart Images

Figure CN122285407A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image processing technology, and in particular to a method, apparatus, device, storage medium, and program product for detecting video memory leakage. Background Technology
[0002] Video memory leaks refer to the phenomenon where software requests and allocates video random access memory (VRAM) from the graphics processing unit (GPU) during runtime, but fails to release or return it according to specifications after use. Video memory leaks cause this portion of memory to remain continuously occupied and unusable, leading to a continuous decrease in available video memory and eventually exhaustion of video memory resources.
[0003] In related technologies, abnormal changes in video memory are mainly used to determine whether a video memory leak exists, but this cannot accurately pinpoint the code location where the leak occurs. However, in complex scenarios such as multi-process environments with high video memory usage, simply determining whether a video memory leak exists still requires a significant amount of time to manually locate the leak, which cannot effectively improve the speed of handling video memory leaks. Summary of the Invention
[0004] Therefore, it is necessary to provide a method, apparatus, equipment, storage medium, and program product for detecting video memory leaks that can improve the processing speed of video memory leaks, in order to address the above-mentioned technical problems.
[0005] Firstly, this application provides a method for detecting video memory leaks, including:
[0006] In response to the target process's video memory allocation request, a target video memory block is allocated to the target process;
[0007] Initialize the reference count of the target video memory block allocated to the target process;
[0008] During the execution of the target process, the reference count of the target memory block is monitored;
[0009] When the reference count of the target memory block is updated, the current call stack information of the target memory block is determined, wherein the reference count is updated when the target memory block is referenced or released by the target process;
[0010] If the target memory block has a memory leak, obtain the change record of the call stack information of the target memory block at each reference count update, and determine the location of the memory leak based on the change record.
[0011] In one embodiment, before allocating a target memory block to the target process in response to a memory allocation request from the target process, the method further includes:
[0012] Create a global hash table;
[0013] In the global hash table, a record entry is assigned to each target memory block of the image processor, and the record entry is used to record the information of the corresponding target memory block;
[0014] Based on the record entries of each target memory block in the global hash table, the reference summary information of each target memory block is saved in the memory reference summary file.
[0015] The reference summary information includes the address of the target video memory block, the identifier of the target process, the size of the target video memory block, and the reference count of the target video memory block.
[0016] In one embodiment, monitoring the reference count of the target memory block during the execution of the target process includes:
[0017] Receive monitoring instruction information, the monitoring instruction information being used to indicate at least one target memory block to be monitored;
[0018] Obtain the address of the at least one target memory block to be monitored from the memory reference summary file;
[0019] The address of at least one target memory block to be monitored is written into the memory reference filter file by a target command; wherein the target command is used to instruct the monitoring of a specified target memory block;
[0020] According to the memory reference filtering file, the reference count of the target memory block is monitored during the execution of the target process.
[0021] In one embodiment, the method further includes:
[0022] Based on the address of the at least one target memory block to be monitored, record the call stack information of the at least one target memory block to be monitored when the reference count is updated in the memory reference record file.
[0023] In one embodiment, obtaining the change record of the call stack information of the target memory block during each reference count update includes:
[0024] The change record is obtained from the video memory reference record file;
[0025] Determining the memory leak location based on the change record includes:
[0026] Based on the change record, determine the code path that did not release the target memory block; based on the code path that did not release the target memory block, determine the memory leak location of the target memory block.
[0027] In one embodiment, after allocating a target memory block to the target process in response to the target process's memory allocation request, the method further includes:
[0028] In the record entry corresponding to the target memory block allocated to the target process in the global hash table, the allocation call stack information and the first reference call stack information corresponding to the target memory block allocated to the target process are recorded.
[0029] In one embodiment, the method further includes:
[0030] When the target process terminates, obtain the final reference count of the target memory block in the memory reference summary file;
[0031] If the final reference count is not zero, it is determined that the target memory block has a memory leak; or, if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed, it is determined that the target memory block has a memory leak.
[0032] If the final reference count is zero, it is determined that the target memory block does not have a memory leak; the record information of the target memory block is deleted from the global hash table.
[0033] Secondly, this application also provides a device for detecting memory leakage, comprising:
[0034] The video memory allocation module is used to initialize the reference count of the target video memory block allocated to the target process in response to the video memory allocation request of the target process.
[0035] The reference count monitoring module is used to monitor the reference count of the target memory block during the execution of the target process.
[0036] The information recording module is used to determine the current call stack information of the target memory block when the reference count of the target memory block is updated, wherein the reference count is updated when the target memory block is referenced or released by the target process.
[0037] The leak detection module is used to obtain the change record of the call stack information of the target memory block during each reference count update when the target memory block has a memory leak, and to determine the location of the memory leak based on the change record.
[0038] In one embodiment, the information recording module is specifically used to create a global hash table; allocate a record entry for each target memory block of the image processor in the global hash table, the record entry being used to record the information of the corresponding target memory block; and save the reference summary information of each target memory block in a memory reference summary file according to the record entries of each target memory block in the global hash table; wherein, the reference summary information includes the address of the target memory block, the identifier of the target process, the size of the target memory block, and the reference count of the target memory block.
[0039] In one embodiment, the reference count monitoring module is further configured to receive monitoring instruction information, the monitoring instruction information being used to indicate at least one target memory block to be monitored; obtain the address of the at least one target memory block to be monitored from the memory reference summary file; write the address of the at least one target memory block to be monitored into the memory reference filter file via a target command; wherein the target command is used to indicate monitoring of a specified target memory block; and monitor the reference count of the target memory block during the execution of the target process according to the memory reference filter file.
[0040] In one embodiment, the information recording module is further configured to record, in a memory reference record file, the call stack information of the at least one target memory block to be monitored when the reference count is updated, based on the address of the at least one target memory block to be monitored.
[0041] In one embodiment, the leak detection module is specifically configured to obtain the change record from the video memory reference record file; determine the code path of the target video memory block that has not been released based on the change record; and determine the video memory leak location of the target video memory block based on the code path of the target video memory block that has not been released.
[0042] In one embodiment, the information recording module is further configured to record, in the record entry corresponding to the target memory block allocated to the target process in the global hash table, the allocation call stack information and the first reference call stack information corresponding to the target memory block allocated to the target process.
[0043] In one embodiment, the leak detection module is further configured to: obtain the final reference count of the target memory block in the memory reference summary file when the target process terminates; determine that the target memory block has a memory leak if the final reference count is not zero; or determine that the target memory block has a memory leak if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed; determine that the target memory block does not have a memory leak if the final reference count is zero; and delete the record information of the target memory block from the global hash table.
[0044] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the memory leakage detection method of the first aspect described above.
[0045] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the memory leakage detection method of the first aspect described above.
[0046] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the method for detecting video memory leakage as described in the first aspect.
[0047] The aforementioned method, apparatus, device, storage medium, and program product for detecting video memory leaks respond to a video memory allocation request from a target process by allocating a target video memory block for the target process; initializing the reference count of the target video memory block allocated to the target process; monitoring the reference count of the target video memory block during the execution of the target process; determining the current call stack information of the target video memory block when the reference count of the target video memory block is updated, wherein the reference count is updated when the target video memory block is referenced or released by the target process; and, in the case of a video memory leak, obtaining a record of changes in the call stack information of the target video memory block during each reference count update, and determining the location of the video memory leak based on the change record. Because the current call stack information of the target video memory block is determined in real time when the reference count of the target video memory block is updated, the location of the video memory leak can be determined by the record of changes in the call stack information of the target video memory block during each reference count update, eliminating the need for manual location of the video memory leak, thereby effectively improving the processing speed of video memory leaks. Attached Figure Description
[0048] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0049] Figure 1 A flowchart illustrating a method for detecting video memory leaks provided in an embodiment of this application;
[0050] Figure 2 A flowchart illustrating another method for detecting video memory leaks provided in this application embodiment;
[0051] Figure 3 A structural block diagram of a memory leakage detection device provided in an embodiment of this application;
[0052] Figure 4 This is an internal structural diagram of a computer device provided in an embodiment of this application. Detailed Implementation
[0053] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0054] To address the aforementioned technical problems, embodiments of this application provide a method, apparatus, device, storage medium, and program product for detecting video memory leaks. This method determines the current call stack information of the target video memory block in real time when the reference count is updated. Therefore, if a video memory leak exists, the location of the leak can be determined by recording the changes in the call stack information of the target video memory block during each reference count update, eliminating the need for manual location of the leak and effectively improving the processing speed of video memory leaks.
[0055] The memory leak detection method provided in this application can be applied to any memory leak detection device in scenarios with high memory dependency, such as graphics processing unit (GPU) drivers, deep learning frameworks, and graphics rendering engines. This memory leak detection device can be a GPU or any terminal that includes a GPU.
[0056] The terminals can be, but are not limited to, various personal computers, laptops, smartphones, tablets, IoT devices, and portable wearable devices. IoT devices can include smart speakers, smart TVs, smart air conditioners, smart in-vehicle systems, and projection devices. Portable wearable devices can include smartwatches, smart bracelets, and head-mounted displays. Head-mounted displays can include virtual reality (VR) devices, augmented reality (AR) devices, and smart glasses.
[0057] In one exemplary embodiment, such as Figure 1 As shown, a method for detecting video memory leakage is provided. Taking the application of this method to the aforementioned video memory leakage detection device as an example, the method for detecting video memory leakage includes steps S101 to S105. Wherein:
[0058] S101. In response to the target process's video memory allocation request, allocate a target video memory block for the target process.
[0059] In some embodiments, when a target process in an application needs to reference video memory, it can send a video memory allocation request to a video memory leak detection device. Accordingly, the video memory leak detection device responds to the video memory allocation request of the target process and allocates a target video memory block for the target process based on a preset video memory block allocation rule.
[0060] The target process can be any process within an application that calls GPU resources. The target memory block can be any region of GPU memory allocated to the target process.
[0061] In some embodiments, before allocating a target memory block to the target process in response to a memory allocation request from the target process, the memory leak detection device may also create a global hash table. Subsequently, the memory leak detection device may allocate record entries for each target memory block of the image processor in the global hash table, and save the reference summary information of each target memory block in a memory reference summary file based on the record entries of each target memory block in the global hash table.
[0062] The global hash table is a top-level hash table structure that manages information about all target memory blocks using arrays and linked lists. The global hash table contains multiple record entries.
[0063] It should be understood that the embodiments of this application do not limit how the global hash table is created. In some embodiments, during GPU code initialization, the memory leak detection device can create a global hash table, thereby recording the information of each target cache block through multiple record entries in the global hash table.
[0064] GPU code initialization can include driver loading and module initialization.
[0065] For example, when creating a global hash table, the HashInsert function can be called to generate a record entry for each target memory block of the GPU in the global hash table, thereby recording the information of the corresponding target memory block through the record entry. Different target memory blocks use a separate record entry in the global hash table.
[0066] For example, as the target memory blocks are referenced and released, the information recorded in the entries of each target memory block in the global hash table is dynamically updated. At the same time, the memory leak detection device can extract the reference summary information from the record entries of each target memory block in real time and save the extracted reference summary information to the memory reference summary file.
[0067] The aforementioned video memory reference summary file (vram_ref_dump_summary) is used to store the reference summary information of the target video memory block in the global hash table. This reference summary information may include brief record information, that is, some key record information, thereby avoiding the storage of too much information in the video memory reference summary file when the system complexity is too high.
[0068] For example, the reference summary information may include the address of the target video memory block, the identifier of the target process, the size of the target video memory block, and the reference count of the target video memory block.
[0069] The target memory block address is used to represent the location of GPU memory in physical or virtual memory space; the target process identifier is used to represent the number of the target process that references the target memory block; the target memory block size is used to represent the memory usage capacity of the target memory block; and the target memory block reference count is used to represent the number of times the target memory block is referenced.
[0070] In other embodiments, the target memory blocks for which the reference summary information is recorded in the aforementioned memory reference summary file can be target memory blocks with non-zero reference counts. That is, the memory reference summary file summarizes the reference summary information of target memory blocks with non-zero reference counts, allowing users to identify target memory blocks of interest by querying the file. Correspondingly, when the memory reference summary file is not empty, there are currently target memory blocks with non-zero reference counts that have not been released; in this case, the memory reference summary file summarizes the target memory blocks with non-zero reference counts. When the memory reference summary file is empty, there are currently no target memory blocks with non-zero reference counts, and all target memory blocks have been released.
[0071] In this embodiment of the application, by storing the summary information of the target memory blocks in the global hash table in the memory reference summary file, the memory usage of the target memory blocks corresponding to each target process can be displayed in a timely and convenient manner through the memory reference summary file, which facilitates the timely detection and handling of memory leak problems.
[0072] S102. Initialize the reference count of the target video memory block allocated to the target process.
[0073] In this step, after allocating a target memory block to the target process in response to the target process's memory allocation request, the memory leak detection device can initialize the reference count of the target memory block allocated to the target process.
[0074] The reference count of the target memory block is used to represent the number of times the target memory block is referenced. Accordingly, the reference count of the target memory block allocated to the target process is initialized, that is, the reference count of the target memory block allocated to the target process is set to 0.
[0075] In some embodiments, after allocating a target memory block to the target process in response to the target process's memory allocation request, the memory leak detection device can also record the allocation call stack information and the first reference call stack information corresponding to the target memory block allocated to the target process in the record entry corresponding to the target memory block allocated to the target process in the global hash table.
[0076] The allocation call stack information refers to the call stack information when the target video memory block is allocated to the target process, and the first reference call stack information refers to the call stack information when the target process first references the target memory block. The call stack information contains information about the target process during the process of referencing or releasing the target video memory block, including the called function, the call order, the file where it is located, and the line number.
[0077] S103. During the execution of the target process, monitor the reference count of the target memory block.
[0078] In this step, after the memory leak detection device initializes the reference count of the target memory block allocated to the target process, it can monitor the reference count of the target memory block during the execution of the target process.
[0079] In some embodiments, the present application may monitor a portion of the target memory blocks or monitor all target memory blocks; the present application does not impose any restrictions on this.
[0080] In some embodiments, when monitoring all target memory blocks, the memory leak detection device can directly monitor the reference count of all target memory blocks of the GPU.
[0081] In some embodiments, when monitoring a portion of the target video memory blocks, the video memory leak detection device can receive monitoring instruction information and obtain the address of at least one target video memory block to be monitored from a video memory reference summary file. Subsequently, the video memory leak detection device monitors the reference count of the target video memory blocks to be monitored during the execution of the target process.
[0082] For example, in response to a user's query request, the memory leak detection device can display the reference summary information of each target memory block in the memory reference summary file on the terminal. Subsequently, based on the reference summary information of each target memory block in the displayed memory reference summary file, the user sends monitoring instruction information to the memory leak detection device, thereby indicating at least one target memory block to be monitored.
[0083] For example, after receiving monitoring instruction information, the memory leak detection device can obtain the address of at least one target memory block to be monitored from the memory reference summary file, and write the address of the target memory block to be monitored into the memory reference filter file through the target command. Subsequently, the memory leak detection device monitors the reference count of the target memory block during the execution of the target process based on the memory reference filter file containing the addresses of the target memory blocks to be monitored.
[0084] The target command is used to instruct the monitoring of a specified target memory block.
[0085] For example, a memory leak detection device can use a target command to control a memory reference filter file to filter out target memory blocks to be monitored. The target command can be an output command (e.g., an echo command). The memory reference filter file (vram_ref_dump_filte) is used to query the address of the target memory block specified by the target command, thereby filtering out the memory usage of the target memory blocks that require special attention.
[0086] For example, the memory leak detection device writes the address 0x12345678 of the target memory block to be monitored into a memory reference filter file via the echo command. This allows the memory reference filter file to filter out the target memory blocks that need to be monitored through address 0x12345678, so that the memory leak detection device can monitor the usage of the filtered target memory blocks according to the memory reference filter file.
[0087] It should be noted that the address of the target memory block to be monitored written in the above target command can be one or more, and this application embodiment does not impose any restrictions on this. When only one address of the target memory block to be monitored is written, the memory leakage detection device only needs to monitor one suspicious target memory block. When multiple addresses of the target memory blocks to be monitored are written, the memory leakage detection device needs to monitor multiple suspicious target memory blocks simultaneously.
[0088] In this embodiment of the application, the address of the target memory block to be monitored is written into the memory reference filter file by the target command, thereby filtering out the target memory blocks that need to be monitored, avoiding monitoring all target memory blocks and improving monitoring efficiency.
[0089] It should be understood that the embodiments of this application do not limit how the reference count of the target memory block is changed. In some embodiments, the reference count is updated when the target memory block is referenced or released by the target process.
[0090] For example, when the target memory block is referenced (Ref) by the target process, the reference count of the target memory block in the record information of the target memory block in the global hash table is increased (e.g., incremented by one). For example, when the target memory block is unrefractive (Unref) by the target process, the reference count of the target memory block in the record information of the target memory block in the global hash table is decreased (e.g., decremented by one).
[0091] S104. When the reference count of the target memory block is updated, determine the current call stack information of the target memory block.
[0092] In this step, after the memory leak detection device monitors the reference count of the target memory block during the execution of the target process, it can determine the current call stack information of the target memory block when it detects an update to the reference count of the target memory block.
[0093] The call stack information is information about the target process during the process of referencing or releasing the target video memory block, including the called function, the call order, the file where it is located, the line number, and other information.
[0094] For example, when a memory leak detection device detects an increase in the reference count of a target memory block, it can obtain the current call stack information of the target memory block in real time. Similarly, when a memory leak detection device detects a decrease in the reference count of a target memory block, it can also obtain the current call stack information of the target memory block in real time.
[0095] In some embodiments, the memory leak detection device may also record the call stack information of at least one target memory block being monitored when the reference count is updated in the memory reference record file, based on the address of at least one target memory block to be monitored.
[0096] The video memory reference log file (vram_ref_dump_stack) is used to record in detail the call stack information related to the target video memory to be monitored, as indicated by the video memory reference filter file.
[0097] For example, when the target memory block is referenced (Ref) by the target process, the reference count of the target memory block in the global hash table is incremented by one. At this time, the memory leak detection device can traverse the records in the hash table and save the current call stack information of the target memory block to the memory reference record file. When the target memory block is released (Unref) by the target process, the reference count of the target memory block in the global hash table is decremented by one. At this time, the memory leak detection device can also traverse the records in the hash table and save the current call stack information of the target memory block to the memory reference record file.
[0098] For example, the memory reference record file can use the address of the target memory block to be monitored to record the call stack information of different target memory blocks. Accordingly, when recording the call stack information, the memory leak detection device can determine the recording position of the call stack information of the corresponding target memory block in the memory reference record file by using the address of the target memory block to be monitored.
[0099] In some embodiments, this application does not limit how the memory reference summary information, memory reference filter file, and memory reference log file are created. For example, when the GPU driver is loaded, the memory tracing parameter (vram_trace) can be enabled. At this time, after the memory leak detection device runs the application, it can automatically generate the memory reference summary information, memory reference filter file, and memory reference log file in the background. This application can dynamically enable and disable the memory reference summary information, memory reference filter file, and memory reference log file through the memory tracing parameter.
[0100] S105. If there is a memory leak in the target memory block, obtain the change record of the call stack information of the target memory block at each reference count update, and determine the location of the memory leak based on the change record.
[0101] In this step, after determining the current call stack information of the target memory block, the memory leak detection device can obtain the change record of the call stack information of the target memory block at each reference count update if there is a memory leak in the target memory block, and determine the location of the memory leak based on the change record.
[0102] The following section explains how to determine memory leaks.
[0103] In some embodiments, the memory leak detection device can obtain the final reference count of the target memory block from the memory reference summary file when the target process terminates. Subsequently, if the final reference count is not zero, it is determined that the target memory block has a memory leak; or, if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed, it is determined that the target memory block has a memory leak.
[0104] The final reference count of the target memory block can be the reference count recorded when the target process terminates. When the final reference count of the target memory block is zero, it can be determined that the references and releases of the target memory block allocated by the target process are corresponding, and there are no cases of unreleased or unallocated memory, thus confirming that there is no memory leak.
[0105] For example, upon termination of the target process, the reference summary information for each target memory block, primarily including the final reference count of each memory block, can be viewed through a memory reference summary file to determine whether a memory leak has occurred. If the final reference count of any target memory block is not zero, it is determined that the target memory block allocated by the target process is still in use and has not been released, thus indicating a potential memory leak risk. If the application corresponding to any target process exits, the GPU context corresponding to the target process is destroyed, and the final reference count is still not zero, it can be directly determined that the target memory block corresponding to that target process has a memory leak.
[0106] This application can quickly and accurately determine whether a target memory block has a memory leak by checking whether the final reference count of the memory block is zero, thereby improving the processing speed of memory leaks.
[0107] In some embodiments, if the final reference count is zero, the memory leak detection device can determine that the target memory block does not have a memory leak. Accordingly, when the target memory block is destroyed, if the final reference count is zero, the record information of the target memory block can be deleted from the global hash table.
[0108] When the final reference count is zero, this application deletes the record information of the target memory block in the global hash table when destroying the target memory block, thereby reducing invalid information in the global hash table and reducing the consumption of computing resources.
[0109] The following section explains how to determine the location of a memory leak based on the changes in the call stack information of the target memory block during each reference count update.
[0110] In some embodiments, the memory leak detection device can obtain change records from a memory reference record file. Then, based on the change records, the device determines the code path of the unreleased target memory block. Finally, based on the code path of the unreleased target memory block, the device determines the location of the memory leak in the target memory block.
[0111] The code path can be the location of the code executed by the target process, such as a specific line of code.
[0112] For example, by using the call stack information of the target memory block recorded in the memory reference log file during reference count updates, the code path when the target memory block was allocated, the code path when it was referenced when the reference count increased, and the code path when it was released when the reference count decreased can be determined. Since reference count updates and call stack information are in one-to-one correspondence, correspondingly, by recording the changes in the code path when referencing and releasing the target memory block, it is possible to accurately identify code paths that only reference but do not release the target memory block. Subsequently, the source code of the code path where the target memory block was not released can be mapped to determine the location of the memory leak in the target memory block.
[0113] It should be understood that by recording the changes in the call stack information of the target memory block when the reference count is updated in the memory reference record file, the code path of the unreleased target memory block can be determined. Then, by using the code path of the target memory block, the location of the memory leak in the source code can be quickly and accurately located.
[0114] In this application, a monitoring mechanism using a global hash table and call stack tracing can dynamically record the allocation, reference, and release of the target memory blocks of the GPU during the execution of the target process's code, and locate unreleased memory blocks and their call sources. This allows for a comprehensive search and extraction of the context information of the call stack closely related to the target process. Since the context information of the call stack covers various key data during the execution of the target process, the location of memory leaks can be determined quickly and accurately, effectively improving the processing speed of memory leaks.
[0115] The memory leak detection method provided in this application, in response to a memory allocation request from a target process, allocates a target memory block to the target process; initializes the reference count of the target memory block allocated to the target process; monitors the reference count of the target memory block during the execution of the target process; when the reference count of the target memory block is updated, determines the current call stack information of the target memory block, wherein the reference count is updated when the target memory block is referenced or released by the target process; in the case of a memory leak in the target memory block, obtains the change record of the call stack information of the target memory block at each reference count update, and determines the memory leak location based on the change record. Because the current call stack information of the target memory block is determined in real time when the reference count of the target memory block is updated, the memory leak location can be determined by the change record of the call stack information of the target memory block at each reference count update, eliminating the need for manual location of the memory leak, thereby effectively improving the processing speed of memory leaks.
[0116] In one exemplary embodiment, such as Figure 2 As shown, another method for detecting video memory leaks is provided. Taking the application of this method to the aforementioned video memory leak detection device as an example, the method for detecting video memory leaks includes steps S201 to S211. Wherein:
[0117] S201. Create a global hash table.
[0118] S202. Allocate a record entry for each target memory block of the image processor in the global hash table. The record entry is used to record the information of the corresponding target memory block.
[0119] S203. Based on the record entries of each target memory block in the global hash table, save the reference summary information of each target memory block in the memory reference summary file.
[0120] The reference summary information includes the address of the target video memory block, the identifier of the target process, the size of the target video memory block, and the reference count of the target video memory block.
[0121] S204. In response to the target process's video memory allocation request, allocate a target video memory block for the target process.
[0122] S205. Initialize the reference count of the target video memory block allocated to the target process.
[0123] S206. In the record entry corresponding to the target video memory block allocated to the target process in the global hash table, record the allocation call stack information and the first reference call stack information corresponding to the target video memory block allocated to the target process.
[0124] S207. Receive monitoring instruction information.
[0125] Among them, the monitoring indication information is used to indicate at least one target memory block to be monitored.
[0126] S208. Obtain the address of at least one target memory block to be monitored from the memory reference summary file.
[0127] S209. Write the address of at least one target memory block to be monitored into the memory reference filter file using the target command.
[0128] The target command is used to instruct the monitoring of a specified target memory block.
[0129] S210. Based on the video memory reference filter file, monitor the reference count of the target video memory block during the execution of the target process.
[0130] S211. In the case of a memory leak in the target memory block, obtain the change record of the call stack information of the target memory block at each reference count update, and determine the location of the memory leak based on the change record.
[0131] In some embodiments, obtaining a record of changes in the call stack information of the target memory block during each reference count update includes: obtaining the change record from a memory reference record file. Determining the memory leak location based on the change record includes: determining the code path of the unreleased target memory block based on the change record; and determining the memory leak location of the target memory block based on the code path of the unreleased target memory block.
[0132] In some embodiments, the memory leak detection device can also obtain the final reference count of the target memory block from the memory reference summary file when the target process terminates. If the final reference count is not zero, it is determined that the target memory block has a memory leak; or, if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed, it is determined that the target memory block has a memory leak. If the final reference count is zero, it is determined that the target memory block does not have a memory leak; the record information of the target memory block is deleted from the global hash table.
[0133] The memory leak detection method provided in this application, in response to a memory allocation request from a target process, allocates a target memory block to the target process; initializes the reference count of the target memory block allocated to the target process; monitors the reference count of the target memory block during the execution of the target process; when the reference count of the target memory block is updated, determines the current call stack information of the target memory block, wherein the reference count is updated when the target memory block is referenced or released by the target process; in the case of a memory leak in the target memory block, obtains the change record of the call stack information of the target memory block at each reference count update, and determines the memory leak location based on the change record. Because the current call stack information of the target memory block is determined in real time when the reference count of the target memory block is updated, the memory leak location can be determined by the change record of the call stack information of the target memory block at each reference count update, eliminating the need for manual location of the memory leak, thereby effectively improving the processing speed of memory leaks.
[0134] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0135] Based on the same inventive concept, this application also provides a memory leak detection device for implementing the memory leak detection method described above. The solution provided by this device is similar to the solution described in the above method; therefore, the specific limitations in one or more memory leak detection device embodiments provided below can be found in the limitations of the memory leak detection method described above, and will not be repeated here.
[0136] In one exemplary embodiment, such as Figure 3 As shown, a memory leak detection device 300 is provided, comprising: a memory allocation module 301, a reference count monitoring module 302, an information recording module 303, and a leak determination and detection module 304, wherein:
[0137] The video memory allocation module 301 is used to initialize the reference count of the target video memory block allocated to the target process in response to the video memory allocation request of the target process.
[0138] The reference count monitoring module 302 is used to monitor the reference count of the target memory block during the execution of the target process.
[0139] The information recording module 303 is used to determine the current call stack information of the target memory block when the reference count of the target memory block is updated. The reference count is updated when the target memory block is referenced or released by the target process.
[0140] The leak detection module 304 is used to obtain the change record of the call stack information of the target memory block during each reference count update when there is a memory leak in the target memory block, and determine the location of the memory leak based on the change record.
[0141] In one embodiment, the information recording module 303 is specifically used to create a global hash table; allocate a record entry for each target memory block of the image processor in the global hash table, and the record entry is used to record the information of the corresponding target memory block; according to the record entries of each target memory block in the global hash table, save the reference summary information of each target memory block in the memory reference summary file; wherein, the reference summary information includes the address of the target memory block, the identifier of the target process, the size of the target memory block, and the reference count of the target memory block.
[0142] In one embodiment, the reference count monitoring module 302 is further configured to receive monitoring instruction information, which indicates at least one target memory block to be monitored; obtain the address of at least one target memory block to be monitored from the memory reference summary file; write the address of at least one target memory block to be monitored into the memory reference filter file through a target command; wherein the target command is used to indicate the monitoring of a specified target memory block; and monitor the reference count of the target memory block during the execution of the target process according to the memory reference filter file.
[0143] In one embodiment, the information recording module 303 is further configured to record, in the memory reference record file, the call stack information of at least one target memory block to be monitored when the reference count is updated, based on the address of at least one target memory block to be monitored.
[0144] In one embodiment, the leak detection module is specifically used to obtain change records from the video memory reference record file; determine the code path of the unreleased target video memory block based on the change records; and determine the video memory leak location of the target video memory block based on the code path of the unreleased target video memory block.
[0145] In one embodiment, the information recording module 303 is further configured to record the allocation call stack information and the first reference call stack information corresponding to the target memory block allocated to the target process in the record entry corresponding to the target memory block allocated to the target process in the global hash table.
[0146] In one embodiment, the leak detection module 304 is further configured to obtain the final reference count of the target memory block in the memory reference summary file when the target process ends; if the final reference count is not zero, determine that the target memory block has a memory leak; or, if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed, determine that the target memory block has a memory leak; if the final reference count is zero, determine that the target memory block does not have a memory leak; and delete the record information of the target memory block in the global hash table.
[0147] Each module in the aforementioned memory leak detection device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of a computer device in software form, so that the processor can call and execute the corresponding operations of each module.
[0148] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 4As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When executed by the processor, the computer program implements a method for detecting video memory leakage. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0149] Those skilled in the art will understand that Figure 4 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0150] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the above-described method for detecting video memory leaks.
[0151] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the above-described method for detecting video memory leaks.
[0152] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the above-described method for detecting video memory leaks.
[0153] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0154] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0155] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0156] The above embodiments are merely illustrative of several implementation methods of this application, and their descriptions are relatively specific and detailed. However, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A method for detecting video memory leakage, characterized in that, The method includes: In response to the target process's video memory allocation request, a target video memory block is allocated to the target process; Initialize the reference count of the target video memory block allocated to the target process; During the execution of the target process, the reference count of the target video memory block is monitored; When the reference count of the target memory block is updated, the current call stack information of the target memory block is determined, wherein the reference count is updated when the target memory block is referenced or released by the target process; If the target memory block has a memory leak, obtain the change record of the call stack information of the target memory block at each reference count update, and determine the memory leak location based on the change record.
2. The method according to claim 1, characterized in that, Before allocating a target memory block to the target process in response to the target process's memory allocation request, the method further includes: Create a global hash table; In the global hash table, a record entry is assigned to each target memory block of the image processor, and the record entry is used to record the information of the corresponding target memory block; Based on the record entries of each target memory block in the global hash table, the reference summary information of each target memory block is saved in the memory reference summary file. The reference summary information includes the address of the target video memory block, the identifier of the target process, the size of the target video memory block, and the reference count of the target video memory block.
3. The method according to claim 2, characterized in that, The step of monitoring the reference count of the target memory block during the execution of the target process includes: Receive monitoring instruction information, the monitoring instruction information being used to indicate at least one target memory block to be monitored; Obtain the address of the at least one target memory block to be monitored from the memory reference summary file; The address of at least one target memory block to be monitored is written into the memory reference filter file by a target command; wherein the target command is used to instruct the monitoring of a specified target memory block; According to the memory reference filtering file, the reference count of the target memory block is monitored during the execution of the target process.
4. The method according to claim 3, characterized in that, The method further includes: Based on the address of the at least one target memory block to be monitored, record the call stack information of the at least one target memory block to be monitored when the reference count is updated in the memory reference record file.
5. The method of claim 4, wherein, The step of obtaining the change record of the call stack information of the target memory block during each reference count update includes: The change record is obtained from the video memory reference record file; Determining the location of the memory leak based on the change record includes: Based on the change records, determine the code path for which the target video memory block has not been released; Based on the code path of the unreleased target memory block, the location of the memory leak in the target memory block is determined.
6. The method of claim 2, wherein, After allocating a target memory block for the target process in response to its memory allocation request, the method further includes: In the record entry corresponding to the target memory block allocated to the target process in the global hash table, the allocation call stack information and the first reference call stack information corresponding to the target memory block allocated to the target process are recorded.
7. The method of claim 4, wherein, The method further includes: When the target process terminates, obtain the final reference count of the target memory block in the memory reference summary file; If the final reference count is not zero, it is determined that the target memory block has a memory leak; or, if the final reference count is not zero, the application corresponding to the target process exits, and the GPU context corresponding to the target process is destroyed, it is determined that the target memory block has a memory leak. If the final reference count is zero, it is determined that the target memory block does not have a memory leak; the record information of the target memory block is deleted from the global hash table.
8. A device for detecting video memory leakage, characterized in that, The device includes: The video memory allocation module is used to initialize the reference count of the target video memory block allocated to the target process in response to the video memory allocation request of the target process. The reference count monitoring module is used to monitor the reference count of the target memory block during the execution of the target process. The information recording module is used to determine the current call stack information of the target memory block when the reference count of the target memory block is updated, wherein the reference count is updated when the target memory block is referenced or released by the target process. The leak detection module is used to obtain the change record of the call stack information of the target memory block during each reference count update when the target memory block has a memory leak, and to determine the location of the memory leak based on the change record.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.