Solved

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

Posted on 2008-06-21
6
1,283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 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