Solved

GC not cleaing up my app !

Posted on 2006-07-10
5
215 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
  • 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 50 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 450 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

9 Experts available now in Live!

Get 1:1 Help Now