Leanpub Header

Skip to main content

.NET GC FAQ

Comprehensive summary in the form of 64 questions and answers about .NET memory management from an author of over 1000 pages big Pro .NET Memory Management book. Learn about typical issues, interesting caveats and ways of omitting them!

Minimum price

$9.90

$14.90

You pay

$14.90

Author earns

$11.92
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
87
Pages
About

About

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.

Share this book

Author

About the Author

Konrad Kokosa

Author of the Pro .NET Memory Management book. Programming for over a dozen years, solving performance problems and architectural puzzles in the .NET world, speeding up web applications. Independent consultant, blogger, speaker and fan of Twitter. He also shares his passion as a trainer in the area of .NET, especially about application performance and diagnostics. Microsoft MVP in the Visual Studio and Development Tools category. Co-founder of https://dotnetos.org initiative.

Contents

Table of Contents

1Introduction

2General questions

  1. 2.1What does XY problem mean?

3Memory management

  1. 3.1What is the memory consumption of my program?
  2. 3.2What is the difference between the heap and the stack?
  3. 3.3Does big memory consumption of a process slows it down?
  4. 3.4When access violation may happen in regular .NET application?
  5. 3.5Memory is cheap! Why should I care about its usage then?!
  6. 3.6How fast is .NET in terms of memory management performance comparing to native?
  7. 3.7What does matter more - memory consumption or memory access?
  8. 3.8How to handle large amount of data?
  9. 3.9Assembly management (unloading)
  10. 3.10How often are the OS memory allocation routines invoked?
  11. 3.11IIS - is garbage collection shared between app pools?!
  12. 3.12Why does memory go down after I take a heap dump?
  13. 3.13What is the one thing that you wish you knew about GC and/or memory in .NET when you were starting your .NET journey?

4Objects and Allocations

  1. 4.1What is the smallest object size possible?
  2. 4.2What is the biggest object size possible?
  3. 4.3Does memory object layout matter?
  4. 4.4Is there any difference between LOH and SOH allocation?
  5. 4.5Why arrays of doubles are allocated in Large Object Heap?
  6. 4.6I’ve heard allocations are cheap in .NET? If so, why they should be avoided?
  7. 4.7What is string interning?
  8. 4.8Is it possible to take an address of an object?
  9. 4.9How statics and ThreadStatic are handled in AppDomain vs AssemblyLoadContext?

5Garbage Collection

  1. 5.1When GC kicks in? How often it happens? How long it takes?
  2. 5.2How the GC works in .NET and how it knows what to delete?
  3. 5.3Do reference cycles slow down GC or affect memory/performance?
  4. 5.4Why references may become GC roots?
  5. 5.5How does the C# garbage collector find objects whose only reference is an interior pointer?
  6. 5.6Does setting to null make any sense?
  7. 5.7What is relation between allocated objects count, allocated memory size and GC pause time?
  8. 5.8What will happen if developer try to access an object which is garbage collected?
  9. 5.9What is the difference between server-mode GC and the regular one?
  10. 5.10What GC mode should I use?
  11. 5.11What are default generation sizes?
  12. 5.12How quickly an object get promoted to gen 1?
  13. 5.13Why does background GC can’t handle gen0 and gen1 collections?
  14. 5.14How Foreground GC and Background GC does not interfere on each other?
  15. 5.15Why does server mode isn’t the default mode for development environment?
  16. 5.16How compaction works in GC?
  17. 5.17Why all user threads are suspended for blocking parts of GC in case of Server GC?
  18. 5.18How to optimise GC configuration for containers/shared environments?
  19. 5.19Can one achieve deterministic pause times in .NET GC?
  20. 5.20What are typical memory related problems in .NET?
  21. 5.21What is the most important advice related to .NET memory management?
  22. 5.22What does the CLR do before throwing an OutOfMemoryException?
  23. 5.23How to build a GC friendly cache?
  24. 5.24Why GC.Collect calls are so discouraged?
  25. 5.25What are the optimization tricks that take into account GC and the memory usage in general?
  26. 5.26What is pinning?
  27. 5.27What is demotion?
  28. 5.28Where information about pinning and marking are stored?
  29. 5.29Is fragmentation bad or good?
  30. 5.30How to avoid fragmentation in SOH? in LOH?
  31. 5.31What are card tables and card bundles?
  32. 5.32What are write barriers?
  33. 5.33Why I notice different behaviour under Debug and Release builds?
  34. 5.34Is it possible to check in-process whether GC is happening?
  35. 5.35What is the reason for so high % Time in GC values?
  36. 5.36Will be there more GC configuration knobs, like in Java HotSpot GC?
  37. 5.37Can we replace GC in .NET? Can we modify it?
  38. 5.38Why gc.cpp file in CoreCLR is so huge?
  39. 5.39Is there any roadmap for improving GC?
  40. 5.40Is .NET GC better than GC in X language/runtime?
  41. 5.41How to learn more about GC?

6Resource management

  1. 6.1What is the difference between a managed and unmanaged resource?
  2. 6.2Should I implement a finalizer when I implement IDisposable?
  3. 6.3When in doubt - Dispose() - Is this always the case?
  4. 6.4Does GC treat any memory available under ArrayPool in any special way?
  5. 6.5I have a lot of objects in finalization queue. Is it bad?!
  6. 6.6Is it possible to have an object not referenced (unreachable) in finalization queue?

7Miscellaneous topics

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

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) and EPUB (for phones, tablets and 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub