Memory leak problems

I read a lot about this since I was asked to fix a C # application that has memory leak problems, but I could not find an answer to these 2 problems:

Consider the following code:

private static ArrayList list = new ArrayList(); public void Function() { list.add(object1); list.add(object2); //didn't call clear() prior to reusing list list = new ArrayList(); } 

Since the list was not cleared before creating a new one, will it generate some kind of garbage that will not be released after the static list is placed?

The second problem concerns Form.Dispose (). I see that many of the controls available in the designer’s view (i.e. Labels, picture boxes) require removal. It seems that calling Dispose () on the form will cause all these types of controls to be deleted as well (correct me if I'm wrong), which is odd, as the designer adds redundant void Dispose (bool disposing) , which doesn't do this. I assume this happens in the void Dispose (bool disposing) method of the Form base class.

The problem with the above is that it is not very clear to me what I need to do to ensure that all Form resources are correctly located. I don’t understand how Form knows which objects it needs to dispose of. For example, if in my form I have a field that is an IDisposable custom object, will the Form know what it needs to be disposed of? Or should I add the code needed to self-release the object?

Also, if I need to add code to place certain objects, then how can I deal with the fact that the designer has already overcome the void Dispose (bool disposing) method ? Should I edit the code created by the constructor or is there a cleaner way to do this?

I hope this should not have been embarrassing, it is a little difficult to explain. Thanks

+2
source share
3 answers

No, this is not a leak. When the garbage collector goes to search for references to objects, it will no longer find a link to the original ArrayList. You replaced it. Thus, it will automatically destroy this original ArrayList object, as well as all its elements, if they are not specified anywhere.

The Form class knows how to manage itself, as well as all the controls that are child windows in this form. This happens when the user closes the form, the WM_CLOSE message that Windows sends triggers for this code. The Form.Controls collection helps you find the link to all child controls so that it can also be disposed of.

However, this will not happen if you remove the controls from the form yourself. Now it's up to you to call Dispose (). In particular, the Controls.Clear () method is dangerous. What is unusual is that it causes a constant leak, the controls that you remove are supported by "parking". Which keeps the window handle alive, so you can move them elsewhere, for example, to another container window. If you do not actually move them, they will remain in this parking window forever. No other classes in the structure behave this way.

This leak is easily diagnosed using the tab Taskmgr.exe, Processes. View + Select Columns and mark USER Objects. If it steadily rises during the execution of your program, you lose control.

+8
source

What is the scope of your static arraylist. Believes that it has a shape. If so, it will not be deleted, since static objects are always considered root and have an application lifetime. In my experience, statics always gets more memory and gets promoted to 2nd because of this fact. If in doubt, take a .net profiler and check it out. You can also take memory dumps and analyze them with windbg to find out the real cause of the leak.

+1
source

In many memory management infrastructures, garbage collected and otherwise freeing up memory in an application usually does not cause the application to free up that memory, but instead records the fact that the memory should be available for future requests. Part of the idea of ​​garbage collection is that when user code requests memory, and the application knows that it has at least what is available immediately, neither the code nor the application will take care of “any memory” , which is not necessary for this request, or “free.” When the last available reference to an object is destroyed or becomes inaccessible, the object actually ceases to exist immediately there, but usually there isn’t much sense in trying to return the memory previously used by nonexistent objects to those since until such reclamation is required to fulfill a distribution request, or the “freed-up memory for time” relationship is as good as its probability.

Please note that in order to restore the system associated with an object, it is absolutely essential that no link exist for this object. If there are accessible, achievable weak references to an object that is otherwise unavailable, the garbage collector must invalidate all such references (so that they no longer identify the object) before the space used by the object can be recovered. If otherwise the unreachable object has a registered finalizer, the system should put the object in the queue of things that need to be updated immediately (which makes it unacceptable for reclamation) and unregister the finalizer.

Weak links and links used for finalization are automatically terminated by the GC when all other object references are abandoned and therefore do not cause memory leaks. However, there is another kind of help that can cause unpleasant leaks: subscribing to events from publishers who are experiencing subscribers. If object A subscribes to an event from object B, object A cannot be collected using garbage unless (1) it is unsubscribed from the event, or (2) B itself becomes suitable for garbage collection. I am puzzled by why Microsoft did not turn on some auto-unsubscribe features for events, but they did not.

+1
source

All Articles