Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-21
6
Medium Priority
?
1,304 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 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