静态属性最终会出现在第二代中吗?
当我有一个静态字段/属性在应用程序池的生命周期中一直存在于我的应用程序池中时,它位于内存中的哪里?我假设它升级到了 gen2
并在那里度过了余生? (假设它足够小,不会进入 LOH)
When I have a static
field/property which lives in my application pool for the life of the application pool, where does it live in memory? I'm assuming its promoted into gen2
and lives out its days there? (assuming its small enough not to go into the LOH)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态属性位于分配给类型/类的类型对象的内存空间中的堆中。它们在加载类型的同时加载。
静态属性不能被垃圾收集。类型对象永远无法被卸载。
本书第 4 章末尾 CLR via C# 对此进行了解释。这是一个小片段图:
这只是该图的一部分(无法完全复制,因为版权)并显示
Employee
类型对象及其静态字段。类型对象与所有其他对象类似,并且有一个指向其类型的指针,该类型恰好是System.Type
。Static properties live in the heap in the memory space allocated to the type object for the type/class. They are loaded at the same time the type is loaded.
Static properties cannot be garbage collected. Type object can never be unloaded.
End of the chapter 4 of the book CLR via C# explains it. Here is a small snippet diagram:
This is just a portion of the diagram (cannot copy fully because of the copyright) and shows
Employee
type object with its static fields. Type object is similar to all other objects and has a pointer to its type which happens to beSystem.Type
.