Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get unused memory back from the large object heap LOH from multiple managed apps?
    primarykey
    data
    text
    <p>While talking to a colleague about a particular group of apps using up nearly 1.5G memory on startup... he pointed me to a very good link on <a href="http://msdn.microsoft.com/en-us/library/ms954594.aspx" rel="nofollow noreferrer">.NET production debugging</a></p> <p>The part that has me puzzled is ...</p> <blockquote> <p>For example, if you allocate 1 MB of memory to a single block, the large object heap expands to 1 MB in size. When you free this object, the large object heap does not decommit the virtual memory, so the heap stays at 1 MB in size. If you allocate another 500-KB block later, the new block is allocated within the 1 MB block of memory belonging to the large object heap. During the process lifetime, the large object heap always grows to hold all the large block allocations currently referenced, but never shrinks when objects are released, even if a garbage collection occurs. Figure 2.4 on the next page shows an example of a large object heap.</p> </blockquote> <p>Now let's say we have a fictional app that creates a flurry of large objects ( > 85KB), so the large object heap grows lets say to 200 Meg. Now lets say we have 10 such app instances running.. so that 2000 Megs allocated. Now is this memory never given back to the OS until the process shuts down... (is what I understood)</p> <p>Are there any gaps in my understanding? How do we get back unused memory in the various LOHeaps ; we don't create the perfect storm of OutOfMemoryExceptions ? </p> <p><strong>Update:</strong> From Marc's response, I wanted to clarify that the LOH objects are not referenced - the large objects are use-n-throw - however the heap doesn't shrink even though the heap is relatively empty post the initial surge.</p> <p><strong>Update#2:</strong> Just including a code snippet (exaggerated but gets the point across I think).. I see an OutOfMemoryException around the time the Virtual memory hits the 1.5G mark on my machine (1.7G on another).. From <a href="http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx" rel="nofollow noreferrer">Eric L.'s blog post</a>, 'process memory can be visualized as a massive file on disk..' - this result is thus unexpected. The machines in this instance had GBs of free space on the HDD. Does the PageFile.sys OS file (or related settings) impose any restrictions?</p> <pre><code> static float _megaBytes; static readonly int BYTES_IN_MB = 1024*1024; static void BigBite() { try { var list = new List&lt;byte[]&gt;(); int i = 1; for (int x = 0; x &lt; 1500; x++) { var memory = new byte[BYTES_IN_MB + i]; _megaBytes += memory.Length / BYTES_IN_MB; list.Add(memory); Console.WriteLine("Allocation #{0} : {1}MB now", i++, _megaBytes); } } catch (Exception e) { Console.WriteLine("Boom! {0}", e); // I put a breakpoint here to check the console throw; } } static void Main(string[] args) { BigBite(); Console.WriteLine("Check VM now!"); Console.ReadLine(); _megaBytes = 0; ThreadPool.QueueUserWorkItem(delegate { BigBite(); }); ThreadPool.QueueUserWorkItem(delegate { BigBite(); }); Console.ReadLine(); // will blow before it reaches here } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload