How To Reduce Garbage Collection Performance Overhead

MY application profiling showing a lot of garbage collection overhead. The profiler does not have a drill in the garbage collection. What to do to reduce overhead?

I have many low-living arraylists and some long-living who only die when the application closes.

+4
source share
4 answers

Well, basically you have to cut down on the garbage collector. There are certain “patterns” that do a lot of work.

  • Avoid having multiple objects with finalizers. Finalizers impose additional work on the garbage collector, because an object with a finalizer must be collected twice.
  • Avoid the “midlife crisis” .. NET GC (on the desktop) is a GC generation. When the object survives in the first collection, but soon "dies", GC did a great job for nothing. (transition to the second generation, collecting again, etc.). Therefore, try to optimize the life time of your objects so that they either die quickly or survive for a long time.
  • Reduce unnecessary distributions. For example, using the value type wisely. Or do the work in a less intense way.

So, in your case, I think that you or you have a “middle-aged” crisis with short lists. Or you just select a list like crazy.

In the first case: try to reduce the life of the lists. I can’t say what the solution looks like for your application.

In the second case: try to avoid highlighting so many lists. Maybe you can use the correct value types? Or fixed size arrays? Or change the structure of the code so that less lists are needed?

In any case, I would recommend commenting on your application and see how much memory you allocate and how much you can collect in the first generation.

+7
source

If you have too much garbage collection overhead, reduce the amount of garbage. Try reusing lists (pre-distribute and use them, clean them when it's done).

If you are using an ArrayList with type values, try using List<T> instead.

+5
source

If the overhead of garbage collection becomes a serious performance problem, then you need to look at your design and recalculate the number of short-lived objects that you create.

+2
source

If this application runs as a service or does a lot of work before returning to the user interface, you may need to change the garbage collection model.

Without additional details, it is difficult to give a good recommendation.

0
source

Source: https://habr.com/ru/post/1315964/


All Articles