Re: Returning memory

Tech-Archive recommends: Fix windows errors by optimizing your registry



Victor,

That is exactly what you do. However, a Bitmap does use some unmanaged
resources (it implements IDisposable), so I would be concerned about leaving
those for the GC to clean up.

To do this, I would call dispose on the image that the image holder is
currently holding, before you assign the new bitmap to it, like so:

private void GetImageObject()
{
// Get the old image.
imgImage = picboxImageHolder.Image;

// Dispose.
imgImage.Dispose();

// Set the new bitmap.
picboxImageHolder.Image = new Bitmap(myStream);
}

However, I would use the using statement, like so:

using (Image oldImage = picboxImageHolder.Image)
{
// Set the new image.
picboxImageHolder.Image = new Bitmap(myStream):
}

This will set the image to the new image, and then dispose of the old
image (assuming there is one) when the new image is set.

It is also more concise.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"victor" <victor@xxxxxxxxxxxxx> wrote in message
news:36fu72d8gl10167l5hlkk6be04nf6ojscu@xxxxxxxxxx
Hi,
I've the following question: in my app I used the following construct
to acquire images:

Image imgImage;
Stream myStream=null;
...
private void GetImageObject()
{
imgImage = new Bitmap(myStream);
picboxImageHolder.Image=imgImage;
...
}
--------------
The GetImage method is being invoked every second thru a timer tick.
This results in a cumulating memory consumption (seen in TaskManager).
Since 'delete' isn't available in C#, how should I return this memory?
Rely on GarbageCollector?
Please advise - thank you!
victor.


.



Relevant Pages

  • Re: memory leak
    ... Adding GC.Collectcauses the memory to drop, ... Now do you still agree that the GC will cleanup Bitmap objects? ... Dispose is not a required call - it is optional. ... done with those resources. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: memory leak
    ... Adding GC.Collectcauses the memory to drop, ... GC.Collect in the exception handler, ... Now do you still agree that the GC will cleanup Bitmap objects? ... Dispose is not a required call - it is optional. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: memory leak
    ... ArgumentException on Full Framework) when I have a ton of virtual memory ... "new Bitmap" line. ... Dispose is not a required call - it is optional. ... you are done with those resources. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: memory leak
    ... Adding GC.Collectcauses the memory to drop, ... Now do you still agree that the GC will cleanup Bitmap objects? ... Dispose is not a required call - it is optional. ... done with those resources. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: memory leak
    ... bug bug in another thread). ... Adding GC.Collectcauses the memory to drop, ... Now do you still agree that the GC will cleanup Bitmap objects? ... Dispose is not a required call - it is optional. ...
    (microsoft.public.dotnet.framework.compactframework)