OutOfMemory problems in WinCE 5.0

G

Guest

Hello

I am struggeling with a 'OutOfMemoryException' and would very much like some
help to find out why this is happening.

In the GC class there is a method called ‘GetTotalMemory’.
(http://msdn2.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx) .
According to the documentation this method
“Retrieves the number of bytes currently thought to be allocatedâ€
My understanding is that this method returns the number of bytes that the
process is currently using.

Further, my understanding is that in Windows CE 5.0 each process is limited
to a maximum of 32MB of memory.

My application has approximately 4MB of embedded resources (all of which are
images) that are inclued in the .exe file.
When the application starts I assume that these files are loaded into RAM.
When calling the ‘GetTotalMemory’ just after the main Window or Form has
been displayed, this method returns approximately 4MB, so this seems to
correspond well.

In my program I have added the following piece of code:

catch (OutOfMemoryException OOM)
{
MessageBox.Show("OOM. TotalMemory usage [bytes] =" +
GC.GetTotalMemory(false).ToString());
}


What puzzels me is that the OutOfMemoryException is thrown when
‘GC.GetTotalMemory(false)’ returns between 3 MB and 4 MB, which
is far below the 32MB limit.
The target device has 256 MB of RAM, so how can this be?
 
G

Guest

Further, my understanding is that in Windows CE 5.0 each process is
limited
to a maximum of 32MB of memory.

Your understaning is not 100% correct. It has a maximum of 32MB of address
space. A process can use and access more RAM if it's in shared memory. The
GCHeap is in the 32MB space, but your assemblies themselves are not.
My application has approximately 4MB of embedded resources (all of which
are
images) that are inclued in the .exe file.
When the application starts I assume that these files are loaded into RAM.

This is not necessarily the case. Your assembly is loaded as a memory
mapped file out of shared memory space. It takes up little space in the
32MB slot (the EE need some info on the assembly which it holds in the 32-MB
slot). When you load the image itself into a Bitmap object, it then gets a
GCHeap allocation and that will come out of the 32MB slot (so yes, you have
2 copies). All of these can be (and likey are) in RAM - the app, the
assembly and the Bitmap object.
What puzzels me is that the OutOfMemoryException is thrown when
'GC.GetTotalMemory(false)' returns between 3 MB and 4 MB, which
is far below the 32MB limit.

As you said, the docs state that GetTotalMemory "Retrieves the number of
bytes currently thought to be allocated." This is simply how much memory
the GC itself thinks it has allocated. The GC, however, is not the only
thing in your process slot - there's a lot more to it than that. Your app
itself is. Your thread stacks are. The CLR may be (depends on some
factors). DLLs that other apps have loaded may be. Fragmentation also
means that you can't always get every last bit of that memory in use, and
large allocations (like with Bitmaps) compound that problem as they need
large contiguous blocks.
The target device has 256 MB of RAM, so how can this be?

In CE 5.0 and earlier, the device memory really has little to do with it.
You could have 20GB of RAM and your process space still only has 32MB of
addressable memory and you would still see the error. This is one reason
why CE 6.0's kernel was completely rearchitected.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
 
G

Guest

Thank you very much for your answer Chris.

For the time being my hardware does not support Win CE 6.0, so I need to
find a way to make the application run properly on Win CE 5.0.

So what should I do to make sure that the application does not use more
memory than absolutely necessary?

I have read your blog on "Bitmaps in the Compact Framework" dated 22 August
2006, and my understanding is that explicit calls to Dispose() should be made
when using the width/height constructor.

But what else should I do?
Does it make any sense to place the images in a .dll instead of embedding
the .exe file?
 
G

Guest

Does it make any sense to place the images in a .dll instead of embedding
the .exe file?

Makes no difference. The best way to use the least amount of memory is to
load the images only when needed. Using lower color depth when possible is
also a good idea.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
 

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