简体   繁体   中英

How to minimize the length of the GC collections?

I need an application that will run smoothly. I have many serial chunks of computations I need to consecutively perform in short periods of time each, so I don't mind the GC doing it's job and I even can take more frequent collections but what I need to minimize the length of each GC collection.

I would like (if possible) to have 1 milli max pause of thread activity due to the GC each time.

what is the best way to acheive this in .NET (I know that .NET it not the technology for such demands but if it will meet my demands when optimized the save of development hours and flexibility for future specs is good incentive to try it out)?

Right from the MSDN page: https://msdn.microsoft.com/en-us/library/ms973837.aspx

The .NET garbage collector provides a high-speed allocation service with good use of memory and no long-term fragmentation problems, however it is possible to do things that will give you much less than optimal performance. To get the best out of the allocator you should consider practices such as the following:

  • Allocate all of the memory (or as much as possible) to be used with a given data structure at the same time. Remove temporary allocations that can be avoided with little penalty in complexity.

  • Minimize the number of times object pointers get written, especially those writes made to older objects.

  • Reduce the density of pointers in your data structures.

  • Make limited use of finalizers, and then only on "leaf" objects, as much as possible. Break objects if necessary to help with this.

  • A regular practice of reviewing your key data structures and conducting memory usage profiles with tools like Allocation Profiler will go a long way to keeping your memory usage effective and having the garbage collector working its best for you.

As Ron mentioned in his comment. You have to be extra smart with .NET if you want a lot of control over the GC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM