How to free busy memory

I have a main window in my project and many other child widows inside the main one.
I noticed that. When I open the main window, it occupies 1500K of memory, when I open one child window, 6000K is added to the occupied memory.
When I open the second window, I do the same. When I close two child windows, the occupied memory is not freed.
So I want to free up occupied memory when I close child windows.
How can i do this?
Please advise me the code sample in vb.net, if possible.
This problem is often seen on computers on the LAN LAN on my computer (on a developer's computer that has an SQL server).

+4
source share
7 answers

Usage, as suggested by Pranay, will work just as it would by default call the Dispose method. otherwise explicitly you must call this.dispose () after calling this.close () in your child forms. But do not forget that you are not going to use child elements of the form or value after closing. Because the utility will finally clear everything.

MSDN example for disposing of an unmanaged resource

Imports System Imports System.ComponentModel ' The following example demonstrates how to create ' a resource class that implements the IDisposable interface ' and the IDisposable.Dispose method. Public Class DisposeExample ' A class that implements IDisposable. ' By implementing IDisposable, you are announcing that ' instances of this type allocate scarce resources. Public Class MyResource Implements IDisposable ' Pointer to an external unmanaged resource. Private handle As IntPtr ' Other managed resource this class uses. Private component As component ' Track whether Dispose has been called. Private disposed As Boolean = False ' The class constructor. Public Sub New(ByVal handle As IntPtr) Me.handle = handle End Sub ' Implement IDisposable. ' Do not make this method virtual. ' A derived class should not be able to override this method. Public Overloads Sub Dispose() Implements IDisposable.Dispose Dispose(True) ' This object will be cleaned up by the Dispose method. ' Therefore, you should call GC.SupressFinalize to ' take this object off the finalization queue ' and prevent finalization code for this object ' from executing a second time. GC.SuppressFinalize(Me) End Sub ' Dispose(bool disposing) executes in two distinct scenarios. ' If disposing equals true, the method has been called directly ' or indirectly by a user code. Managed and unmanaged resources ' can be disposed. ' If disposing equals false, the method has been called by the ' runtime from inside the finalizer and you should not reference ' other objects. Only unmanaged resources can be disposed. Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean) ' Check to see if Dispose has already been called. If Not Me.disposed Then ' If disposing equals true, dispose all managed ' and unmanaged resources. If disposing Then ' Dispose managed resources. component.Dispose() End If ' Call the appropriate methods to clean up ' unmanaged resources here. ' If disposing is false, ' only the following code is executed. CloseHandle(handle) handle = IntPtr.Zero ' Note disposing has been done. disposed = True End If End Sub ' Use interop to call the method necessary ' to clean up the unmanaged resource. <System.Runtime.InteropServices.DllImport("Kernel32")> _ Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean] End Function ' This finalizer will run only if the Dispose method ' does not get called. ' It gives your base class the opportunity to finalize. ' Do not provide finalize methods in types derived from this class. Protected Overrides Sub Finalize() ' Do not re-create Dispose clean-up code here. ' Calling Dispose(false) is optimal in terms of ' readability and maintainability. Dispose(False) MyBase.Finalize() End Sub End Class Public Shared Sub Main() ' Insert code here to create ' and use the MyResource object. End Sub End Class 

(Update) [Verification]

If your child form has a signature. By default, they are added to the form.

 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 
0
source

Some of the other answers to this question contain a disappointing amount of misinformation, while others greatly complicate the problem. There are many misconceptions related to garbage collection in .NET, and theories tied up here certainly do not help this problem.

First of all, using profiling memory in Windows Task Manager is a huge mistake . You will receive serious invalid information , and trying to change your application in accordance with this information will only lead to a worse situation, and not for the better. If you suspect that you have performance problems (and it is very doubtful that most applications will actually experience any problems), you need to invest in a proper memory profiler and use them instead.

Secondly, the whole point of garbage collection is that you do not need to worry about it. . And you do not need to worry not only about this, but you should not worry about it either. When writing applications targeted at the .NET Framework, you should not do or try to manage manual control manually. Resist the temptation to tinker with the internal workings of the garbage collector, and firmly put your fingers in your ears when someone tells you to call GC.Collect manually to make garbage collection happen. I suppose I should never have said, but there is hardly ever a reason for this. I much more often suspect code that manually causes garbage collection than anything else.

Why shouldn't you manually reference the garbage collection? Well, beyond the obvious argument that it defeats the whole point of using a managed language in the first place, because garbage collection is a very slow and expensive process . You want it to work as little as possible to maintain maximum performance. Fortunately, the programmers who implemented the garbage collection algorithms are much smarter and more experienced than you or me: they designed it to run only when it is needed, and not more often than that. You will not see the advantages of working with him more often, but you will see the disadvantage. This should be completely opaque to you as a programmer.

The only exception is when you work with unmanaged objects that are not collected or managed by the garbage collector. You can recognize these objects because they all implement the IDisposable interface , which provides the Dispose method for releasing unmanaged resources. On objects that expose this method, you must call it as soon as you finish using the object. Or even better, wrap the declaration and use the object in a using statement , which automatically processes the removal of the object, regardless of what (even if the exception is thrown in the code where you use the object, for example).

Of course, you will notice that some standard Windows Forms library objects implement the IDisposable method. The ubiquitous Form class , for example, provides the Dispose method. However, this does not necessarily mean that you are responsible for manually deleting these objects. In general, you only need to explicitly call the Dispose method on objects that you explicitly create - it's easy to remember, right? Objects created automatically by the Framework are also automatically destroyed by the Framework. For example, the controls that you place on the Form object at design time are automatically deleted when their container form is located. And the Form objects themselves are automatically deleted when they are closed. This is especially true for the question raised in your question. The documentation for the Form.Close method tells us the following:

When the form is closed, all resources created inside the object are closed and the form is deleted.

[.,]

Two conditions when the form is not placed on Close is when (1) it is part of an application with a multiple document interface (MDI) and the form is not visible; and (2) you displayed the form using ShowDialog . In these cases, you will need to manually call Dispose to mark all the garbage collection form controls.

Note that in general, you will not have to manually call Form.Dispose manually from your code. It is not possible for the user to close the MDI child form when the MDI parent is not visible, and if you accidentally closed the form in code when its parent is invisible, you can simply insert a call to Form.Dispose . When you display a form as a modal dialog using the ShowDialog method, you can conveniently wrap its creation and use it in the using statement.

Now recall that just calling the Dispose method on an object only frees up unmanaged resources and puts the object as available for garbage collection. It does not immediately release the memory declared by this object. This is important because this is exactly what your memory profiling attempts were aimed at. You know that objects are located because you mention that variables become inaccessible to you (you say that you are "losing your values"). This is because you cannot access located objects. This does not necessarily mean, however, that the memory they claimed has been completely released. Performing this task is the garbage collector that we have already installed, you should not monkey. He will wait until the memory is freed until the application runs or desperately needs to reuse that memory. Otherwise, it will defer collection and , still OK .

+13
source

use a block that automatically allocates memory

 Using { resourcelist | resourceexpression } [ statements ] End Using 
+2
source

This is something you should not care about.

The .NET Framework Garbage Collector will do the job for you.

The .NET Framework garbage collector manages the allocation and release of memory for your application. Each time you create a new object, a common execution language allocates memory for the object from the managed heap. In view, since the address space is available in a managed heap, runtime continues to allocate space for new objects.

Edit

You need to make sure that you are not using the resources that you have become free. Garbage collector functions are located in the GC class under the System namespace.

To call it, you can do GC.Collect() , although I suggest you read more about this topic and see some examples, like this

+1
source

The garbage collector will do the work for you, so you really shouldn't mind. You should have a deeper look inside if and only if you use unmanaged resources (com interop / PInvoke).

0
source

I am not an expert at VB.net, but as far as I know, it has a garbage collector. This usually means that closing child windows does not free up memory, but if you remove all references to child windows, the garbage collector can free it in the next run.

In addition, you can usually β€œask” the garbage collector, but it usually β€œsolves” itself at startup.

0
source

You can use one of the memory profilers, for example. ANTS Memory Profiler (see Using ANTI Memory Profiler to Track Memory Leaks in WinForms ). You can also use WinDbg, but without experience it will be more difficult than a specialized tool.

One of the common causes of a "memory leak" is to add an "external" event handler to form (for example, a static or long living object) and not delete it when the form is destroyed, so GC "thinks" that you have a link to the form instead of collecting your data.

Why and how to avoid memory leaks of the event handler?

.NET Case Leak Case Study: event handlers that made Baloon's memory

Garbage Collection Basics

Dispose, Finalization and Resource Management

How to identify memory leaks in a common language runtime

How to detect and avoid memory and resource leaks in .NET applications

0
source

Source: https://habr.com/ru/post/892219/


All Articles