Connecting Tech Pros Worldwide Forums | Help | Site Map

Garbage Collector - what am I doing wrong (Windows CE)

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 7 '09
Hello guys,

we are working on an application system for Windows CE which consists of several processes.

One of our devices only has 60 MB of RAM. It is divided in 8MB program storage and 52MB memory storage.

We have the following scenario:
Once our main application (and several small helper applications) is started 45 MB memory storage is in use.

That means only 7 MB are left. So far so good.

This main application gives the user the possibility to start several other tasks. Each task is another process.

And here comes the interesting part:
When the user tries to start one task it results in an systemoutofmemory exception. I understand, that makes sense.
BUT: When I now check the memory which is in use: it is reduced to 32MB - although the same processes are running as before.
So I guess somehow the GC must have noticed that memory is about to exceed and frees several MBs.

Another interesting fact:
When the user tries to start another task it does not result in an SOFM Exception. However, when I check the memory storage: it has been reduced to 32 MB as well - although no process has thrown an exception.

So my assumption is:
In the first scenario the GC frees memory too slow for the task that is being started. The process tries to allocate memory but fails => exception.

In the second scenario the GC frees memory fast enough so that the task can get whatever it is asking for.

Is that correct? If so: Is there a possibility to get scenario 1 working as well? Maybe a way to tell the GC to free memory earlier than usually.
(I do not want to add a timer to each process that executes GC.Collect every once in a while :-))

I hope you guys understood my problem. If not, please ask.

Thank you very much,
Kevin

Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 235
#2: Oct 7 '09

re: Garbage Collector - what am I doing wrong (Windows CE)


Look further into the GC class, I believe there's settings you can use to make it be a little more on top of cleaning things out. As I understand it, it's not advisable to call GC.Collect() explicitely, but if you're going to do it, do it when you actually free up memory (not on a timer).

For example, lets say you have a method that allocates a large block of data to do it's processing, at the end of the method set the reference to the array to null, then call GC.Collect().

Either way, I'd explore what the GC class has to offer (if anything) before you resort to that :)
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#3: Oct 7 '09

re: Garbage Collector - what am I doing wrong (Windows CE)


"The garbage collector tracks and reclaims objects allocated in managed memory. Periodically, the garbage collector performs garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory." MSDN

GC is activated when the memory is low. Maybe process A requires more resources and memory therfoere throws exception. Process B also causes the GC to collect but doesn't throw error as it (maybe) requires less resources.
I would suggest you insert
Expand|Select|Wrap|Line Numbers
  1. GC.Collect();
  2. GC.WaitForPendingFinalizers();
  3.  
in your code, to free up memory and run garbage collection forcefully. However doing this you will suspend all thread till GC completes.

Also with .NET Framework 3.5 you can use
GC.WaitForFullGCComplete method, to check GC status.
Reply


Similar C# / C Sharp bytes