Link to home
Start Free TrialLog in
Avatar of Kinger247
Kinger247

asked on

GC not cleaing up my app !

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
Avatar of AlexFM
AlexFM

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.
Avatar of Kinger247

ASKER

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 ?

SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that worked great !
thankyou.