Solved

GC not cleaing up my app !

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

827 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