how delete array from memory

  • Thread starter Thread starter tony collier
  • Start date Start date
T

tony collier

This is an array question but it is based in my web page so i shall give
scenario:

when the client navigates to the page, the page takes some figures and does
loads of calulations in page_load which result in an array with 4.8 million
elements. i then extract a mere handful which is all the client needs and
then return these results to the client. I would therefore like to delete
the array from memory at the end of the page_load event. Can someone
please tell me how to do this. thanks.
 
But you can control the GC...

GC.Collect(GC.MaxGeneration); // forces a full collect
GC.Collect(GC.GetGeneration(myArray)); // forces a collect up to the generation
your array is in

This isn't recommended, but if you are doing this calculation simultaneously for
many users you may
have a worker process roll-over (denial of service for some short period of
time) before the weight
on the GC forces it to kick in and collect. You need to weight roll-overs using
the process counter
and if they are high then you should manually control the GC since you know the
best time to collect
your objects and the GC only makes guesses.
 
and if they are high then you should manually control the GC since you
know the
best time to collect
your objects and the GC only makes guesses.

Justin, have you implemented and tested this in a real world app? I'm not
trying to be rhetorical - I'm really asking, since I've never had the
experience of using GC outside of simple test/diagnostic apps (and everytime
I have tried to use it other places, people smarter than I have taken me to
task for it :). It still seems risky to me to call the GC, perhaps even
every time this page fires, in light of the potential expense of GC in
ASP.NET especially - but I will defer to our resident gurus.

One alternative for this kind of thing is running code in an AppDomain, then
unloading it, which I gave an example of doing here:
http://blogs.geekdojo.net/richard/archive/2003/12/10/428.aspx

This certainly has some expense to it, but seems to work well enough at
deterministically removing memory.

Richard
 
For about six months we used it in the .NET Terrarium application. Until the
GC caught up with our expectations and we didn't need it anymore. During that
time there was a deterministic location we could call from (between game ticks)
that caused a low impact on our application at hand and so it worked perfectly.

In this guy's case, he has a couple of options:
1. Don't GC at all, and hope that the worker process doesn't role over
constantly
2. Don't GC at all, and hope that the memory used on the array gets re-used for
the next array
3. GC if he finds that either 1 or 2 isn't coming true.

So what I am saying is pick the lesser of several evils depending on his own
goals and
inspection of what is happening on his servers. Generally speaking, #2 should
come into
play for him, since the memory should get re-used after it goes out of scope.
The largest problem
I see is that he is allocating a huge array, and allocating huge arrays
generally means finding
contiguous locations in memory for said arrays. Because of these types of
allocations in addition
to more normal allocations he might find situations where the managed heap grows
uncontrollably (e.g.
very fast in a short amount of time, causing the work process to role)
 
Okay, so I would suggest an option 4 - Do all 'excessive' memory work in a
temporary AppDomain, and reclaim the memory when the AppDomain is unloaded.
This is partly speculation on my part, but it seems this would mean
targeted, deterministic removal of only the memory allocated for the 'large
memory process' and leave the GC to do it's work normally on the range of
objects created in a normal ASP.NET page request.

Worth a try, IMHO,
Richard
 
Back
Top