Solved

GC not cleaing up my app !

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

696 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