OutOfMemoryException
(1)
GC
(1)
GetTotalMemory
(1)
Dispose
(1)
Collect
(1)
Remove
(1)

How can "OutOfMemoryException" best be avoided?

Asked By G
24-Jan-07 08:54 AM
Hello

I have a Windows Form application running on a PC with Windows CE 5.0. To
navigate back and forth in the program, I typically open a new
Windows Form and close the preceding one.
If the Forms are opened and closed rapidly (which would be the case if the
users presses the "NextButton" and "BackButton" very fast), an
OutOfMemoryException is thrown.
How can this best be avoided?


I have also noted that an OutOfMemoryException can be provoked by quickly
adding and removing Controls  to a Windows Form. This I have tested with a
simple application having a button which adds and removes a PictureBox when
pressed. At the same time I log the available memory using the
GetTotalMemory() function in the GC class: "GC.GetTotalMemory(false)".
What puzzels me is that the OutOfMemoryException is thrown even if
GC.GetTotalMemory() reports that there is more than 150MB of RAM available.
What could be the reason for this?

GT

Total memory does not reflect space available for the GC heap (which can never

24-Jan-07 08:59 AM
Total memory does not reflect space available for the GC heap (which can
never be more than 32MB in theory, probably half that or so in reality).

The issue is probably the Bitmap backing that PitureBox.  Trap the exception
and call GC.Collect and see if it makes a difference  Here's a little
background reading:

http://blog.opennetcf.org/ctacke/PermaLink,guid,987041fc-2e13-4bab-930a-f79021225b74.aspx


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

.

Thank you for the reply.

Asked By G
24-Jan-07 10:22 AM
Thank you for the reply. That was very educative, but I still have some more
questions:

MSDN states the following about the OutOfMemoryException Class:
the execution of a program"

But what kind of memory (physical or virtual) is reffered to here?
Is it possible to get the OutOfMemoryException even if you have a
considerable amout of free physical memory/ RAM ?

I understand that Windows CE 6.0 supports much larger processes.
Would running the application/code on the same target but with Win CE 6.0
instead of 5.0 make a difference?

Hi,Try to call yourBipmat.Dispose() when you stop using this.

Asked By Fabien
24-Jan-07 02:30 PM
Hi,

Try to call yourBipmat.Dispose() when you stop using this.


BR


Fabien Decret
Windows Embedded Consultant

ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/

ore
ption
. To
f the
ckly
ith a
des messages pr=E9c=E9dents -
It actually is thrown when al attempt to make an allocation fails.
24-Jan-07 03:31 PM
It actually is thrown when al attempt to make an allocation fails.  Due to
CE's memory architecture the nature of that failure will change depending on
the type of allocation (and what memory region its coming from).  It's very
possible to see a failure when the device has plenty of physical RAM left if
it is out of virtual space.


If the error is because you're out of virtual space, then yes, 6.0 would
make a difference.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
How can "OutOfMemoryException" best be avoided?
Asked By Marc Ambrosius
25-Jan-07 05:43 AM
If you make use of Controls.Add() and Controls.Remove(), your control leaves
a reference in a static list and therefor the memory cannot be released by
GC. Please read the following blog entry to get for information:
http://blog.opennetcf.org/afeinman/CommentView,guid,9999cdb7-507c-4ca3-ad7d-8977079e1de4.aspx

I am not sure if everything is still true for .NETCF 2.0, but it is a good
idea anyway to dispose Controls and Forms.

Marc
Not the case in CF 2.0.
25-Jan-07 05:10 PM
Not the case in CF 2.0.  I'm certain it's the Bitmap behind the PictureBox
causing the issue and my blog explains what's happening.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--
Post Question To EggHeadCafe