examples of garbage collection bottlenecks

2019-07-13 08:24发布

I remembered someone telling me one good one. But i cannot remember it. I spent the last 20mins with google trying to learn more.

What are examples of bad/not great code that causes a performance hit due to garbage collection ?

10条回答
欢心
2楼-- · 2019-07-13 08:55

When you have some loop involving the creation of new object's instances: if the number of cycles is very high you procuce a lot of trash causing the Garbage Collector to run more frequently and so decreasing performance.

查看更多
叛逆
3楼-- · 2019-07-13 09:02
  • frequent memory allocations
  • lack of memory reusing (when dealing with large memory chunks)
  • keeping objects longer than needed (keeping references on obsolete objects)
查看更多
淡お忘
4楼-- · 2019-07-13 09:05

I can give you an example that will work with the .Net CLR GC:

If you override a finalize method from a class and do not call the super class Finalize method such as



protected override void Finalize(){
    Console.WriteLine("Im done");
    //base.Finalize(); => you should call him!!!!!
}

When you resurrect an object by accident


protected override void Finalize(){
    Application.ObjJolder = this;
}

class Application{
    static public object ObjHolder;
}

When you use an object that uses Finalize it takes two GC collections to get rid of the data, and in any of the above codes you won't delete it.

查看更多
手持菜刀,她持情操
5楼-- · 2019-07-13 09:05

In most modern collectors, any use of finalization will slow the collector down. And not just for the objects that have finalizers.

查看更多
登录 后发表回答