Maximum Memory AllocationI've encountered an interesting memory allocation limit in a C# project, and I'm hoping to find an explanation.  When I started to experience System.OutOfMemoryException exceptions being thrown in my application, I was sure that I did not have enough memory allocated to hit the 2 GB user space memory limit for 32-bit processes.  So I created a simple test application to see how much memory I could allocate, and to my surprise I started to get exceptions after only 1.4 GB (internally counted), with allocating 4,096 bytes at a time.  If I allocate 1 MB at a time, I can get up to 1.8 GB, but this is still less than the theoretical 2 GB I should be able to allocate.  My first thought is CLR overhead, but that's 200 - 600 MB of overhead.  Even with the 4096 allocation block size, that's only around 350,000 allocations to reach the 1.4 GB limit that I am experiencing.  I highly doubt it takes 600 MB to store a List&lt;&gt; of 350,000 byte arrays.  My only other thought is that the CLR is keeping some memory reserved for various purposes, but again, 600 MB is a lot of memory to prevent allocation for.<br><br>Does anyone here know if this is CLR overhead, a limitation in the way .NET handles memory in a 32-bit environment, or some settings I'm missing somewhere?  Being limited to 1.4 - 1.8 GB implies a 10 - 30% overhead with the .NET memory manager, and that just seems awfully high!<br><br>As a sanity check, I ran the same test with native C++ and my allocation limit was 2 GB, as expected.<br><br>The machine I'm using has 4 GB of RAM on Vista 64, so the physical memory is available.<br>© 2009 Microsoft Corporation. All rights reserved.Sat, 16 Aug 2008 23:43:26 Zb4839085-772b-4925-a0f6-2fe21096c376http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#b4839085-772b-4925-a0f6-2fe21096c376http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#b4839085-772b-4925-a0f6-2fe21096c376ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory AllocationI've encountered an interesting memory allocation limit in a C# project, and I'm hoping to find an explanation.  When I started to experience System.OutOfMemoryException exceptions being thrown in my application, I was sure that I did not have enough memory allocated to hit the 2 GB user space memory limit for 32-bit processes.  So I created a simple test application to see how much memory I could allocate, and to my surprise I started to get exceptions after only 1.4 GB (internally counted), with allocating 4,096 bytes at a time.  If I allocate 1 MB at a time, I can get up to 1.8 GB, but this is still less than the theoretical 2 GB I should be able to allocate.  My first thought is CLR overhead, but that's 200 - 600 MB of overhead.  Even with the 4096 allocation block size, that's only around 350,000 allocations to reach the 1.4 GB limit that I am experiencing.  I highly doubt it takes 600 MB to store a List&lt;&gt; of 350,000 byte arrays.  My only other thought is that the CLR is keeping some memory reserved for various purposes, but again, 600 MB is a lot of memory to prevent allocation for.<br><br>Does anyone here know if this is CLR overhead, a limitation in the way .NET handles memory in a 32-bit environment, or some settings I'm missing somewhere?  Being limited to 1.4 - 1.8 GB implies a 10 - 30% overhead with the .NET memory manager, and that just seems awfully high!<br><br>As a sanity check, I ran the same test with native C++ and my allocation limit was 2 GB, as expected.<br><br>The machine I'm using has 4 GB of RAM on Vista 64, so the physical memory is available.<br>Mon, 17 Mar 2008 02:09:08 Z2008-03-17T02:09:08Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c8d465b0-8478-4d4e-afd1-57e8f751c88dhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c8d465b0-8478-4d4e-afd1-57e8f751c88dFWiehttp://social.msdn.microsoft.com/Profile/en-US/?user=FWieMaximum Memory Allocation<p align=left><font face=Arial size=2>I think you're encountering two limitations of the managed heap.</font></p> <p align=left> </p> <p align=left>Old school memory managers implement a flat memory model where every malloc/new is allocated on the lowest memory address where the requested size fits.</p> <p align=left>- allocate 10 bytes --&gt; address 0</p> <p align=left>- allocate 20 bytes --&gt; address 10</p> <p align=left>- free the first 10 bytes --&gt;heap fragmentation, only 10 bytes available at p=0.</p> <p align=left>This gets gradually worse as more malloc/free cycles are completed.</p> <p align=left> </p> <p align=left>Modern memory managers are more sophisticated. They would for example allocate a separate array for all small elements. This prevents the situation where an Int32 may cut the max allocation size in half. But it does introduce an additional limitation of the max size, i.e. the size of the small-elements-array is deducted from the total heap.</p> <p align=left> </p> <p align=left>Second, .NET makes a class from about everything. This increases the memory allocation for each object.</p> <p align=left>For example the Int32 class needs only 4 bytes to store the value of the integer. But it also contains a bunch of function pointers like Int32::CompareTo(), Int32::ToString() etc.</p> <p align=left>This may explain why the max total allocation depends on the size of each object. Larger objects generally have lower overhead, at least relatively.</p> <p align=left> </p> <p align=left> </p>Mon, 17 Mar 2008 12:48:45 Z2008-03-17T12:48:45Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#225d54fb-0ea6-4ece-9119-4a941c845cd1http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#225d54fb-0ea6-4ece-9119-4a941c845cd1ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory AllocationI understand the overhead of managed objects, it just seems excessive for this test case.<br><br>When allocating 4,096 byte chunks, one test run is able to allocate 1,453,608,960 bytes, or 354,885 total allocations.  That gives us approximately 546,391,040 bytes of additional memory until we hit the 2 GB limit.  Assuming the size of all code stored in user memory is negligible compared to the 546 MB of additional memory available, that gives us about 1,540 bytes of overhead per byte array allocated (plus one List&lt;&gt; instance), which is roughly 38% overhead.  <br><br>That's 1.5 KB for GC book-keeping and managed object overhead, per object.  Is there really that much overhead?<br>Mon, 17 Mar 2008 15:58:26 Z2008-03-17T15:58:26Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#564f5f4e-95d2-4e6f-9ef8-519add82798bhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#564f5f4e-95d2-4e6f-9ef8-519add82798bJ Hallamhttp://social.msdn.microsoft.com/Profile/en-US/?user=J%20HallamMaximum Memory Allocation<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>FWie wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p align=left>Second, .NET makes a class from about everything. This increases the memory allocation for each object.</p> <p align=left>For example the Int32 class needs only 4 bytes to store the value of the integer. But it also contains a bunch of function pointers like Int32::CompareTo(), Int32::ToString() etc.</p> <p align=left>This may explain why the max total allocation depends on the size of each object. Larger objects generally have lower overhead, at least relatively.</p></td></tr></tbody></table></td></tr></tbody></table></div> <p></p><br> <p align=left>Structs and Classes don't contain pointers to their methods.</p> <p align=left>The methods are, at the assembly level, completely separate functions which inherit a &quot;this&quot; pointer via a register.</p><br> <p style="text-align:left"></p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div><br> <p>class x {<br>  public int value = 0</p> <p>  public int GetValue() { return value; }</p> <p>}<br></p></div></div> <p></p> <p style="text-align:left"><br></p> <p style="text-align:left"></p> <p align=left>An instance of x is only 4 bytes.<br></p>Mon, 17 Mar 2008 16:35:19 Z2008-03-17T16:35:19Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c1660488-0c68-4ced-827a-03c8dff37280http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c1660488-0c68-4ced-827a-03c8dff37280ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory AllocationThere's still going to be a vtable for the object, and some sort of GC handle for book-keeping, right?<br>Mon, 17 Mar 2008 17:01:52 Z2008-03-17T17:01:52Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#3bdd4fdd-d3ef-4278-b40b-4f7af4b00e73http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#3bdd4fdd-d3ef-4278-b40b-4f7af4b00e73CommonGenius.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=CommonGenius.comMaximum Memory Allocation<p align=left>Trying to gauge .NET memory usage by repeated allocation is a pointless exercise. The CLR manipulates memory internally to such a degree that you cannot hope to get results that you can interpret. If you want to find out how much memory your application is using, run the <a title="http://msdn2.microsoft.com/en-us/library/ms979205.aspx" href="http://msdn2.microsoft.com/en-us/library/ms979205.aspx">CLR profiler</a> (<a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;DisplayLang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;DisplayLang=en">download</a>).</p> <p align=left> </p> <p align=left>That said:</p> <p align=left> </p> <p align=left> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>FWie wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p></p> <p align=left>Int32 class needs only 4 bytes to store the value of the integer. But it also contains a bunch of function pointers like Int32::CompareTo(), Int32::ToString() etc.</p> <p align=left></p></td></tr></tbody></table></td></tr></tbody></table></div> <p></p> <p align=left> </p> <p align=left>Int32 is a struct (value type) not a class (reference type). Value types only ever take up as much space as they need to store their data; in the case of an Int32, that means 4 bytes. However:</p> <p align=left> </p> <p align=left> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>J Hallam wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"><br>An instance of x is only 4 bytes.<br></td></tr></tbody></table></td></tr></tbody></table></div><br> <p></p> <p>Actually, an instance of x is 12 bytes on a 32-bit system. This is because every instance of a reference type includes a 4-byte sync block and a 4 byte reference to the method table for the type of the instance. The existence of the sync block has a long and ignoble history which I won't go into here; google it if you are interested. The pointer to the method table is necessary because reference types are always declared on the heap, and only a memory pointer is returned to the context that made the allocation. In order for ,NET's static type system to be possible, each instance must keep track of its own type. Value types, on the other hand, are never allocated on the heap in isolation; they are either allocated on the stack, in which case the current method context can be used to determine the type, or they are allocated on the heap as part of a reference type allocation, in which the case type of the reference can be used to to determine the type.</p> <p align=left> </p> <p align=left>Here is a quick reference for memory layout in .NET. There are many others available online.</p> <p align=left><a title="http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout" href="http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout">http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout</a></p>Mon, 17 Mar 2008 19:02:23 Z2008-03-17T19:02:23Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e13785c3-007c-40db-ae26-cc31b17c38a0http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e13785c3-007c-40db-ae26-cc31b17c38a0ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory Allocation<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>CommonGenius.com wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p align=left>Trying to gauge .NET memory usage by repeated allocation is a pointless exercise. The CLR manipulates memory internally to such a degree that you cannot hope to get results that you can interpret. If you want to find out how much memory your application is using, run the <a title="http://msdn2.microsoft.com/en-us/library/ms979205.aspx" href="http://msdn2.microsoft.com/en-us/library/ms979205.aspx">CLR profiler</a> (<a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;DisplayLang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;DisplayLang=en">download</a>).</p> <p align=left></p></td></tr></tbody></table></td></tr></tbody></table></div> <p></p> <p align=left><br></p> <p align=left>The point is not to see how much memory I am allocating, the point is to see how much memory I <span style="font-style:italic">can</span> allocate for use in my program.  I'm trying to find out why my allocation limit is as low as 1.4 GB.  Due to third-party library restrictions, I am unable to just switch to the 64-bit CLR.<br></p><br><br> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>CommonGenius.com wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"><br> <p align=left>Here is a quick reference for memory layout in .NET. There are many others available online.</p> <p align=left><a title="http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout" href="http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout">http://en.csharp-online.net/Common_Type_System%E2%80%94Memory_Layout</a></p></td></tr></tbody></table></td></tr></tbody></table></div><br><br>Thanks for the link.<br><br>I understand the mechanisms involved in managed allocations, but I cannot see where this extra overhead is coming from.  There should only be 8-12 bytes of overhead per object, as far as I know.<br><br>Mon, 17 Mar 2008 19:21:14 Z2008-03-17T19:21:14Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c2c93703-0d3f-47fa-ae46-f0d2c141f777http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c2c93703-0d3f-47fa-ae46-f0d2c141f777CommonGenius.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=CommonGenius.comMaximum Memory Allocation<p align=left><font face=Arial size=2>I understand. My point is that your method of determining how much you <i>can</i> allocate relies on being able to accurately determine how much you <i>are</i> allocating during your test. And that's just not realistic. The only way you could do that is by allocating in one single large chunk, and i don't think that will give you an usable results.</font></p>Mon, 17 Mar 2008 19:26:37 Z2008-03-17T19:26:37Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#3bfcdac5-6403-4a95-b3e0-7508d697d874http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#3bfcdac5-6403-4a95-b3e0-7508d697d874ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory AllocationThat's true if I was concerned with how much physical/virtual memory the process uses.  Instead, I am concerned with the amount of usable memory I am able to allocate.  For instance:<br><br> <div style="text-align:left"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>byte[] arr = new byte[4096];</p></div></div><br></div><br>This gives me 4,096 bytes of memory that I can use for processing, no more and no less.  How much <span style="font-style:italic">actual</span> memory is allocated to hold the array, object header, and whatever else is needed is some arbitrary number of bytes greater than 4,096.  My concern is with how much memory I am able to allocate for my internal processing within a C# program, and why my allocations start to fail after 1.4 GB.<br>Mon, 17 Mar 2008 19:46:22 Z2008-03-17T19:46:22Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e0c8e7ed-efe0-4b8e-b12a-e8025449aeachttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e0c8e7ed-efe0-4b8e-b12a-e8025449aeacCommonGenius.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=CommonGenius.comMaximum Memory Allocation<p align=left><font face=Arial size=2>But the two are related. How much memory you can allocate for use in your program depends both on how much physical/virtual memory exists on the system, and how much overhead is being used for the CLR and the type system. I don't see how you expect to separate the concerns.</font></p> <p align=left> </p> <p align=left>In your original application, did you perform any memory profiling when you started to get OOMs to see how much memory your application was using?</p>Mon, 17 Mar 2008 20:10:18 Z2008-03-17T20:10:18Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#ced27f4d-524f-4ad4-a792-f73d8581b456http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#ced27f4d-524f-4ad4-a792-f73d8581b456ShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory AllocationRight, I was trying to say that the method I was using to measure memory allocation is meaningful for my purposes.  I'm not just using some arbitrary method to measure how much total process memory I am using.  Since my allocations are limited to around 1.4 GB for the arrays I'm using, there must be some significant overhead somewhere.  The whole point of my question is to determine how much overhead exists within .NET that would limit the amount of memory I am able to use in my program for internal uses.  Clearly there is an intimate relationship there.<br><br> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>CommonGenius.com wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"><span></span><br><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">In your original application, did you perform any memory profiling when you started to get OOMs to see how much memory your application was using?</span></span><br></td></tr></tbody></table></td></tr></tbody></table></div><br><br>That's the crux of the whole problem.  No matter how I profiled (CLR Profiler, even Task Manager), I was significantly under 2 GB of total memory usage.<br>Mon, 17 Mar 2008 20:33:59 Z2008-03-17T20:33:59Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#02810125-d0d2-48e3-a0fa-866b688ca7a9http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#02810125-d0d2-48e3-a0fa-866b688ca7a9CommonGenius.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=CommonGenius.comMaximum Memory Allocation<p align=left>But total memory being used is not the only limiting factor when talking about memory allocations. Particularly for long running applications or applications which allocate large chunks of memory, memory fragmentation, especially in the large object heap, can cause you to get OOM even when you have memory available.</p> <p align=left> </p> <p align=left>Task manager is completely useless when profiling .NET applications; don't even bother opening it. The CLR profiler should be accurate, but its results can be complicated and hard to intrepret. Did you try using the .NET performance counters?</p>Mon, 17 Mar 2008 20:43:06 Z2008-03-17T20:43:06Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#df647cc3-3062-4fb1-81ad-f1f00935dd0chttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#df647cc3-3062-4fb1-81ad-f1f00935dd0cShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory Allocation<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>CommonGenius.com wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p align=left>But total memory being used is not the only limiting factor when talking about memory allocations. Particularly for long running applications or applications which allocate large chunks of memory, memory fragmentation, especially in the large object heap, can cause you to get OOM even when you have memory available.</p> <p align=left></p></td></tr></tbody></table></td></tr></tbody></table></div> <p></p><br> <p align=left>I can see fragmentation being an issue with my original code, but my test code is designed to minimize its effects, yet I get the same results.<br></p> <p align=left><br></p> <p align=left>I was hoping to avoid memory fragmentation as much as possible by doing sequential byte[] allocations with a List&lt;&gt; instance that is allocated with a sufficient initial capacity.  I'm not discounting that memory fragmentation could be the issue.  What strikes me as odd though, is if it is memory fragmentation, why do I get so much more fragmentation in .NET, where the GC is supposed to actively minimize fragmentation, than I do with C++ on a native heap, with the allocation patterns being equal?  Is the per-object book-keeping information held in a different location in the heap than the byte[] array, causing fragmentation?<br></p><br> <p align=left>Let's assume for a second there is 12 bytes of total overhead per .NET-object, including the object header, and any GC book-keeping information.  That's only 4.3 MB of overhead for all of my allocations.  That still leaves over 500 MB.  If memory fragmentation is the cause, then I'm losing 25% of my available memory, and that's with purposely writing allocations to minimize fragmentation.  The same allocation pattern in C++ gives me practically zero fragmentation, that's what concerns me.</p><br> <p align=left>In my test program, the allocations are small and easily compactible.  My gut feeling is that .NET should be able to do better than 75% efficiency in this case, and I want to know why it's not.<br></p><br> <p align=left> <div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>CommonGenius.com wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p></p> <p align=left>Task manager is completely useless when profiling .NET applications; don't even bother opening it. The CLR profiler should be accurate, but its results can be complicated and hard to intrepret. Did you try using the .NET performance counters?</p></td></tr></tbody></table></td></tr></tbody></table></div><br><br>Definitely, Task Manager doesn't tell you much, I just threw that in there to show I tried using every possible tool I could find.<br><br>Thanks for your help/suggestions, by the way!<br>Mon, 17 Mar 2008 21:27:11 Z2008-03-17T21:27:11Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#1ca70515-972d-491e-bc23-174211bf73a4http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#1ca70515-972d-491e-bc23-174211bf73a4Feng Chenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Feng%20ChenMaximum Memory Allocation<font size=2><span style="font-family:Verdana">We'd appreciate if you can provide a sample project to let's reproduce this issue and investigate in house.<br></span></font>Tue, 18 Mar 2008 07:49:55 Z2008-03-18T07:49:55Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#903ac20b-509e-475f-8be6-d6c267161acdhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#903ac20b-509e-475f-8be6-d6c267161acdShawMishrakhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShawMishrakMaximum Memory Allocation<p align=left><font face=Arial size=2>Sure thing.  I packaged up the project sources and uploaded them <a title="http://www.cse.ohio-state.edu/~holewins/AllocTest.zip" href="http://www.cse.ohio-state.edu/~holewins/AllocTest.zip">here</a>.</font></p> <p align=left> </p> <p>The .suo options file is not included, so the project build target should be manually set to x86 Release before building.</p> <p align=left> </p> <p align=left>To run the test, you can run it on the command-line without arguments, in which case it will attempt byte array and HGlobal allocations of various sizes and print the results to stdout.  Or, you can pass arguments of the form:</p> <p align=left> </p> <p align=left>&quot;AllocTest byte &lt;array size&gt;&quot;  or  &quot;AllocTest hglobal &lt;chunk size&gt;&quot;</p> <p align=left> </p> <p align=left>The first form will attempt to allocate byte arrays of the specified size, and the second will attempt to allocate HGlobal chunks (Marshal.AllocHGlobal/Marshal.FreeHGlobal) of the specified size.</p> <p align=left> </p> <p align=left>The HGlobal allocations are much more consistant and I am able to allocate significantly more memory using that approach, the problem is that this does not help when working with managed objects, as far as I know.</p> <p align=left> </p> <p align=left>Thanks for the taking the time to look into this.</p>Tue, 18 Mar 2008 16:51:06 Z2008-03-18T16:51:06Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#91a9b6af-68b4-48df-a54d-806f05bae651http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#91a9b6af-68b4-48df-a54d-806f05bae651AbdElRaheimhttp://social.msdn.microsoft.com/Profile/en-US/?user=AbdElRaheimMaximum Memory Allocation<p align=left><font face=Arial size=2></font> </p> <p>If you are going to be using that much ram you might want to investigate using the <font size=2>System.Runtime.MemoryFailPoint class.  The MemoryFailPoint class throws a InsufficientMemoryException which is recoverable while a OutOfMemoryException is most of the time not recoverable.</font></p> <p align=left> </p><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>try</p></font></font><font size=2> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>using</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>MemoryFailPoint</font></font><font size=2> _FailPoint = </font><font color="#0000ff" size=2><font color="#0000ff" size=2>new</font></font><font size=2> </font><font color="#2b91af" size=2><font color="#2b91af" size=2>MemoryFailPoint</font></font><font size=2>(1500))</p> <p>{</p> <p></font><font color="#008000" size=2><font color="#008000" size=2>// Big memory usage here.</p></font></font><font size=2> <p>}</p></blockquote> <p>}</p> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>catch</font></font><font size=2>(</font><font color="#2b91af" size=2><font color="#2b91af" size=2>InsufficientMemoryException</font></font><font size=2>)</p> <p>{</p> <blockquote dir=ltr style="margin-right:0px"> <p></font><font color="#008000" size=2><font color="#008000" size=2>// Handle error here</p></blockquote></font></font><font size=2> <p>}</p></font>Wed, 19 Mar 2008 22:40:13 Z2008-03-19T22:40:13Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c2fcde32-3be6-493d-a608-736407c8641dhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#c2fcde32-3be6-493d-a608-736407c8641dsocatoahttp://social.msdn.microsoft.com/Profile/en-US/?user=socatoaMaximum Memory AllocationHi,<br><br>Did you get a solution for this problem? I am encountering a similar issue, my remoted server will not allocate more than 1 GB of memory.<br><br>Thanks,<br>Catalina<br> Tue, 10 Jun 2008 23:46:17 Z2008-06-10T23:46:17Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e4386296-d6df-4ce4-81e1-e25da2cc44d9http://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#e4386296-d6df-4ce4-81e1-e25da2cc44d9willy denoyettehttp://social.msdn.microsoft.com/Profile/en-US/?user=willy%20denoyetteMaximum Memory Allocation<p><font style="background-color:#ecedeb">A similar issue doesn't mean it's the same, please start a new thread and give us some more details about the issues you have.<br><br>Willy.<br>C# MVP<br></font></p>Wed, 11 Jun 2008 08:20:35 Z2008-06-11T08:20:35Zhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#173cf1f6-0006-4edc-a323-74093887b35bhttp://social.msdn.microsoft.com/Forums/en-US/clr/thread/b4839085-772b-4925-a0f6-2fe21096c376#173cf1f6-0006-4edc-a323-74093887b35byoyocestmoihttp://social.msdn.microsoft.com/Profile/en-US/?user=yoyocestmoiMaximum Memory AllocationHello,<br><br>Are there any explanations for ShawMishrak's problem?<br><br>On our server, we are not able to allocate more than 1.6GB.<br><br>Of course, I know the situation is different, but I guess that ShawMishrak's situation is generic enough to help a lot of us understand what's involved behind the scenes, and why we can't use all the memory we would have believed was available to us.<br><br>Thanks,<br><br>Lionel.<br> Sat, 16 Aug 2008 23:43:26 Z2008-08-16T23:43:26Z