Solved

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

Posted on 2008-06-21
6
1,260 Views
Last Modified: 2010-04-21
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
                 Me.Controls.Remove(ControlObj)
                 ControlObj = Nothing
            Next
End While
Me.Close()

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.ShowDialog()
        XtraCharts = Nothing
        XtraCharts.Dispose()
        GC.Collect()
        GC.WaitForPendingFinalizers()

By the way, if I try the following:

            While Not XtraCharts.IsDisposed
                XtraCharts = Nothing
                XtraCharts.Dispose()
                GC.Collect()
                GC.WaitForPendingFinalizers()
            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,
0
Comment
Question by:luiggye
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21839436
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.
0
 
LVL 39

Expert Comment

by:appari
ID: 21839544
are you setting CacheToMemory property to true?
if you are then try calling ClearCache method.
0
 
LVL 7

Expert Comment

by:Zippit
ID: 21839589
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

System.GC.Collect()

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21839640
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.
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 21845170
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()
        Try
            Dim Mem As Process
            Mem = Process.GetCurrentProcess()
            SetProcessWorkingSetSize(Mem.Handle, -1, -1)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnReduceMemory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReduceMemory.Click
        SetProcessWorkingSetSize()
        MsgBox("See memory used now!")
    End Sub
0
 

Author Closing Comment

by:luiggye
ID: 31469485
Thanks for your response, it was so usefully.
I will use it carefully...!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question