Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve garbage collection performance?

What kind of optimization patterns can be used to improve the performance of the garbage collector?

My reason for asking is that I do a lot of embedded software using the Compact Framework. On slow devices the garbage collection can become a problem, and I would like to reduce the times the garbage collector kicks in, and when it does, I want it to finish quicker. I can also see that working with the garbage collector instead of against it could help improve any .NET or Java application, especially heavy duty web applications.

Here are some of my thoughts, but I haven’t done any benchmarks.

  • reusing temporary classes/arrays (keep down allocation count)
  • keeping the amount of live objects to a minimum (faster collections)
  • try to use structs instead of classes
like image 484
Martin Liesén Avatar asked Oct 04 '08 12:10

Martin Liesén


People also ask

What are the possible strategies to decrease garbage collection time?

If your application's object creation rate is very high, then to keep up with it, the garbage collection rate will also be very high. A high garbage collection rate will increase the GC pause time as well. Thus, optimizing the application to create fewer objects is THE EFFECTIVE strategy to reduce long GC pauses.

How does garbage collection impact performance?

The most common performance problem associated with Java™ relates to the garbage collection mechanism. If the size of the Java heap is too large, the heap must reside outside main memory. This causes increased paging activity, which affects Java performance.

What are the techniques for garbage collection?

There are two ways to do it : Using System. gc() method: System class contain static method gc() for requesting JVM to run Garbage Collector. Using Runtime.


1 Answers

The key is to understand how the CF GC works for allocations. It's a simple mark-and-sweep, non-generational GC with specific algorithms for what will trigger a GC, and what will cause compaction and/or pitching after collection. There is almost nothing you can do at an app level to control the GC (the only method available is Collect, and it's use is pretty limited, as you can't force compaction anyway).

Object re-use is a good start, but simply keeping the object count low is probably one of the best tools, as all roots have to be walked for any collection operation. Keeping that walk short is a good idea. If compaction is killing you, then preventing segment fragmentation will help. Objects >64k can be helpful in that regard as they get their own segment and are treated differently than smaller objects.

To really understand how the CF GC works, I'd recommend watching the MSDN Webcast on CF memory management.

like image 61
ctacke Avatar answered Sep 23 '22 01:09

ctacke