0% found this document useful (0 votes)
47 views14 pages

garbage collector ppt.pptxmnkjn

bjhgyujg

Uploaded by

hokona4225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views14 pages

garbage collector ppt.pptxmnkjn

bjhgyujg

Uploaded by

hokona4225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Dot Net Framework

Garbage Collector
Garbage Garbage Collector
Int a=10; a 10
Garbage Collector
The garbage collector provides the following benefits:

• Frees developers from having to manually release memory.


• Allocates objects on the managed heap efficiently.
• Reclaims objects that are no longer being used, clears their memory, and
keeps the memory available for future allocations. Managed objects
automatically get clean content to start with, so their constructors don't
have to initialize every data field.
• Provides memory safety by making sure that an object cannot use the
content of another object.
Phases in Garbage Collection

Marking Phase Phase -1


(List of Live Object created)

Sweeping Phase
(Unmarked object considered as garbage
Phase -2
and are ready for reclamation)

Compacting Phase Phase -3


(Live object moved and compacted)
Wedding Lunch Program
Lunch Ground

Phase -1

Phase -2

Phase -3
Phases in Garbage Collection

Marking Phase Phase -1


(List of Live Object created)

Sweeping Phase
(Unmarked object considered as garbage
Phase -2
and are ready for reclamation)

Compacting Phase Phase -3


(Live object moved and compacted)
Phases in Garbage Collection

• Marking Phase: During a garbage collection cycle, the GC starts by marking all reachable objects. It
traverses the object graph, starting from the roots (global variables, references on the stack, etc.), marking
each reachable object as "in-use."

• Sweeping Phase: Once the marking phase is complete, the GC sweeps through the managed heap,
identifying all the objects that were not marked during the marking phase. These unmarked objects are
considered garbage and are ready for reclamation.

• Compact Phase: In the final phase, the garbage collector reclaims the memory occupied by the garbage
objects. It compacts the live objects, moving them together to eliminate memory fragmentation, and updates
the heap's free space.
Garbage Collector
Heap Generations in Garbage Collection
The heap memory is organized into 3 generations so that various
objects with different lifetimes can be handled appropriately during
garbage collection.

Generation 0
Generation 1
Generation 2
Heap Memory

Generation - 0

Generation – 1

Generation – 2
Why do we need Generations?

• Normally, when we are working with big applications, they can create thousands of
objects. So, for each of these objects, if the garbage collector goes and checks if they
are needed or not.

• it’s really a bulky process. By creating such generations what it means if an object in
Generation 2 buckets it means the Garbage Collector will do fewer visits to this
bucket.

• The reason is, if an object move to Generation 2, it means it will stay more time in
the memory. It’s no point going and checking them again and again.

You might also like