Link to home
Start Free TrialLog in
Avatar of bastawhiz
bastawhiz

asked on

Memory Leak?

I have a problem with my software. What has been happening is that I have noticed a slight increase in the amount of memory that my software takes up every time it changes an image's picture. For example:

Image1.BackgroundImage = ImageList1.Images.Item(0)

This code snippet would increase the amount of memory used by the size of the image in the imagelist.

One other code snippetsthat I have found to increase memory usage is:

    Public Function ResImage(ByVal Resource As String) As Bitmap

        Dim image As System.IO.Stream = GStreamResource(Resource)

        Return New Bitmap(image)

    End Function

    Public Function GStreamResource(ByVal Name As String) As IO.Stream

        Return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(Application.ProductName & "." & Name)

    End Function

and then calling:

ClickyImage.Image = ResImage("MyLittlePNG.png")

Am I thinking incorrectly that I am somehow wrongly assigning a picture to an image? Is VB just beign odd? AM I A BAD PROGRAMMER?!

Thank you.
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

In .NET, memory is not necessarily freed when you think it is. Look under "garbage collection" for all the details.

The long and short of it is that, even when memory is deallocated, .Net applications reserve it for potential future use until the application is terminated.

Avatar of bastawhiz
bastawhiz

ASKER

Ok, just for clarification, I have been using the Garbage Collection sub:

System.GC.Collect()

after every one of the subs I have put together above. Even so, I am unable to 'un-reserve' this memory. Is there a way to do that? Performance is not an issue in this software.
Again... even when the garbage is collected, the memory is still allocated to the application, until it is forced to release it bythe OS.

Think of a spoiled child (the application) who refuses to let any of their siblings/firends play with any of their toys (memory). Every time the child gets a chance, they grab another toy, and sit on it, until they have amassed a huge pile of toys that no one else can play with. Then, along come the parents (OS) and straighten everything out...

Thank you very much. Just to make the point distribution fair, I have a breif second question and if you can answer I'll add a little incentive ;-).

Is/are there any websites out there that specifically document the performance or speed of various subs and functions in .Net (I know they change between languages, but in general).

For example, mapping the speed of Instr() or Left() or Mid().


If you can give me a website (or more than one, for that matter), I will make sure you get at least 500 points.

ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

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
Thank you vary much! This is excellent information that I shall begin to read.

Thank you again.