zoukankan      html  css  js  c++  java
  • .Net memory management Learning Notes

    Managed Heaps

    In general it can be categorized into 1) SOH and 2) LOH.  size lower than 85K will be in SOH, size larger than 85K will be in LOH.

    Small Object Heap

    GC will do 1) Mark 2) Sweep 3) Compact on SOH.

    How GC works

    When a small object is created (less than 85KB in size), it is stored in the small object heap. The CLR organizes the small object heap into three generations – generation 0, generation 1, and generation 2 – that are collected at different intervals. Small objects are generally allocated to generation 0, and if they survive a GC cycle, they are promoted to generation 1. If they survive the next GC cycle, they are promoted to generation 2.

    Further, garbage collection of the small object heap includes compaction, meaning that as unused objects are collected, the GC moves living objects into the gaps to eliminate fragmentation and make sure that the available memory is contiguous. Of course, compaction involves overhead – both CPU cycles and additional memory for copying objects. Because the benefits of compaction outweigh the costs for small objects, compaction is performed automatically on the small object heap.

    GC uses generational garbition collection. .Net will device memory into 3 generations. Gen 0 is the youngest one, Gen 2 is the olddest one.

    Gen 0 collection will empty Gen 0 for sure. Either move the objects to Gen 1, or Die and compact the rootless objects.

    Gen 1 collection will scan gen 0 and gen 1 both, for objects still has root reference, move them from gen 0 to gen 1, and move the ones in gen 1 to gen 2.  

    Gen 2 is a full collection because all of the generations are inspected and colleted. 

    Gen 0, Gen 1 and Gen 2 are 10 times difference compared to each other.

    Some object may have un-managed resources, so these objects will have their reference added to the Finalization Queue.  There is a seperate thread other than GC which will run and call Finalize () method to recycle the objects.

    For rootless objects, collect them. in SOH, compact the available space.  in LOH, it does not do compaction, but it will maintain a free memory table.  next time, when there is a object > 85KB, it will check free table firstly, if it can accomodate the object, save it, if not, do fragmentation and allocate new memory.

  • 相关阅读:
    【转载】关于sql server 代理(已禁用代理xp)
    Silverlight中将WriteableBitmap互转byte数组
    思迅Pay PC ,WIN7 ,KB3042058
    微软新Edge浏览器 WIN7 无法登录
    List<SelectListItem> 转为 SelectList
    图片jpg,png转为BASE64编码
    “Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项。
    mac os 10.15.1 懒人 .CDR
    微信刷脸SDK获取sub_openid
    win7 安装 visual studio 2019 时闪退(VS2019)
  • 原文地址:https://www.cnblogs.com/xuyanran/p/8453471.html
Copyright © 2011-2022 走看看