Re: Returning memory
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 1 Jun 2006 16:21:07 -0400
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.
.
- Follow-Ups:
- Re: Returning memory
- From: Göran Andersson
- Re: Returning memory
- From: victor
- Re: Returning memory
- From: victor
- Re: Returning memory
- References:
- Returning memory
- From: victor
- Returning memory
- Prev by Date: Re: Size of Dictionary Object
- Next by Date: RE: Cookie Expires In 2.0 Framework
- Previous by thread: Returning memory
- Next by thread: Re: Returning memory
- Index(es):
Relevant Pages
|