What is the best way to release memory in VB .NET ?

In VB.NET, I open a form in my application where I will place some charts using XTRACHARTS library.

Then, the memory gets is increased a lot, and when I close the form, the memory is never released any more.

Following, the sub that I close the form.

Private Sub XtraCharts_Close(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

Me.PanChart.Controls.Remove(AreaChart)   ' AreaChart is the chart object defined as private
Me.AreaChart = Nothing
' I believe that the following is not necessary, but I put it, in order to see what happen...!!
While Me.Controls.Count > 0
           For Each ControlObj As Windows.Forms.Control In Me.Controls
                 ControlObj = Nothing
End While

End Sub

Following, the code executed in the main form, that calls the "XtraCharts" form, and where I am trying to release the resources used by the charts objects (inside the "XtraCharts" form)

        XtraCharts = Nothing

By the way, if I try the following:

            While Not XtraCharts.IsDisposed
                XtraCharts = Nothing
            End While

The loop never ends. In other words, XtraCharts.IsDisposed is allways false.....!!!

I am so confused about how to use the GC.

I need an example of the correct way to guarantee to release the unused memory...!!!

Thanks in advance,
Who is Participating?
iboutchkineConnect With a Mentor Commented:
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean

    Public Sub SetProcessWorkingSetSize()
            Dim Mem As Process
            Mem = Process.GetCurrentProcess()
            SetProcessWorkingSetSize(Mem.Handle, -1, -1)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnReduceMemory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReduceMemory.Click
        MsgBox("See memory used now!")
    End Sub
Jaime OlivaresSoftware ArchitectCommented:
Theoretically, nothing of these is needed, just create the dialog and leave the user to close it without any other extra cleaning action, unless the chart library has an specific cleaning requirement.
are you setting CacheToMemory property to true?
if you are then try calling ClearCache method.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

You could also try forcing a garbage collection after releasing all this stuff.  However, this isn't something you want to do often.  It is a fairly expensive operation so you won't want to call it inside a loop or anything


Open in new window

Jaime OlivaresSoftware ArchitectCommented:
Collect is not needed, and not guaranteed to collect inmediately. the .net environment will release memory when it needs, unless there is some kind of memory leak.
luiggyeAuthor Commented:
Thanks for your response, it was so usefully.
I will use it carefully...!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.