?
Solved

GC not cleaing up my app !

Posted on 2006-07-10
5
Medium Priority
?
223 Views
Last Modified: 2010-04-23
I'm running a test trying to resolve a few issues in my application. Basically I've got a memory leak and have created the below test.
But I can't understand what's going on. In the small application below, I add multiple (3mb) picturebox's to a panel on a form.

I watch the memory increase as I add them.
But when I remove them, why doesn't the memory come down ?

Is there a way of getting the GC to clean this up ?  Or some other way perhaps ?

I really don't understand the cleanup operation for .net ....


'//-- CODE ---------------------

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadImage()
    End Sub

    Private Sub LoadImage()
        Dim PictureBox As New PictureBox
        Static Counter As Integer = 0

        Counter += 1

        With PictureBox
            .Parent = Panel1
            .Load("c:\test.bmp")    '//-- A 3mb bimap ...
            .Top = 10 * Counter
            .Left = 10 * Counter
            .Width = 100
            .Height = 100
        End With
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Panel1.Controls.Clear()
    End Sub
End Class
0
Comment
Question by:Kinger247
[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
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17072435
It is impossible to make memory leak in pure .NET application, you need to use unmanaged code for this.
GC allows to application to consume almost all available memory before it decides to clear it. To keep memory clean you only need to dispose all disposable objects like bitmaps, files etc. before they go out of scope. I don't see any problem in your code.
0
 
LVL 10

Author Comment

by:Kinger247
ID: 17072554
I have tried GC.Collect, which works in the above app  after the clear, but doesn't work in my app which is obviously a bit more complicated than the above app.

When I first run my app, it consumes about 35mb. After about an hour of using the app, the memory rises to about 300mb.
Would this be seen as normal ?

0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 17072667
If you don't use unmanaged code, memory leaks are impossible.
In code sample which you posted, there is a lot of memory consumed, after pressing Button2 all controls with bitmaps are cleared, so there are objects for collecting. In more complicated application you can think that some objects should be collected, but there are still references to them somewhere. This is the only reason I see why GC doesn't collect them.
300 MB of memory is not problem for .NET application, and there is no need to call GC.Collect. Run application for a long time to check whether consumed memory is stabilizing at some level. But you your application doesn't release resources, don't expect for GC to clean memory. For example, application which loads large bitmaps and keeps all of them in some list without releasing, always increases memory, and finally crashes.
0
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 1800 total points
ID: 17073390
The problem might lie in how you are clearing the controls.  By calling the .Clear method, you're removing the controls from the panel, not destroying the object itself, so you still have those pictureboxes floating around.  To kill them off, you need to dispose and dereference them before the GC will acknowledge them as ready for collecting.  Try the following in your Button2 click event:

       For Each ctrl As Control In Panel1.Controls
            If Not ctrl Is Nothing Then
                ctrl.Dispose()
                ctrl = Nothing
            End If
        Next

        GC.Collect()
0
 
LVL 10

Author Comment

by:Kinger247
ID: 17075193
that worked great !
thankyou.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

801 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