Releasing memory held by array?

C

Chris Ashley

How can I manually release the memory held by a large array? I have
tried setting the array to null but no joy. The garbage collector
appears to be ignoring it completely.
 
J

Jon Skeet [C# MVP]

Chris said:
How can I manually release the memory held by a large array? I have
tried setting the array to null but no joy. The garbage collector
appears to be ignoring it completely.

How have you come to the conclusion that it's being ignored? Can you
give a short but complete example demonstrating the problem?

Jon
 
C

Chris Ashley

Jon said:
How have you come to the conclusion that it's being ignored? Can you
give a short but complete example demonstrating the problem?

Jon

public void SetByteData()
{
int intTotalStrips = (infoHdr.biHeight / STRIP_HEIGHT) + 1;
int intLastStripHeight = infoHdr.biHeight % STRIP_HEIGHT;
theBitmaps = new RawBitmap[intTotalStrips];

// Generate standard strips
for (int i = 0; i < intTotalStrips; i++)
{
theBitmaps = new RawBitmap(fileHdr, infoHdr);
theBitmaps.SetHeight(STRIP_HEIGHT);
theBitmaps.BytImgData = ReadLines(ioBinaryStream,
STRIP_HEIGHT);
}
// Generate last strip
int intLastBound = theBitmaps.GetUpperBound(0);
//theBitmaps[intLastBound] = new RawBitmap(fileHdr, infoHdr);
//theBitmaps[intLastBound].SetHeight(intLastStripHeight);
//theBitmaps[intLastBound].BytImgData =
ReadLines(ioBinaryStream, intLastStripHeight);

}


This is where my memory shoots up. The RawBitmap class holds a huge
array of bytes, and in turn the class that this method belongs to holds
approximately 100 instances of RawBitmap in an array. The method is
being called from an ASPX page but the memory is never being released,
even at the end of the page lifecycle. In task manager the aspnet
process increases its memory usage with each refresh of the page.
 
C

Chris Ashley

Chris said:
This is where my memory shoots up. The RawBitmap class holds a huge
array of bytes, and in turn the class that this method belongs to holds
approximately 100 instances of RawBitmap in an array. The method is
being called from an ASPX page but the memory is never being released,
even at the end of the page lifecycle. In task manager the aspnet
process increases its memory usage with each refresh of the page.

Manually calling GC.Collect in the Dispose method for my class appears
to have solved it.
 
J

Jon Skeet [C# MVP]

Chris said:
Manually calling GC.Collect in the Dispose method for my class appears
to have solved it.

That suggests it's reached generation 2 - it would eventually get
collected, but there's nothing to trigger garbage collection
automatically at the end of the page lifecycle.

I *think* you'd have found that after a fair number of requests, the GC
would have collected the array.

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

It doesn't necessarily have to go to Gen 2. If the arrays that the
RawBitmap instances point to are large enough (the OP says that it holds a
huge array of bytes), then those might go to the Large Object Heap.

As for calling GC.Collect, it's not the best in ASP.NET environments.
ASP.NET apps get into a more predictable routine for GC than say, windows
apps (which have more erratic spikes when it comes to allocating and
deallocating memory). Calling GC.Collect will disrupt that routine.

And for what? You (the OP) are using Task Manager to gauge whether or
not memory is being released. Task Manager only reports the ^working set^
to you, and not whether or not your memory is being collected properly.

If you are going to performance tune an ASP.NET application, you need to
know what to look at. In this case, it is the performance counters in .NET,
not Task Manager. If anything, you ^should^ see the value in Task Manager
increase. I'd almost say that if it wasn't as you do work, there was
something wrong.

Here is a link to a list of performance counters which would be of help
to you:

http://msdn2.microsoft.com/en-us/library/x2tyfybc.aspx

And by all means, take out the call to GC.Collect in your Dispose
implementation. It's not helping you. In fact, you are probably going to
see a drop in throughput as a result.
 
N

Nicholas Paldino [.NET/C# MVP]

And to elaborate even more, one of the people at MS just wrote about why
you shouldn't use Task Manager this morning:

http://blogs.msdn.com/tess/archive/2006/09/06/742568.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
It doesn't necessarily have to go to Gen 2. If the arrays that the
RawBitmap instances point to are large enough (the OP says that it holds a
huge array of bytes), then those might go to the Large Object Heap.

As for calling GC.Collect, it's not the best in ASP.NET environments.
ASP.NET apps get into a more predictable routine for GC than say, windows
apps (which have more erratic spikes when it comes to allocating and
deallocating memory). Calling GC.Collect will disrupt that routine.

And for what? You (the OP) are using Task Manager to gauge whether or
not memory is being released. Task Manager only reports the ^working set^
to you, and not whether or not your memory is being collected properly.

If you are going to performance tune an ASP.NET application, you need
to know what to look at. In this case, it is the performance counters in
.NET, not Task Manager. If anything, you ^should^ see the value in Task
Manager increase. I'd almost say that if it wasn't as you do work, there
was something wrong.

Here is a link to a list of performance counters which would be of help
to you:

http://msdn2.microsoft.com/en-us/library/x2tyfybc.aspx

And by all means, take out the call to GC.Collect in your Dispose
implementation. It's not helping you. In fact, you are probably going to
see a drop in throughput as a result.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jon Skeet said:
That suggests it's reached generation 2 - it would eventually get
collected, but there's nothing to trigger garbage collection
automatically at the end of the page lifecycle.

I *think* you'd have found that after a fair number of requests, the GC
would have collected the array.

Jon
 

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