Related .NET internals:
1. Gen 0 and Gen 1 are
compacting heap regions: When Garbage collector (GC) operates on a Gen
0 or Gen 1 Heap region, It picks the un-referred objects and strongly referred
objects are moved to higher heap region. Due to this Gen 0 and Gen 1 heaps get
compacted after a GC operation.
2. Gen 2 and LOH are
non-compacting heaps: These two heap regions do
not compact after a GC operation, so they are called non-compacting heaps.
Let’s prove the above concepts
using a live debugging demo.
Our Suggestion: We have made a short
YouTube video explaining the concepts and Steps walkthrough that can help the
below hands-on session. Video link is available here
Demo Scenario:
In this lab, the user needs to analyze
different dumps of the same application at different occasions. Let us describe
the occasions of collecting the dumps.
First dump: (Before GC condition)
Application creates
10000 new apple objects and they are situating at the Gen 0 heap area. An application
dump was collected at this time.
Second dump: (After a GC)
A Garbage collection occurred on the
application heap. A dump is collected
at this state.
at this state.
Third dump: (one more GC happened)
Two times GC happened on application.
A dump is collected at this state.
Third dump: (one more GC happened)
GC happened third time on application.
A dump is collected at this state.
Lab 1:
Analyzing first dump
Prerequisites for this demo
- The dump file (23MB) for this demo available Here
- Download dump file, unzip the file.
Step-by-Step instructions for this demo
- Download dump file, unzip the file.
- Start WinDBG (x86 version).
- Open File Menu => Open Crash dump.
5. Apply command to load SOS.dll
[Image given below]
(sos.dll contains all .Net debugging commands. So this is a preliminary step prior
to every .Net application debugging using WinDBG.)
6. We
know that this dump has taken just after 10000 objects are created on the heap.
Let’s observe the Generational heaps of
the application.
It
is understood that after the garbage collection the Gen 0 referred objects
moved to Gen 1 region. Due to this Gen 0 heap is compacted.
This way the demo has proved
our concept Gen 0 heap compacts after the garbage collection.
Lab 3: Analyzing Third dump
Expecting result:
Since the dump has taken after happening two GC on application, now
the heap objects are moved from Gen1 => Gen 2 . (image provided below).
Also Gen 1 heap is compacted in comparison with second dump result.
Apply command !heapstat on the prompt (Result provided below)
In this result user can
observe,
a) All
the three generations Gen 0, Gen 1, Gen
2 and LOH regions and
their respective heap sizes.
their respective heap sizes.
b) Gen 0 size is huge [pointed by red circle]
compared to other regions.
All the apple objects are situated here. Gen 1 and Gen 2 are just 12
bytes in size.
All the apple objects are situated here. Gen 1 and Gen 2 are just 12
bytes in size.
(Info : The above picture is the Before GC snapshot of Generational heaps)
Now let’s see what happens to these Gen
heap regions after a garbage collection (GC)
occurrence. For that we have to
analyze the second dump which had taken after a
GC occurrence.
Lab 2:
Analyzing Second dump
Prerequisites for this demo
1
1. Repeat
the steps in first demo except in the 4th step . Choose the second
dump
( CompactingHeaps_AfterFirstGC.dmp”) and continue till the 5th step.
2. Apply
command !heapstat on the prompt [Result image provided below]
In this result user can
observe,
a) Compare
this command output with the output of first dump. We can see
the Gen 0 heap is
compacted in the second dump.
b) Also, the Gen 1 heap region is expanded (Red
circle in below image)
comparing with the first one.
Lab 3: Analyzing Third dump
This is exercise for the user. Download
the third dump from Here and follow the
same steps of Lab 2 (except select the third dump in step 4)
same steps of Lab 2 (except select the third dump in step 4)
Expecting result:
Since the dump has taken after happening two GC on application, now
the heap objects are moved from Gen1 => Gen 2 . (image provided below).
Also Gen 1 heap is compacted in comparison with second dump result.
It is proved that, even
Gen 1 heap compact after a GC .
Lab 4:
Analyzing 4th dump
This is exercise for the user. Download
the 4th dump from Here and follow the
same
steps of Lab 2 (except select the third dump in step 4).
Expected Result
:
Unlike Gen 0 and Gen 1 heap regions, Gen 2 heap will
not compact. It is a
non-compacting heap region. Image given below. Please observe this result
with the
third dump result. The size of Gen 2 heap is more or less the same.




