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
Ad

More Related Content

Viewers also liked (13)

.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
 
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 components
Bì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 Collection
Doug Hawkins
 
IoT garbage monitoring
IoT garbage monitoringIoT garbage monitoring
IoT garbage monitoring
Vitthal Adhav
 
.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
 

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
Amin Mesbahi
 
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 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 games
Wooga
 
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
 
Garbage collection in C#,important topic
Garbage collection in C#,important topicGarbage collection in C#,important topic
Garbage collection in C#,important topic
ksks28058
 
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
 
.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
Amin Mesbahi
 
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 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 games
Wooga
 
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
 
Garbage collection in C#,important topic
Garbage collection in C#,important topicGarbage collection in C#,important topic
Garbage collection in C#,important topic
ksks28058
 
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
 
Ad

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!
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
 
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 servers
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
 
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
 
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
 
Ad

Recently uploaded (20)

Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New VersionPixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
Pixologic ZBrush Crack Plus Activation Key [Latest 2025] New Version
saimabibi60507
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 

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