.NET Framework - 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
OutOfMemoryException
(1)
GC
(1)
GetTotalMemory
(1)
Dispose
(1)
Collect
(1)
Remove
(1)
 
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
--

.
  G replied...
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?
  Fabien replied...
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 -
 
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
--
  Marc Ambrosius replied...
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
 
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
--
Create New Account
help
garbage collector: la syntaxe ? .NET Framework Bonjour, J'ai besoin de forcer le garbage (classe GC) dans mon prog d'affichage de photos. Je ne suis pas sur d'avoir la express. Un timer provoque l'affichage de photos, au bout de 26 photos j'ai OutOfMemoryException. Jai vu que le garbage ne se fait pas. Je voudrais forcer le garbage. J'essaie GC.collect() sans succ?s. Quelque chose ressemblant ?: If GC.GetTotalMemory(False)<50000 GC.Collect() End If - - Jacques dit Jacquouille VB.NET - French Discussions OutOfMemoryException (1) Vista (1) GetTotalMemory (1) Image.FromFile (1) FromFile (1) Image (1) Site (1) Collect (1) Bonjour, Il est
to the instance). So, the instance becomes immediately available for the garbage collector. Of course, GC does not collect at once, but in our example GC never collects any UserControl instance. This has been approved also by Microsoft's Remote Performance Monitor. Instead, the amount of used memory increases and eventually we get an OutOfMemoryException (after about 20, 000 allocations). GC: 680 GC: 1163048 GC: 2323048 GC: 3483048 GC: 4643048 . . . OutOfMemory Exception Testing the code on Windows with .NET 2.0 we get the
OutOfMemory problems in WinCE 5.0 .NET Framework 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 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
System.OutOfMemoryException during serialization .NET Framework Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream> , <Obj> ) method. The type of <stream> is IO.MemoryStream = = = = = Exception: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 214 Bytes Allocated memory 1, 028, 914, 416 Bytes [to get the memory I called GC.GetTotalMemory(False)] Please note my system is a x64 workstation with 6 GB RAM (and Windows System.IO.BinaryWriter.Write (1) System.IO.MemoryStream.Write (1) Windows Server 2003 R2 (1) OutOfMemoryException (1) Visual Studio 2005 (1) BinaryWriter.WriteMember (1) BinaryWriter.WriteValue (1) there is currently a otherwise you would get 2 indentical objects, instead of an old one needing to be GC'd, and a new larger one. Nope, sorry. Robert = Piggy ??? Well i am not active