Solved

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

Posted on 2008-06-21
6
1,233 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Find value in LINQ result ? 4 17
regex to allow alphanumeric characters and \ 2 22
SQl help with selection 14 44
Showdialog 8 20
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now