5
  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.

Environment is .Net C#

4
  • 1
    First, at the very least mention the environment (if any) your question is about. At least .Net and Java have a OOM exception. Secondly, 2) this is not even a proper question. What are you asking? Voting to close. Commented Oct 26, 2009 at 9:25
  • 1) Envoirment is .Net C# 2) why this ques is not proper Commented Oct 26, 2009 at 9:49
  • Well, question 1) a question, but so general that it does not really have a meaningful answer. 2) is not a question, and 3) should at least get a question mark. Please try and use proper punctuation and spelling, and most importantly and give some context. Commented Oct 26, 2009 at 10:35
  • Try reading catb.org/~esr/faqs/smart-questions.html , while a bit lengthy, it explains quite well how to ask meaningful questions. Worth a read. Commented Oct 26, 2009 at 10:37

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

how much memory is available to app how can i check that ?
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.
2

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.

Comments

1
  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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.