SlideShare une entreprise Scribd logo
1  sur  32
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

Contenu connexe

En vedette

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
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET componentsBình Trọng Án
 
.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 CollectionDoug Hawkins
 
IoT garbage monitoring
IoT garbage monitoringIoT garbage monitoring
IoT garbage monitoringVitthal Adhav
 
.NET Framework Overview
.NET Framework Overview.NET Framework Overview
.NET Framework OverviewDoncho Minkov
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net frameworkArun Prasad
 
IoT based garbage monitoring system
IoT based garbage monitoring systemIoT based garbage monitoring system
IoT based garbage monitoring systemRanjan Gupta
 
Introduction to metadata management
Introduction to metadata managementIntroduction to metadata management
Introduction to metadata managementOpen Data Support
 

En vedette (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
 

Similaire à 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 4aminmesbahi
 
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 SolrGregg 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, EtsyLucidworks
 
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 SolrCloudAnshum 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 ContainersC4Media
 
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 JavaAbhishek Asthana
 
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
 
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 gamesWooga
 
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 Controlindiver
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotionMichael 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 LuccisanoGuillaume 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 objectsITD Systems
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraJon 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 TechniquesBrett Meyer
 
Common Challenges in DevOps Change Management
Common Challenges in DevOps Change ManagementCommon Challenges in DevOps Change Management
Common Challenges in DevOps Change ManagementMatt Ray
 
Cloud conference - mongodb
Cloud conference - mongodbCloud conference - mongodb
Cloud conference - mongodbMitch Pirtle
 
REST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl MongersREST::Neo4p - Talk @ DC Perl Mongers
REST::Neo4p - Talk @ DC Perl MongersMark 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 TrenchesGeorge Ang
 

Similaire à 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 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"
 
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
 
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
 

Plus de 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
 
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 serversWekoslav 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 applicationsWekoslav 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.jsWekoslav 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 JavaScriptWekoslav Stefanovski
 
Testing your Single Page Application
Testing your Single Page ApplicationTesting your Single Page Application
Testing your Single Page ApplicationWekoslav 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 rightWekoslav Stefanovski
 
SOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere MortalsSOLID -Clean Code For Mere Mortals
SOLID -Clean Code For Mere MortalsWekoslav Stefanovski
 

Plus de 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
 

Dernier

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 

Dernier (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

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