What does it mean to set large fields to null

A

Andrew Falanga

Hi,

I'm going through a link provided to me from someone here earlier
about implementing the IDisposable interface on factory patterned
objects. In the document from MSDN I'm seeing this comment in the
Dispose( ) function:

// Set large fields to null.

Is this something as simple as this (an example):

byte[] byteArray = new byte[/*some really large number*/];

byteArray = null;


Ultimately, this question has to do with how to release a large amount
of memory. I'm working graphic images and one class needs to work
with PNM, or PPM, graphics. There isn't anything in GDI+ that handles
this type natively so I've implemented it myself.

Since implementing IDisposable I'd like to know about how I can grab
enough memory to load the graphic data into memory, similar to a
Bitmap object, and then, like the Bitmap, call Dispose( ) on my object
and watch the memory drop. From my C++ experience I would do this by
freeing a pointer. However, I don't know how to hold onto a valid
pointer reference outside of unsafe code, and I'd need to for this to
work. So, how do I allocate enough memory to hold the graphic data in
memory and then release with a call to Dispose( ) when I want to?

Thanks,
Andy
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I'm going through a link provided to me from someone here earlier
about implementing the IDisposable interface on factory patterned
objects.  In the document from MSDN I'm seeing this comment in the
Dispose( ) function:

      // Set large fields to null.

Is this something as simple as this (an example):

byte[] byteArray = new byte[/*some really large number*/];

byteArray = null;

IDisposable is not related to size of the memory to be "released" it
only has to do with unmanaged resources, if your code does not work
with unmanaged resources. For example if you have a FileStream at some
point of its implementation has to hold an handle from the OS, that
handle needs to be released at some point.
Bitmap I'm pretty sure use some of GDI+ features so it also need to
release it.

Also in your post you talk about seeing a decreased in used memory,
you might no see that in .NET the memory that the OS assign to the
process, has no relation to how .net handle that memory. For example
if you need to load a big image the OS will be requested some memory,
but when you "dispose" the image the memory is nt returned at once to
the OS, but it's marked as free in the managed process.

Hp[e you understand the above, it might not be clear at once.
 
A

Andrew Falanga

I'm going through a link provided to me from someone here earlier
about implementing the IDisposable interface on factory patterned
objects.  In the document from MSDN I'm seeing this comment in the
Dispose( ) function:
      // Set large fields to null.
Is this something as simple as this (an example):
byte[] byteArray = new byte[/*some really large number*/];
byteArray = null;

IDisposable is not related to size of the memory to be "released" it
only has to do with unmanaged resources, if your code does not work
with unmanaged resources. For example if you have a FileStream at some
point of its implementation has to hold an handle from the OS, that
handle needs to be released at some point.
Bitmap I'm pretty sure use some of GDI+ features so it also need to
release it.

Also in your post you talk about seeing a decreased in used memory,
you might no see that in .NET the memory that the OS assign to the
process, has no relation to how .net handle that memory. For example
if you need to load a big image the OS will be requested some memory,
but when you "dispose" the image the memory is nt returned at once to
the OS, but it's marked as free in the managed process.

Hp[e you understand the above, it might not be clear at once.

Thanks to both Pete and Ignacio. Some time after posting, I did find
what I'm looking for. I found in the
System.Runtime.InteropServices.Marshal.* class a couple of function to
allocate and free memory. The idea is to mimic, as closely as
possible, what's happening in the GDI+ libraries. It appears that the
image is loaded directly into memory when making a Bitmap class and
then released with a call to Dispose( ). Because I'm using GDI+ for
TIFF, JPEG and PNG, I wanted the similar things to be happening when
my class and PNM files.

Thanks again. I believe that what I've found in the libraries will
suffice. Some preliminary testing on a foo project has proven quite
favorable.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top