Finalization

1.what is the main disadvantage of garbage collection?

Typically, garbage collection has certain disadvantages, including consuming additional resources, performance impacts, possible stalls in program execution, and incompatibility with manual resource management.

 

Garbage collection consumes computing resources in deciding which memory to free, even though the programmer may have already known this information.

The penalty惩罚 for the convenience of not annotating object lifetime manually in the source code is overhead, which can lead to decreased or uneven performance.[4] 

A peer-reviewed paper came to the conclusion that GC needs five times the memory to compensate for this overhead and to perform as fast as explicit memory management.[5] 

Interaction with memory hierarchy effects can make this overhead intolerable in circumstances that are hard to predict or to detect in routine testing.

The impact on performance was also given by Apple as a reason for not adopting garbage collection in iOS despite being the most desired feature.[6]

 

The moment when the garbage is actually collected can be unpredictable, resulting in stalls (pauses to shift/free memory) scattered throughout a session.

Unpredictable stalls can be unacceptable in real-time environments, in transaction processing, or in interactive programs.

Incremental, concurrent, and real-time garbage collectors address these problems, with varying trade-offs.

 

The modern GC implementations try to avoid blocking "stop-the-world" stalls by doing as much work as possible on the background (i.e. on a separate thread),

for example marking unreachable garbage instances while the application process continues to run.

In spite of these advancements, for example in .NET CLR paradigm it is still very difficult to maintain large heaps (millions of objects) with resident objects

that get promoted to older generations without incurring noticeable delays (sometimes a few seconds).

 

Non-deterministic GC is incompatible with RAII based management of non-GCed resources[citation needed].

As a result, the need for explicit manual resource management (release/close) for non-GCed resources becomes transitive to composition.

That is: in a non-deterministic GC system, if a resource or a resource-like object requires manual resource management (release/close),

and this object is used as "part of" another object, then the composed object will also become a resource-like object that itself requires manual resource management (release/close).

 

2.How do you achieve deterime finalization?

https://www.safaribooksonline.com/library/view/programming-net-components/0596102070/ch04s05.html

The Open/Close Pattern

The Dispose() Pattern

The IDisposable Pattern

Disposing and Error Handling

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(399)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示