473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Returning memory

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.
Jun 1 '06 #1
6 1638
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]
- mv*@spam.guard.caspershouse.com

"victor" <vi****@somewhere.com> wrote in message
news:36********************************@4ax.com...
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.

Jun 1 '06 #2
Great, Nicholas; thanks for the response.
I'll implement it first thing tomorrow!
greetz, victor.


On Thu, 1 Jun 2006 16:21:07 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
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.


Jun 1 '06 #3
Nicholas,

Works perfectly - just what I need! ('used the using method)
Appreciate your answering manner: clear, immediately usable snippets.
Thank you for sharing your knowledge!
greetz, Victor
============

On Thu, 1 Jun 2006 16:21:07 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
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.


Jun 2 '06 #4
I would use the more verbous form.

The trick using "using" is neat, but it's still a trick. The construct
is only used to call dispose, it serves no other purpose. IMHO the code
gets clearer if it just calls dispose instead of causing the call as a
byproduct of an unused construct.

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Jun 2 '06 #5
I, however, prefer "using". As long as the syntax is clear and snappy, why
not use it?

It's similar to "with" in Lisp, if I recall correctly.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:uA**************@TK2MSFTNGP05.phx.gbl...
I would use the more verbous form.

The trick using "using" is neat, but it's still a trick. The construct is
only used to call dispose, it serves no other purpose. IMHO the code gets
clearer if it just calls dispose instead of causing the call as a
byproduct of an unused construct.

Nicholas Paldino [.NET/C# MVP] wrote:
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.


Jun 3 '06 #6
I'm not against the use of "using" per se. I use it frequently myself.

I couldn't find anything about "with" in lisp. If it works like "with"
in other languages (VB, Pascal), it's nothing like "using".

The normal use of the "using" keyword is when you create an object that
you want to dispose when you are done with it. If it's used to dispose
an object that you don't create at that time, you have to understand
exactly what the entire piece of code is doing before you can see why
"using" is used at all.

Michael A. Covington wrote:
I, however, prefer "using". As long as the syntax is clear and snappy, why
not use it?

It's similar to "with" in Lisp, if I recall correctly.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:uA**************@TK2MSFTNGP05.phx.gbl...
I would use the more verbous form.

The trick using "using" is neat, but it's still a trick. The construct is
only used to call dispose, it serves no other purpose. IMHO the code gets
clearer if it just calls dispose instead of causing the call as a
byproduct of an unused construct.

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Jun 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: benevilent | last post by:
Hey, I'm trying to debug the memory allocation in an embedded use of the Python interpreter. The longer I leave my program running the more memory it consumes. The total number of objects in...
11
by: jong | last post by:
I have a problem with an C# application. The application is a 24x7 low volume message processing server. The server has a single thread of processing, running in a continuous loop, for each...
4
by: Pieter Claassen | last post by:
I have a number of functions that does the following: char * test(){ char * test; .... return test; }
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
12
by: shaun roe | last post by:
I'm reading about exception safety and wondering when I need to bother, and when a program can crash without hogging some resource. If I am only talking about memory allocated during a program,...
2
by: Chris | last post by:
I have a Bayesian simulation package that keeps running into memory allocation problems. I have a feeling this has something to do with Python (2.5.1.1) not freeing memory. The program essentially...
1
by: mw55309 | last post by:
Hi I am not a beginner to perl, but am a beginner to this problem! I create a perl object, read about 500,000 rows from a MySQL database, populate some structures and print out a table of...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
4
by: Christian Heimes | last post by:
Gabriel Genellina schrieb: Pure Python code can cause memory leaks. No, that's not a bug in the interpreter but the fault of the developer. For example code that messes around with stack frames...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.