Remember a memory leak problem in ArcObject development

In the process of developing desktop GIS processing software, I encountered a problem of program crash, the problem is also very simple, the program crash did not occur when exporting a small amount of data, but in the process of processing a large amount of data, the program crash occurred Question; This naturally made me think of the problem of memory leaks, but since I don't do much desktop development in C#, how to locate the variables of memory leaks has become my problem.

1. Find tools

First, run the program in the debug mode until the program crashes and an exception is thrown. It turns out that it is an OutOfMemory problem. The program that throws the exception is located within the loop, which also confirms the problem of memory leaks. The next step is to see what are the objects that have not been released during use, because the code was written by someone else, and after reading it for a long time, I didn’t find out which variable it is (this step is a test of coding experience, or it may be my lack of experience, in short found no problems).
Then I considered the tools. I have no experience with any tools. I searched on Baidu and found two solutions: Visual Leak Detector and the diagnostic tool that comes with Visual Studio. The former looks very awesome. I read the blog introduction , you can directly locate the line of the memory leak, which feels okay, but I can see that the code of others is in C language, we are C#, I don’t know it, forget it, let’s take a look at the diagnostic tool that comes with VS .

2. Learning Tools

You can read this blog here: VS memory leak three detection methods
1. Debugging-"Window-"Display diagnostic tools
insert image description here
2. Break the point at the place to be debugged, and select memory usage in the diagnostic window
insert image description here
3. Go to the first breakpoint 4. Execute
insert image description here
insert image description here
the next sentence of breakpoint debugging, and then click to capture a snapshot. If the memory usage increases, it will change.
insert image description here

3. Positioning problem

In the second section, use the diagnostic tool 3 and 4 to test multiple times, and find the data structure of the memory leak. Here, because the single data is relatively small, the difference may not be seen, and because it is in a loop, so add each in the code. Endpoints that are interrupted every 10,000 cycles, so that it is easy to post memory leak points.
insert image description here
It is found here that there is a memory leak in the ComReleaser object.
So look at the source code

comReleaser.ManageLifetime(featureBuffer);

The featureBuffer variable is created in the loop, and every time the above statement is created, it will be added to the comreleaser, and the code to release the memory is outside the loop, resulting in a memory explosion during the loop

4. Solve

Here, the above code is commented out, instead of manually managing the memory, the memory recovery is returned to the GC of C#. After the test, the memory leak problem is found to be solved, Bingo!

Guess you like

Origin blog.csdn.net/zhoulizhu/article/details/129878397