4 ms·
.NET Garbage Collector Basics and Performance Hints (2003)
- gus_massa 10y agoThis article is from 2003.
- thomasz 10y agoIt's still solid advise, though.
- gus_massa 10y agoI agree, but if the OP or a mod adds (2003) to the title then it's easier to realize that it's an old version. Perhaps the current version has a few more tricks. Perhaps someone makes a comparison with the current JVM garbage collector, and it would be "unfair" to compare the 2017 version of one against the 2003 version of the other.
- jstimpfle 10y agoIt's ".NET" Garbage collector, not "Net".
- jstimpfle 10y agoNo need to downvote, the title has been fixed. Thanks.
- osd 10y agoThis is a really weird article. It manages to "cover" a huge amount of ground while simultaneously saying pretty much nothing.
- barrkel 10y agoIt has a bunch of concrete recommendations, and its explanation of how the GC works lets you see the rationale behind those recommendations, and extend them to novel situations. It explains the middle-age death problem, a direct consequence of the generations, by giving you a mental framework to reason about the .net GC. I used the insights in this article to build an application server back in 2005 or so that spent about 2% of its time in GC at full load.
- algorithmsRcool 10y agoFor those interested in more details about .NET garbage collector here is some reading. Garbage Collection Overview https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).... Fundamentals of garbage collection https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).... Visualising the .NET Garbage Collector http://mattwarren.org/2016/06/20/Visualising-the-dotNET-Garbage-Collector/ http://mattwarren.org/2016/06/20/Visualising-the-dotNET-Garb... Clr Book of the runtime : Garbage Collection https://github.com/dotnet/coreclr/blob/master/Documentation/botr/garbage-collection.md https://github.com/dotnet/coreclr/blob/master/Documentation/... Maoni Stephen's Blog https://blogs.msdn.microsoft.com/maoni https://blogs.msdn.microsoft.com/maoni https://blogs.msdn.microsoft.com/maoni/2004/06/15/using-gc-efficiently-part-1/ https://blogs.msdn.microsoft.com/maoni/2004/06/15/using-gc-e... https://blogs.msdn.microsoft.com/maoni/2004/09/25/using-gc-efficiently-part-2/ https://blogs.msdn.microsoft.com/maoni/2004/09/25/using-gc-e... https://blogs.msdn.microsoft.com/maoni/2004/12/19/using-gc-efficiently-part-3/ https://blogs.msdn.microsoft.com/maoni/2004/12/19/using-gc-e... https://blogs.msdn.microsoft.com/maoni/2005/05/06/using-gc-efficiently-part-4/ https://blogs.msdn.microsoft.com/maoni/2005/05/06/using-gc-e... There is also a refactoring effort to standardize the GC<->EE interface https://github.com/dotnet/coreclr/projects/3 https://github.com/dotnet/coreclr/projects/3 I would link to the source code of the GC itself, but it's a single 35KSLOC file.
- yread 10y agoI wonder how much of the advice is still important with the improvements .NET GC has received over the decades [1] [2] [1] http://scottdorman.github.io/2008/11/07/clr-4.0-garbage-collection-changes/ http://scottdorman.github.io/2008/11/07/clr-4.0-garbage-coll... [2] https://blogs.msdn.microsoft.com/dotnet/2012/07/20/the-net-framework-4-5-includes-new-garbage-collector-enhancements-for-client-and-server-apps/ https://blogs.msdn.microsoft.com/dotnet/2012/07/20/the-net-f...
- randseq 10y agoThe optimizations improved GC performance in general reducing latency, but, the advice in the article is still good. One thing that may seem dated is the concern with Finalization, which is still a valid concern, but nowadays there are very few cases in which one needs to create a finalizer for their types.
- yread 10y agoYes, they are all good tips. You will get the best performance if you don't touch memory at all, but that's not practical. There is always a trade-off: don't create large long-living objects mutable split them into smaller ones? But then you have more long lived objects.
- alkonaut 10y agoIf you are considering GC issues in an app, I think the reference code you should look at now is Roslyn and how it uses pooled ImmutableArrays to work with immutable collections almost without allocation overhead in most areas. Most GC churn in most applications (I would think) is down to Linq and collection use. https://github.com/dotnet/roslyn/blob/master/src/ExpressionEvaluator/Core/Source/ResultProvider/Helpers/ArrayBuilder.cs https://github.com/dotnet/roslyn/blob/master/src/ExpressionE...
- jconley 10y agoBack in 2005-2006 we ran into significant issues with the GC and pinned memory related to Windows sockets when building a networked server for XMPP. I believe the GC has improved since then for that scenario, but it was a big surprise when it happened. It was a classic case of a leaky abstraction. http://blog.jdconley.com/2006/06/how-to-build-scalable-net-server.html http://blog.jdconley.com/2006/06/how-to-build-scalable-net-s...
- zamalek 10y agoI was still in high school when I read that post. Thanks for writing it. Buffer pooling is still incredibly relevant in .Net (as it is in many languages).
- gubbe 10y agoJust for the sake of reference: https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.buffermanager(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.servicemodel...
- markdoubleyou 10y agoAlong these same lines, Microsoft.IO.RecyclableMemoryStream is also useful. https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream https://github.com/Microsoft/Microsoft.IO.RecyclableMemorySt...
- mysterydip 10y agoThis is timely as I'm troubleshooting a .NET app with randomly long garbage collection times. I'm just the sysadmin in this scenario, without access to the code, so I'm approaching it from Splunk and AppDynamics. If anyone has suggestions beyond this article, I'd appreciate it :)
- ZenoArrow 10y agoThis may be of interest: http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/learning-memory-management/memory-management-fundamentals http://www.red-gate.com/products/dotnet-development/ants-mem...
- prefect42 10y agoThat's a nice memory profiler, have good success with it. I've also used DebugDiag, https://www.microsoft.com/en-us/download/details.aspx?id=49924 https://www.microsoft.com/en-us/download/details.aspx?id=499... and plain ol' perfmon and its performance counters, to help track down GC-related issues.
- gumaflux 10y agoAgreed. Starting point PerfMon and PerfCounters, as an addition to great suggested profilers I would also have a look at PerfView / ETW. https://blogs.msdn.microsoft.com/dotnet/2012/10/09/improving-your-apps-performance-with-perfview/ https://blogs.msdn.microsoft.com/dotnet/2012/10/09/improving...