views:

588

answers:

5

Hi,

1) What are the possible reasons of OutofMemory exception.

2) Memory allocations should be handled by GC.

3) How much memory is allocated/Available to normal .Net/C# application

In our application it comes at different places like Stream.ReadToEnd() and DataTable.WriteXml(Memory stream) function.

*) Envoirment is .Net C#

+5  A: 

The OutOfMemory exception happens whenever a call to any of the following MSIL instructions fails

  1. newobj
  2. newarr
  3. box

Which basically the operations that allocate new memory in the heap, in your case the Stream.ReadToEnd apparently allocates array of bytes internally to load the stream in memory, so if the file in big enough to break the process it will throw this exception.

bashmohandes
+1  A: 
  1. Lets say you have max of 10MB memory to use in your app. You create a new List and add to it Object instances. Let's now say that each object instance "weight" 1MB. So, the first 10 Instances will be added with no problems but the 11th instance will throw the OutOfMemoryException as after the first 10 instances you used all the allocated memory (10MB).

  2. The garbage collector looks for "Garbage", Instances which are not going to used - Which CANT be used as no other instances are pointing to them. In case for example have an instance member of type List with containing instances, the GC will not collect the List nor it's instances. Keep on adding instances to the List might and up with OutOfMEmory Exception.

Use the following vm arguments if you want/need to increase the memory used by your app: Java youAppName -Xms128m -Xmx512m

Rod
+1  A: 

Either you are using more memory than the app has available to it. In this case you will need to work out how to make your memory usage more efficient. Using Files / Database to store data you arent immediately using may be necessary..

Or, you have a memory leak. In which case you need to look at removing references to memory when you are no longer using them so the GC can free up the memory.

If you are using C# or .Net you can use the CLR Profiler to analyse your memory to see how it is being used. CLR Profiler

Mongus Pong
how much memory is available to app how can i check that ?
Buzz
On 32-bit Windows (pre-Win7), there's a hard limit of 2GB on Virtual Memory (3GB with a certain switch). 64-bit Windows can support 8TB (7TB on Itanic). The actual memory you can access before an allocation failure can vary.
Robert Fraser
+2  A: 

Either your application has used up the memory available to it or you have a problem with heap fragmentation.

In the first case you have created enough objects to take up all of the memory and you still have reference to them so the garbage collector cannot clean them up.

In the second case, heap fragmentation, you are trying to create an object that is bigger than the largest contiguous chunk of memory in the heap. This is more rare but certainly happens in some cases. The normal heap will get compacted during gc runs but the large object heap will not.

There is a good article on MSDN about the large object heap.

Edit: I remembered another way to get out of memory. You can try and create an object that is larger than 2GB in size. That is the maximum object size in .NET even on 64-bit.

Mike Two
A: 

This is a follow up question, in C++, does the OutOfMemory Exception get thrown in all environments or is it environment specific? If so, does it depend on the compiler and/or the OS? I have seen this exception thrown in a Solaris environment (as a result of the app attempting to process large amounts of data). The compiler we used for this app is homegrown.

hackerforlife
Plain C++ does not throw an OutOfMemoryException. C++/CLI (Managed C++) will like any other .NET language (C#, VB.Net, etc.). If a malloc/calloc/realloc/new fails in C++, the resultant pointer will be null.
Robert Fraser