Minimum price
Suggested price


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.

About the Author

Konrad Kokosa
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.

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 vs AssemblyLoadContext?
  • 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

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

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

In fact, authors have earnedover $12 millionwriting, 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