Solved

GC not cleaing up my app !

Posted on 2006-07-10
5
216 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 32
vb.net 2 37
Vb.net dynamic formulas in runtime 11 63
C# Single Form 8 29
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

22 Experts available now in Live!

Get 1:1 Help Now