.NET GC FAQ
.NET GC FAQ
About the Book
Note: This is in progress book, with a rough list of questions to be answered in the final version. The minimum price will be raised when the book is finished. Estimated arrival time is Fall 2020.
The topic of .NET memory management can be seen as quite simple because everything there is automatic and managed by the Garbage Collector. And that's true for most cases. Still, there are some more boundary cases when the convenient abstraction of the GC may become problematic. For example:
- do you know there can be a memory leak in the managed .NET application?
- do you know that your application may be in over 80% busy by doing the GC, not its original work?
- do you know why we should avoid finalizers?
- do you know what are the common cases of allocations?
This book provides a wide variety of questions and answers like the above. It is obviously not as comprehensive as the author's original Pro .NET Memory Management book. Instead, it complements and extends Pro .NET Memory Management, by providing a summary of various topics, while also mentioning some not covered in the original book.
Table of Contents
- 1 Introduction
-
2 General questions
- 2.1 What does XY problem mean?
-
3 Memory management
- 3.1 What is the memory consumption of my program?
- 3.2 What is the difference between the heap and the stack?
- 3.3 Does big memory consumption of a process slows it down?
- 3.4 When access violation may happen in regular .NET application?
- 3.5 Memory is cheap! Why should I care about its usage then?!
- 3.6 How fast is .NET in terms of memory management performance comparing to native?
- 3.7 What does matter more - memory consumption or memory access?
- 3.8 How to handle large amount of data?
- 3.9 Assembly management (unloading)
- 3.10 How often are the OS memory allocation routines invoked?
- 3.11 IIS - is garbage collection shared between app pools?!
- 3.12 Why does memory go down after I take a heap dump?
- 3.13 What is the one thing that you wish you knew about GC and/or memory in .NET when you were starting your .NET journey?
-
4 Objects and Allocations
- 4.1 What is the smallest object size possible?
- 4.2 What is the biggest object size possible?
- 4.3 Does memory object layout matter?
- 4.4 Is there any difference between LOH and SOH allocation?
- 4.5 Why arrays of doubles are allocated in Large Object Heap?
- 4.6 I’ve heard allocations are cheap in .NET? If so, why they should be avoided?
- 4.7 What is string interning?
- 4.8 Is it possible to take an address of an object?
-
4.9 How statics and ThreadStatic are handled in
AppDomain
vsAssemblyLoadContext
?
-
5 Garbage Collection
- 5.1 When GC kicks in? How often it happens? How long it takes?
- 5.2 How the GC works in .NET and how it knows what to delete?
- 5.3 Do reference cycles slow down GC or affect memory/performance?
- 5.4 Why references may become GC roots?
- 5.5 How does the C# garbage collector find objects whose only reference is an interior pointer?
-
5.6 Does setting to
null
make any sense? - 5.7 What is relation between allocated objects count, allocated memory size and GC pause time?
- 5.8 What will happen if developer try to access an object which is garbage collected?
- 5.9 What is the difference between server-mode GC and the regular one?
- 5.10 What GC mode should I use?
- 5.11 What are default generation sizes?
- 5.12 How quickly an object get promoted to gen 1?
- 5.13 Why does background GC can’t handle gen0 and gen1 collections?
- 5.14 How Foreground GC and Background GC does not interfere on each other?
- 5.15 Why does server mode isn’t the default mode for development environment?
- 5.16 How compaction works in GC?
- 5.17 Why all user threads are suspended for blocking parts of GC in case of Server GC?
- 5.18 How to optimise GC configuration for containers/shared environments?
- 5.19 Can one achieve deterministic pause times in .NET GC?
- 5.20 What are typical memory related problems in .NET?
- 5.21 What is the most important advice related to .NET memory management?
- 5.22 What does the CLR do before throwing an OutOfMemoryException?
- 5.23 How to build a GC friendly cache?
-
5.24 Why
GC.Collect
calls are so discouraged? - 5.25 What are the optimization tricks that take into account GC and the memory usage in general?
- 5.26 What is pinning?
- 5.27 What is demotion?
- 5.28 Where information about pinning and marking are stored?
- 5.29 Is fragmentation bad or good?
- 5.30 How to avoid fragmentation in SOH? in LOH?
- 5.31 What are card tables and card bundles?
- 5.32 What are write barriers?
- 5.33 Why I notice different behaviour under Debug and Release builds?
- 5.34 Is it possible to check in-process whether GC is happening?
- 5.35 What is the reason for so high % Time in GC values?
- 5.36 Will be there more GC configuration knobs, like in Java HotSpot GC?
- 5.37 Can we replace GC in .NET? Can we modify it?
-
5.38 Why
gc.cpp
file in CoreCLR is so huge? - 5.39 Is there any roadmap for improving GC?
- 5.40 Is .NET GC better than GC in X language/runtime?
- 5.41 How to learn more about GC?
-
6 Resource management
- 6.1 What is the difference between a managed and unmanaged resource?
- 6.2 Should I implement a finalizer when I implement IDisposable?
- 6.3 When in doubt - Dispose() - Is this always the case?
-
6.4 Does GC treat any memory available under
ArrayPool
in any special way? - 6.5 I have a lot of objects in finalization queue. Is it bad?!
- 6.6 Is it possible to have an object not referenced (unreachable) in finalization queue?
- 7 Miscellaneous topics
- Notes
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,595,069writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
SignalR on .NET 6 - the Complete Guide
Fiodar SazanavetsLearn everything there is to learn about SignalR and how to integrate it with the latest .NET 6 and C# 10 features. Learn how to connect any type of client to SignalR, including plain WebSocket client. Learn how to build interactive applications that can communicate with each other in real time without making excessive calls.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Functional event-driven architecture: Powered by Scala 3
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way, mainly powered by Fs2 streams in Scala 3!
Leverage your functional programming skills by designing and writing stateless microservices that scale, powered by stateful message brokers.
The easiest way to learn design patterns
Fiodar SazanavetsLearn design patterns in the easiest way possible. You will no longer have to brute-force your way through each one of them while trying to figure out how it works. The book provides a unique methodology that will make your understanding of design patterns stick. It can also be used as a reference book where you can find design patterns in seconds.
CCIE Service Provider Version 4 Written and Lab Exam Comprehensive Guide
Nicholas RussoThe service provider landscape has changed rapidly over the past several years. Networking vendors are continuing to propose new standards, techniques, and procedures for overcoming new challenges while concurrently reducing costs and delivering new services. Cisco has recently updated the CCIE Service Provider track to reflect these changes; this book represents the author's personal journey in achieving that certification.
Cronache di Domain-Driven Design
Francesco Strazzullo, Matteo Baglini, Gianluca Padovani, Emanuele DelBono, Marco Consolaro, Alessandro Colla, Uberto Barbini, Alberto Acerbis, Julie Camosseto, DDD Open, and Alberto BrandoliniCronache di Domain-Driven Design: un libro corale in italiano fatto di storie indipendenti tra loro, che sono il risultato dell’applicazione di Domain-Driven Design su progetti reali.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #3
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #4
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #6
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #7
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #9
Retromat eBook Bundle for Agile Retrospectives
2 Books
If you facilitate retrospectives this bundle is for you: "Plans for Retrospectives" helps beginners learn the lay of the land with tried-and-true plans. Once you know your way around, "Run great agile retrospectives" contains all 135+ activities in Retromat for you to mix and match.