SlideShare a Scribd company logo
Ready
Ready
Wekoslav Stefanovski
Senior Developer / Seavus
Комунална Хигиена.net
Ready
Please Mute
your mobile
devices
Ready
General Sponsors
Platinum Sponsors
Silver Sponsors
Gold Sponsors
Bronze Sponsors
Promoters
Ready
Agenda
• How is memory organized in .net , how is it managed
and by whom?
• What are garbage generations?
• What are finalizers, and why should I care?
• What are weak references (and why should I care)?
• Things that you should never do
Ready
Memory Organization
Ready
Stack & Managed Heap
• Stack – typically a per-thread 1MB structure.
– (Mainly) used for value types
– Local variables (and parameters) usually live here
– Keeps track of execution chain
– Uses stack frames
– Ultra fast, very light
– No garbage collection, because none is needed
Ready
Stack & Managed Heap
• Heap
– Used for everything not elsewhere
– Not a monolith structure, actually 4 heaps
• Small object heap, generation 0
• Small object heap, generation 1
• Small object heap, generation 2
• Large object heap
– Very fast
– The domain of The Collector
Ready
Stack & Managed Heap
Ready
Garbage collector goals
• To enable you to develop your application without
having to free memory.
• To reclaim objects that are no longer being used, clears
their memory, and keeps the memory available for
future allocations.
• To provide memory safety by making sure that an object
cannot use the content of another object.
• To allocate objects on the managed heap efficiently.
Ready
Garbage collector information
• Runs on top of the Windows (or other) memory
management.
• Does so in 3 generations (Gen0, Gen1, Gen2)
• Works on a separate thread(s)
• Can run concurrently (in the background) or separately
• Optimized for latency or throughput
• Essentially a non-real-time component of .net
Ready
Details of a garbage run
• Three phases:
– Mark
– Sweep
– Compact
Ready
Details of a garbage run - mark
• The assumption is: Everything is dead until proven alive
• The Collector makes a list of root objects (from globals,
things on the stack and processor registers)
• Marks referenced objects as alive
• Mark everything referenced by referenced object as
alive
Ready
Details of a garbage run - sweep
• Move everything that is marked alive to the next
generation
• Fix the references, so they are working
• Nuke the current generation (from orbit)
Ready
Details of a garbage run - compact
• On Gen0 and Gen1 runs, zeroes out everything
• On Gen2 zeroes everything that is not marked as live
• Moves the objects next to one another
• We can easily allocate new objects – includes only
reserving the required memory size
• Constructing new objects is very cheap and fast in .net
• It’s actually faster than C++
Ready
Large Object Heap
• Special handling for large objects
• Too heavy to move – so nobody moves them
• Limit is 85KB
• Not collected on Gen0 or Gen1 collections
• Collected on Gen2 collections
• It is never compacted, so it can get heavily fragmented
ReadyReady
Demo:
Collecting
Garbage
Ready
Finalizers and disposables
Ready
Finalizers
• .net does not have destructors
• They are called finalizers
• Finalizers are executed non-deterministically
• Special root collection, called freachable queue
• Causes generational promotion
• It takes at least two runs of different generations to get
rid of a finalizable object
• Exception thrown in finalizers are uncatchable
• Don’t use them if you do not need them
Ready
Disposing
• Managed way to free acquired resources
• Integrated in C# with the using keyword
• It can, but should not throw exception
• The object should be disposed of after use
• No further use is expected after disposal is done
• It can be implemented in a special pattern so that it
plays nicely with finalizers
ReadyReady
Demo:
Implementing
Disposable
Ready
Weak References
Ready
Weak references
• Standard references are considered strong
• Weak reference is a non-binding reference
• It might be there, but then again, it mightn’t
• Commonly used by reference counting memory
managers (not required in .net)
Ready
Weak references
• The garbage collector will collect objects that only have
weak references
• Common scenario: objects that are somewhat
expensive to keep, but not very complicated to get (i.e.
MRU document list)
• Never trust IsAlive when it returns true
ReadyReady
Demo:
Weak
references
Ready
Some bad advices
Ready
GC.Collect
• Rule 1: Don’t
• Rule 2: Only call it if you can prove you know more that
the GC
• If you have lots of recently deceased objects in
Generation 2
• Rule 1 supersedes rule 2
Ready
Performance Measuring
• Never optimize what does need optimization
• It’s very very rare that a nicely written program has
intense memory pressure
• Always monitor performance
• Use performance counters – the GC has some nice
ones
• Don’t always trust them
• Always profile performance (speed and memory wise)
• Lots of profilers available, included with Visual Studio
• Measure, measure, measure
Ready
Finalizers
• Rule 1: Don’t use finalizers
• Rule 2: Don’t leak resources
• Rule 3: If you have to use finalizers, use the Dispose
pattern
Ready
Bottom line
• Forget this presentation (unless you need to remember it)
ReadyReady
Complete the
evaluation and earn the
chance to win valuable
prizes from our
sponsors
Questions
ReadyReady
Thank you

More Related Content

Viewers also liked

.Net assembly
.Net assembly.Net assembly
.Net assembly
mahesh2386
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)
Larry Nung
 
Assemblies
AssembliesAssemblies
Assemblies
Janas Khan
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
Bình Trọng Án
 
.Net Assemblies
.Net Assemblies.Net Assemblies
.Net Assemblies
Muhammad Kamran Rafi
 
.NET Framework 4.0 – Changes & Benefits
.NET Framework 4.0 – Changes & Benefits .NET Framework 4.0 – Changes & Benefits
.NET Framework 4.0 – Changes & Benefits
Deepika Chaudhary
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
Doug Hawkins
 
IoT garbage monitoring
IoT garbage monitoringIoT garbage monitoring
IoT garbage monitoring
Vitthal Adhav
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
Naveen Sihag
 
.NET Framework Overview
.NET Framework Overview.NET Framework Overview
.NET Framework Overview
Doncho Minkov
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
Arun Prasad
 
IoT based garbage monitoring system
IoT based garbage monitoring systemIoT based garbage monitoring system
IoT based garbage monitoring system
Ranjan Gupta
 
Introduction to metadata management
Introduction to metadata managementIntroduction to metadata management
Introduction to metadata management
Open Data Support
 

Viewers also liked (13)

.Net assembly
.Net assembly.Net assembly
.Net assembly
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)
 
Assemblies
AssembliesAssemblies
Assemblies
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
 
.Net Assemblies
.Net Assemblies.Net Assemblies
.Net Assemblies
 
.NET Framework 4.0 – Changes & Benefits
.NET Framework 4.0 – Changes & Benefits .NET Framework 4.0 – Changes & Benefits
.NET Framework 4.0 – Changes & Benefits
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
IoT garbage monitoring
IoT garbage monitoringIoT garbage monitoring
IoT garbage monitoring
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
.NET Framework Overview
.NET Framework Overview.NET Framework Overview
.NET Framework Overview
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 
IoT based garbage monitoring system
IoT based garbage monitoring systemIoT based garbage monitoring system
IoT based garbage monitoring system
 
Introduction to metadata management
Introduction to metadata managementIntroduction to metadata management
Introduction to metadata management
 

Similar to Garbage Collection .Net

.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
aminmesbahi
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
Techizzaa
 
Lessons from Sharding Solr
Lessons from Sharding SolrLessons from Sharding Solr
Lessons from Sharding Solr
Gregg Donovan
 
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, EtsyLessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lucidworks
 
Best practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloudBest practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloud
Anshum Gupta
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup
 
The Highs and Lows of Stateful Containers
The Highs and Lows of Stateful ContainersThe Highs and Lows of Stateful Containers
The Highs and Lows of Stateful Containers
C4Media
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
Erlang, the big switch in social games
Erlang, the big switch in social gamesErlang, the big switch in social games
Erlang, the big switch in social games
Wooga
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
Paolo Negri
 
Make It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version ControlMake It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version Control
indiver
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotion
Michael Denomy
 
Rails performance at Justin.tv - Guillaume Luccisano
Rails performance at Justin.tv - Guillaume LuccisanoRails performance at Justin.tv - Guillaume Luccisano
Rails performance at Justin.tv - Guillaume Luccisano
Guillaume Luccisano
 
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objectsBacking Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
ITD Systems
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
Jon Haddad
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
Brett Meyer
 
Common Challenges in DevOps Change Management
Common Challenges in DevOps Change ManagementCommon Challenges in DevOps Change Management
Common Challenges in DevOps Change Management
Matt Ray
 
Cloud conference - mongodb
Cloud conference - mongodbCloud conference - mongodb
Cloud conference - mongodb
Mitch Pirtle
 
REST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl MongersREST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl Mongers
Mark Jensen
 
2010 AIRI Petabyte Challenge - View From The Trenches
2010 AIRI Petabyte Challenge - View From The Trenches2010 AIRI Petabyte Challenge - View From The Trenches
2010 AIRI Petabyte Challenge - View From The Trenches
George Ang
 

Similar to Garbage Collection .Net (20)

.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
Lessons from Sharding Solr
Lessons from Sharding SolrLessons from Sharding Solr
Lessons from Sharding Solr
 
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, EtsyLessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
Lessons From Sharding Solr At Etsy: Presented by Gregg Donovan, Etsy
 
Best practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloudBest practices for highly available and large scale SolrCloud
Best practices for highly available and large scale SolrCloud
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves
 
The Highs and Lows of Stateful Containers
The Highs and Lows of Stateful ContainersThe Highs and Lows of Stateful Containers
The Highs and Lows of Stateful Containers
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
 
Erlang, the big switch in social games
Erlang, the big switch in social gamesErlang, the big switch in social games
Erlang, the big switch in social games
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
 
Make It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version ControlMake It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version Control
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotion
 
Rails performance at Justin.tv - Guillaume Luccisano
Rails performance at Justin.tv - Guillaume LuccisanoRails performance at Justin.tv - Guillaume Luccisano
Rails performance at Justin.tv - Guillaume Luccisano
 
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objectsBacking Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
Backing Data Silo Atack: Alfresco sharding, SOLR for non-flat objects
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
Common Challenges in DevOps Change Management
Common Challenges in DevOps Change ManagementCommon Challenges in DevOps Change Management
Common Challenges in DevOps Change Management
 
Cloud conference - mongodb
Cloud conference - mongodbCloud conference - mongodb
Cloud conference - mongodb
 
REST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl MongersREST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl Mongers
 
2010 AIRI Petabyte Challenge - View From The Trenches
2010 AIRI Petabyte Challenge - View From The Trenches2010 AIRI Petabyte Challenge - View From The Trenches
2010 AIRI Petabyte Challenge - View From The Trenches
 

More from Wekoslav Stefanovski

Make you a Redux for fun and profit!
Make you a Redux for fun and profit!Make you a Redux for fun and profit!
Make you a Redux for fun and profit!
Wekoslav Stefanovski
 
Blazor - C# for the web
Blazor - C# for the webBlazor - C# for the web
Blazor - C# for the web
Wekoslav Stefanovski
 
Asynchrony on the web
Asynchrony on the webAsynchrony on the web
Asynchrony on the web
Wekoslav Stefanovski
 
Servers? We don't need no stinking servers
Servers? We don't need no stinking serversServers? We don't need no stinking servers
Servers? We don't need no stinking servers
Wekoslav Stefanovski
 
Typescript Decorators
Typescript DecoratorsTypescript Decorators
Typescript Decorators
Wekoslav Stefanovski
 
Coding for the cloud - development of modern web applications
Coding for the cloud - development of modern web applicationsCoding for the cloud - development of modern web applications
Coding for the cloud - development of modern web applications
Wekoslav Stefanovski
 
Through Meteor to the stars - Developing full-stack SPA's with meteor.js
Through Meteor to the stars - Developing full-stack SPA's with meteor.jsThrough Meteor to the stars - Developing full-stack SPA's with meteor.js
Through Meteor to the stars - Developing full-stack SPA's with meteor.js
Wekoslav Stefanovski
 
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScriptTypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
Wekoslav Stefanovski
 
How to write bad code using C#
How to write bad code using C#How to write bad code using C#
How to write bad code using C#
Wekoslav Stefanovski
 
Testing your Single Page Application
Testing your Single Page ApplicationTesting your Single Page Application
Testing your Single Page Application
Wekoslav Stefanovski
 
Smoke and Mirrors - Reflection in C#
Smoke and Mirrors - Reflection in C#Smoke and Mirrors - Reflection in C#
Smoke and Mirrors - Reflection in C#
Wekoslav Stefanovski
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done right
Wekoslav Stefanovski
 
Entity Framework 5 - Code First
Entity Framework 5 - Code FirstEntity Framework 5 - Code First
Entity Framework 5 - Code First
Wekoslav Stefanovski
 
SOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere MortalsSOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere Mortals
Wekoslav Stefanovski
 

More from Wekoslav Stefanovski (14)

Make you a Redux for fun and profit!
Make you a Redux for fun and profit!Make you a Redux for fun and profit!
Make you a Redux for fun and profit!
 
Blazor - C# for the web
Blazor - C# for the webBlazor - C# for the web
Blazor - C# for the web
 
Asynchrony on the web
Asynchrony on the webAsynchrony on the web
Asynchrony on the web
 
Servers? We don't need no stinking servers
Servers? We don't need no stinking serversServers? We don't need no stinking servers
Servers? We don't need no stinking servers
 
Typescript Decorators
Typescript DecoratorsTypescript Decorators
Typescript Decorators
 
Coding for the cloud - development of modern web applications
Coding for the cloud - development of modern web applicationsCoding for the cloud - development of modern web applications
Coding for the cloud - development of modern web applications
 
Through Meteor to the stars - Developing full-stack SPA's with meteor.js
Through Meteor to the stars - Developing full-stack SPA's with meteor.jsThrough Meteor to the stars - Developing full-stack SPA's with meteor.js
Through Meteor to the stars - Developing full-stack SPA's with meteor.js
 
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScriptTypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
TypeScript 1.6 - How I learned to Stop Worrying and Love JavaScript
 
How to write bad code using C#
How to write bad code using C#How to write bad code using C#
How to write bad code using C#
 
Testing your Single Page Application
Testing your Single Page ApplicationTesting your Single Page Application
Testing your Single Page Application
 
Smoke and Mirrors - Reflection in C#
Smoke and Mirrors - Reflection in C#Smoke and Mirrors - Reflection in C#
Smoke and Mirrors - Reflection in C#
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done right
 
Entity Framework 5 - Code First
Entity Framework 5 - Code FirstEntity Framework 5 - Code First
Entity Framework 5 - Code First
 
SOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere MortalsSOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere Mortals
 

Recently uploaded

當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
Jersey (CHE-PING) Su
 
Google ML-Kit - Understanding on-device machine learning
Google ML-Kit - Understanding on-device machine learningGoogle ML-Kit - Understanding on-device machine learning
Google ML-Kit - Understanding on-device machine learning
VishrutGoyani1
 
introduction of Ansys software and basic and advance knowledge of modelling s...
introduction of Ansys software and basic and advance knowledge of modelling s...introduction of Ansys software and basic and advance knowledge of modelling s...
introduction of Ansys software and basic and advance knowledge of modelling s...
sachin chaurasia
 
Odoo E-commerce website development guides
Odoo E-commerce website development guidesOdoo E-commerce website development guides
Odoo E-commerce website development guides
jhkdigitalmarketing
 
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
aslasdfmkhan4750
 
NBFC Software: Optimize Your Non-Banking Financial Company
NBFC Software: Optimize Your Non-Banking Financial CompanyNBFC Software: Optimize Your Non-Banking Financial Company
NBFC Software: Optimize Your Non-Banking Financial Company
NBFC Softwares
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
sachin chaurasia
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
neshakor5152
 
Break data silos with real-time connectivity using Confluent Cloud Connectors
Break data silos with real-time connectivity using Confluent Cloud ConnectorsBreak data silos with real-time connectivity using Confluent Cloud Connectors
Break data silos with real-time connectivity using Confluent Cloud Connectors
confluent
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
rachitkumar09887
 
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
karim wahed
 
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdfAWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
karim wahed
 
Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)
miso_uam
 
Top 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your WebsiteTop 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your Website
e-Definers Technology
 
Wired_2.0_Create_AmsterdamJUG_09072024.pptx
Wired_2.0_Create_AmsterdamJUG_09072024.pptxWired_2.0_Create_AmsterdamJUG_09072024.pptx
Wired_2.0_Create_AmsterdamJUG_09072024.pptx
SimonedeGijt
 
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdfBuilding infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
mohitd6
 
ThaiPy meetup - Indexes and Django
ThaiPy meetup - Indexes and DjangoThaiPy meetup - Indexes and Django
ThaiPy meetup - Indexes and Django
akshesh doshi
 
welcome to presentation on Google Apps
welcome to   presentation on Google Appswelcome to   presentation on Google Apps
welcome to presentation on Google Apps
AsifKarimJim
 
Prada Group Reports Strong Growth in First Quarter …
Prada Group Reports Strong Growth in First Quarter …Prada Group Reports Strong Growth in First Quarter …
Prada Group Reports Strong Growth in First Quarter …
908dutch
 
Introduction_to_Security_Assessments.ppt
Introduction_to_Security_Assessments.pptIntroduction_to_Security_Assessments.ppt
Introduction_to_Security_Assessments.ppt
sudsdeep
 

Recently uploaded (20)

當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
 
Google ML-Kit - Understanding on-device machine learning
Google ML-Kit - Understanding on-device machine learningGoogle ML-Kit - Understanding on-device machine learning
Google ML-Kit - Understanding on-device machine learning
 
introduction of Ansys software and basic and advance knowledge of modelling s...
introduction of Ansys software and basic and advance knowledge of modelling s...introduction of Ansys software and basic and advance knowledge of modelling s...
introduction of Ansys software and basic and advance knowledge of modelling s...
 
Odoo E-commerce website development guides
Odoo E-commerce website development guidesOdoo E-commerce website development guides
Odoo E-commerce website development guides
 
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
Independent Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class H...
 
NBFC Software: Optimize Your Non-Banking Financial Company
NBFC Software: Optimize Your Non-Banking Financial CompanyNBFC Software: Optimize Your Non-Banking Financial Company
NBFC Software: Optimize Your Non-Banking Financial Company
 
ANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdfANSYS Mechanical APDL Introductory Tutorials.pdf
ANSYS Mechanical APDL Introductory Tutorials.pdf
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
 
Break data silos with real-time connectivity using Confluent Cloud Connectors
Break data silos with real-time connectivity using Confluent Cloud ConnectorsBreak data silos with real-time connectivity using Confluent Cloud Connectors
Break data silos with real-time connectivity using Confluent Cloud Connectors
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
 
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) Course Introducti...
 
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdfAWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
AWS Cloud Practitioner Essentials (Second Edition) (Arabic) .pdf
 
Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)Software development... for all? (keynote at ICSOFT'2024)
Software development... for all? (keynote at ICSOFT'2024)
 
Top 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your WebsiteTop 10 Tips To Get Google AdSense For Your Website
Top 10 Tips To Get Google AdSense For Your Website
 
Wired_2.0_Create_AmsterdamJUG_09072024.pptx
Wired_2.0_Create_AmsterdamJUG_09072024.pptxWired_2.0_Create_AmsterdamJUG_09072024.pptx
Wired_2.0_Create_AmsterdamJUG_09072024.pptx
 
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdfBuilding infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
Building infrastructure with code_ A deep dive into CDK for IaC in Java.pdf
 
ThaiPy meetup - Indexes and Django
ThaiPy meetup - Indexes and DjangoThaiPy meetup - Indexes and Django
ThaiPy meetup - Indexes and Django
 
welcome to presentation on Google Apps
welcome to   presentation on Google Appswelcome to   presentation on Google Apps
welcome to presentation on Google Apps
 
Prada Group Reports Strong Growth in First Quarter …
Prada Group Reports Strong Growth in First Quarter …Prada Group Reports Strong Growth in First Quarter …
Prada Group Reports Strong Growth in First Quarter …
 
Introduction_to_Security_Assessments.ppt
Introduction_to_Security_Assessments.pptIntroduction_to_Security_Assessments.ppt
Introduction_to_Security_Assessments.ppt
 

Garbage Collection .Net

  • 2. Ready Wekoslav Stefanovski Senior Developer / Seavus Комунална Хигиена.net
  • 4. Ready General Sponsors Platinum Sponsors Silver Sponsors Gold Sponsors Bronze Sponsors Promoters
  • 5. Ready Agenda • How is memory organized in .net , how is it managed and by whom? • What are garbage generations? • What are finalizers, and why should I care? • What are weak references (and why should I care)? • Things that you should never do
  • 7. Ready Stack & Managed Heap • Stack – typically a per-thread 1MB structure. – (Mainly) used for value types – Local variables (and parameters) usually live here – Keeps track of execution chain – Uses stack frames – Ultra fast, very light – No garbage collection, because none is needed
  • 8. Ready Stack & Managed Heap • Heap – Used for everything not elsewhere – Not a monolith structure, actually 4 heaps • Small object heap, generation 0 • Small object heap, generation 1 • Small object heap, generation 2 • Large object heap – Very fast – The domain of The Collector
  • 10. Ready Garbage collector goals • To enable you to develop your application without having to free memory. • To reclaim objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. • To provide memory safety by making sure that an object cannot use the content of another object. • To allocate objects on the managed heap efficiently.
  • 11. Ready Garbage collector information • Runs on top of the Windows (or other) memory management. • Does so in 3 generations (Gen0, Gen1, Gen2) • Works on a separate thread(s) • Can run concurrently (in the background) or separately • Optimized for latency or throughput • Essentially a non-real-time component of .net
  • 12. Ready Details of a garbage run • Three phases: – Mark – Sweep – Compact
  • 13. Ready Details of a garbage run - mark • The assumption is: Everything is dead until proven alive • The Collector makes a list of root objects (from globals, things on the stack and processor registers) • Marks referenced objects as alive • Mark everything referenced by referenced object as alive
  • 14. Ready Details of a garbage run - sweep • Move everything that is marked alive to the next generation • Fix the references, so they are working • Nuke the current generation (from orbit)
  • 15. Ready Details of a garbage run - compact • On Gen0 and Gen1 runs, zeroes out everything • On Gen2 zeroes everything that is not marked as live • Moves the objects next to one another • We can easily allocate new objects – includes only reserving the required memory size • Constructing new objects is very cheap and fast in .net • It’s actually faster than C++
  • 16. Ready Large Object Heap • Special handling for large objects • Too heavy to move – so nobody moves them • Limit is 85KB • Not collected on Gen0 or Gen1 collections • Collected on Gen2 collections • It is never compacted, so it can get heavily fragmented
  • 19. Ready Finalizers • .net does not have destructors • They are called finalizers • Finalizers are executed non-deterministically • Special root collection, called freachable queue • Causes generational promotion • It takes at least two runs of different generations to get rid of a finalizable object • Exception thrown in finalizers are uncatchable • Don’t use them if you do not need them
  • 20. Ready Disposing • Managed way to free acquired resources • Integrated in C# with the using keyword • It can, but should not throw exception • The object should be disposed of after use • No further use is expected after disposal is done • It can be implemented in a special pattern so that it plays nicely with finalizers
  • 23. Ready Weak references • Standard references are considered strong • Weak reference is a non-binding reference • It might be there, but then again, it mightn’t • Commonly used by reference counting memory managers (not required in .net)
  • 24. Ready Weak references • The garbage collector will collect objects that only have weak references • Common scenario: objects that are somewhat expensive to keep, but not very complicated to get (i.e. MRU document list) • Never trust IsAlive when it returns true
  • 27. Ready GC.Collect • Rule 1: Don’t • Rule 2: Only call it if you can prove you know more that the GC • If you have lots of recently deceased objects in Generation 2 • Rule 1 supersedes rule 2
  • 28. Ready Performance Measuring • Never optimize what does need optimization • It’s very very rare that a nicely written program has intense memory pressure • Always monitor performance • Use performance counters – the GC has some nice ones • Don’t always trust them • Always profile performance (speed and memory wise) • Lots of profilers available, included with Visual Studio • Measure, measure, measure
  • 29. Ready Finalizers • Rule 1: Don’t use finalizers • Rule 2: Don’t leak resources • Rule 3: If you have to use finalizers, use the Dispose pattern
  • 30. Ready Bottom line • Forget this presentation (unless you need to remember it)
  • 31. ReadyReady Complete the evaluation and earn the chance to win valuable prizes from our sponsors Questions