What are the possible reasons of
OutofMemory
exception.Memory allocations should be handled by GC.
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#
The OutOfMemory exception happens whenever a call to any of the following MSIL instructions fails
- newobj
- newarr
- 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.
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
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.
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).
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
精彩评论