{"id":23179,"date":"2006-04-19T00:51:00","date_gmt":"2006-04-19T00:51:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/maoni\/2006\/04\/19\/large-object-heap\/"},"modified":"2021-10-04T16:34:50","modified_gmt":"2021-10-04T23:34:50","slug":"large-object-heap","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/large-object-heap\/","title":{"rendered":"Large Object Heap"},"content":{"rendered":"<p><P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\">LOH (Large Object Heap) contains objects that are 85,000 bytes or bigger (there\u2019s also some objects that are less than 85,000 bytes that are allocated on the LOH by the runtime itself but usually they are very small and we\u2019ll ignore them for this discussion). <\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\"><\/FONT>&nbsp;<\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\">The way LOH is implemented changed dramatically from 1.0 to 1.1. In 1.0 we used a malloc type of allocator for allocating large objects; in 1.1 and beyond we use the same allocator for both large and small objects: we acquire memory from the OS by heap segments and commit on a segment as needed. There\u2019s very little difference between 1.1 and 2.0 for LOH. <\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\"><\/FONT>&nbsp;<\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\">LOH is not compacted \u2013 however if you want a large object to be pinned you should make sure to pin it because whether LOH is compacted or not is an implementation detail that could be changed in the future. Free blocks between live large objects are threaded into a free list which will be used to satisfy large object allocation requests. Note that this is a real advantage for managed heap \u2013 we are able to be very efficient to manage fragmentation because we can coalesce adjacent free objects into a big free block. If a free block is too large we call MEM_RESET on it to tell the OS to not bother backing it up. So far I haven\u2019t heard of any fragmentation problems from production code (of course you write a program that specifically fragments the LOH) \u2013 I think one reason is we are doing a good job controlling fragmentation; the other reason is people usually don\u2019t churn LOH too much \u2013 one typical pattern I\u2019ve seen people use LOH is to allocate some large arrays that hold on to small objects. <\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\"><\/FONT>&nbsp;<\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\">When I talked about weak references in <\/FONT><A href=\"http:\/\/blogs.msdn.com\/maoni\/archive\/2004\/12\/19\/327149.aspx\"><FONT face=\"Verdana\" size=\"2\">Using GC Efficiently \u2013 Part 3<\/FONT><\/A><FONT face=\"Verdana\" size=\"2\">, I mentioned a performance implication of using weak refs which is when you use them to refer to really small objects that are comparable in size. Make sure that you are fine with the ratio of the space that weak ref objects take up over the size of objects that they refer to. There\u2019s no difference between using a weak reference to refer to a large and a small object aside from the fact that by definition this ratio is naturally smaller for large objects because of their sizes.<\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\"><\/FONT>&nbsp;<\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\">In <\/FONT><A href=\"http:\/\/blogs.msdn.com\/maoni\/archive\/2006\/01\/09\/511001.aspx#525121\"><FONT face=\"Verdana\" size=\"2\">this<\/FONT><\/A><FONT face=\"Verdana\" size=\"2\"> comment, it was asked why \u201cexplicitly setting a reference to null would prevent the LOH from growing faster than the collection rate\u201d. Actually I think what likely happened was the large objects were held live by something therefore GC was not reclaiming the memory. By setting these objects to null just lets GC know that those objects are garbage now and can be reclaimed. You can use either CLRProfiler or the !SOS.gcroot command to verify this. <\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"Verdana\" size=\"2\"><\/FONT>&nbsp;<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LOH (Large Object Heap) contains objects that are 85,000 bytes or bigger (there\u2019s also some objects that are less than 85,000 bytes that are allocated on the LOH by the runtime itself but usually they are very small and we\u2019ll ignore them for this discussion). &nbsp; The way LOH is implemented changed dramatically from 1.0 [&hellip;]<\/p>\n","protected":false},"author":3542,"featured_media":58792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[3010,3011,108],"class_list":["post-23179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-general","tag-maoniposts","tag-performance"],"acf":[],"blog_post_summary":"<p>LOH (Large Object Heap) contains objects that are 85,000 bytes or bigger (there\u2019s also some objects that are less than 85,000 bytes that are allocated on the LOH by the runtime itself but usually they are very small and we\u2019ll ignore them for this discussion). &nbsp; The way LOH is implemented changed dramatically from 1.0 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/3542"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=23179"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23179\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=23179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=23179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=23179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}