SlideShare a Scribd company logo
1 of 39
Everything* you 
need to know about 
.NET Memory 
Ben Emmett – NDC London – 4th December 2014 
*Most stuff
Some of what I’m 
about to say is a lie
public void CaffeineCheck(int coffeesConsumed) 
{ 
int coffeesRequired = 3; 
CaffeineAlert alert = new CaffeineAlert(); 
if (coffeesConsumed < coffeesRequired) 
{ 
alert.Announce("Send help."); 
} 
}
The Stack 
CaffeineCheck (int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
..... 
[Return address] 
Alert() 
CaffeineCheck() 
ParentMethod() 
For each thread
eg: 
byte 
int 
char 
Pointers 
eg: 
strings 
classes 
objects 
arrays 
Stack Heap
public void CaffeineCheck(int coffeesConsumed) 
{ 
int coffeesRequired = 3; 
CaffeineAlert alert = new CaffeineAlert(); 
if (coffeesConsumed < coffeesRequired) 
{ 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
Stack 
CaffeineAlert 
alert 
alert.Announce("Send help."); 
} 
}
string 
Email 
string 
Name 
Contact 
list 
string 
Email 
string 
Name 
Contact 
list 
CaffeineAlert 
alert 
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
Stack 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
List<Contact> 
Recipients
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
WRONG 
Stack Heap
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
WRONG 
Stack Heap
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
WRONG 
Stack Heap
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
WRONG 
Stack Heap
string 
Email 
string 
Name 
Contact 
list 
string 
Email 
string 
Name 
Contact 
list 
CaffeineAlert 
alert 
public class CaffeineAlert 
{ 
private List<Contact> Recipients; 
CaffeineCheck(int coffeesRequired) 
int coffeesRequired 
int coffeesConsumed 
CaffeineAlert alert 
[Return address] 
Stack 
} 
public class Contact 
{ 
private string Name; 
private string Email; 
} 
List<Contact> 
Recipients
CaffeineAlert 
alert 
List<Contact> 
Recipients 
String Name String Email String Name String Email
Object 
reference 
Stack
Object 
reference 
Stack
Object 
reference 
Stack
Object 
reference 
Stack
Small Object Heap (SOH) 
The Heap 
Gen 2 
Gen 1 
Gen 0 
Large Object Heap (LOH)
Address 
1100k 
1000k 
900k 
800k 
700k 
600k 
500k 
400k 
300k 
200k 
100k 
0
Address 
1100k 
1000k 
900k 
800k 
700k 
600k 
500k 
400k 
300k 
200k 
100k 
0
public void NaughtyStringMessage(string name) 
{ 
string 
name 
string message = "Hello "; 
message += name; 
Console.WriteLine(message); 
NaughtyStringMessage(string name) 
ref string name 
[Return address] 
Stack Heap 
} 
1 
2 
3 
4 
5 
6 
"Ted"
public void NaughtyStringMessage(string name) 
{ 
string 
name 
string message = "Hello "; 
message += name; 
Console.WriteLine(message); 
"Hello" 
Stack Heap 
} 
string 
message 
1 
2 
3 
4 
5 
6 
"Ted" 
NaughtyStringMessage(string name) 
ref string message 
ref string name 
[Return address]
public void NaughtyStringMessage(string name) 
{ 
string 
name 
string message = "Hello "; 
message += name; 
Console.WriteLine(message); 
"Hello Ted" 
"Hello" 
Stack Heap 
} 
string 
message 
1 
2 
3 
4 
5 
6 
string 
message 
"Ted" 
NaughtyStringMessage(string name) 
ref string message 
ref string name 
[Return address]
public static event EventHandler UserLoggedOut; 
Main form 
MainForm.UserLoggedOut += HideFormData; 
Child form
MainForm 
EventHandler 
UserLoggedOut 
ChildForm 
SecretDataWindow 
Loads Of Other Junk
public class UnmanagedClass 
{ 
~UnmanagedClass() 
{ 
//Clear up the unmanaged resources 
} 
}
public class UnmanagedClass : IDisposable 
{ 
public void Dispose() 
{ 
//Clear up the unmanaged resources 
GC.SuppressFinalize(this); 
} 
~UnmanagedClass() 
{ 
this.Dispose(); 
} 
} Not a complete IDisposable implementation
2013 
Most awesome 
Questions? 
ben.emmett@red-gate.com @bcemmett slideshare.net/benemmett

More Related Content

What's hot

Scala - den smarta kusinen
Scala - den smarta kusinenScala - den smarta kusinen
Scala - den smarta kusinen
Redpill Linpro
 

What's hot (16)

多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails
 
Go for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd editionGo for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd edition
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basics
 
DevOpsCon 2021: Go Web Development 101
DevOpsCon 2021: Go Web Development 101DevOpsCon 2021: Go Web Development 101
DevOpsCon 2021: Go Web Development 101
 
entwickler.de Go Day: Go Web Development 101
entwickler.de Go Day: Go Web Development 101entwickler.de Go Day: Go Web Development 101
entwickler.de Go Day: Go Web Development 101
 
betterCode() Go: Einstieg in Go, Standard-Library und Ökosystem
betterCode() Go: Einstieg in Go, Standard-Library und ÖkosystembetterCode() Go: Einstieg in Go, Standard-Library und Ökosystem
betterCode() Go: Einstieg in Go, Standard-Library und Ökosystem
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
The Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveThe Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's Perspective
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Scala - den smarta kusinen
Scala - den smarta kusinenScala - den smarta kusinen
Scala - den smarta kusinen
 
Hello Go
Hello GoHello Go
Hello Go
 
Posfix
PosfixPosfix
Posfix
 
Don't do this
Don't do thisDon't do this
Don't do this
 
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
 
Tcl2012 8.6 Changes
Tcl2012 8.6 ChangesTcl2012 8.6 Changes
Tcl2012 8.6 Changes
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014
 

Similar to .Net memory management ndc london

Linq - an overview
Linq - an overviewLinq - an overview
Linq - an overview
neontapir
 

Similar to .Net memory management ndc london (15)

KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptx
 
GeeCON 2014 - Functional Programming without Lambdas
GeeCON 2014 - Functional Programming without LambdasGeeCON 2014 - Functional Programming without Lambdas
GeeCON 2014 - Functional Programming without Lambdas
 
What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116
 
Pure kotlin
Pure kotlinPure kotlin
Pure kotlin
 
Best of build 2021 - C# 10 & .NET 6
Best of build 2021 -  C# 10 & .NET 6Best of build 2021 -  C# 10 & .NET 6
Best of build 2021 - C# 10 & .NET 6
 
Why Scala is the better Java
Why Scala is the better JavaWhy Scala is the better Java
Why Scala is the better Java
 
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Domain Driven Design with the F# type System -- F#unctional Londoners 2014Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
 
Linq - an overview
Linq - an overviewLinq - an overview
Linq - an overview
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
 
java sockets
 java sockets java sockets
java sockets
 
Kotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresKotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan Soares
 
Kotlin intro
Kotlin introKotlin intro
Kotlin intro
 

Recently uploaded

%+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
 
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
shinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%+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...
 
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
 
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
 
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...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%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
 
%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
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
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
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
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...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
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 🔝✔️✔️
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

.Net memory management ndc london

  • 1. Everything* you need to know about .NET Memory Ben Emmett – NDC London – 4th December 2014 *Most stuff
  • 2. Some of what I’m about to say is a lie
  • 3. public void CaffeineCheck(int coffeesConsumed) { int coffeesRequired = 3; CaffeineAlert alert = new CaffeineAlert(); if (coffeesConsumed < coffeesRequired) { alert.Announce("Send help."); } }
  • 4. The Stack CaffeineCheck (int coffeesRequired) int coffeesRequired int coffeesConsumed ..... [Return address] Alert() CaffeineCheck() ParentMethod() For each thread
  • 5. eg: byte int char Pointers eg: strings classes objects arrays Stack Heap
  • 6. public void CaffeineCheck(int coffeesConsumed) { int coffeesRequired = 3; CaffeineAlert alert = new CaffeineAlert(); if (coffeesConsumed < coffeesRequired) { CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] Stack CaffeineAlert alert alert.Announce("Send help."); } }
  • 7. string Email string Name Contact list string Email string Name Contact list CaffeineAlert alert public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] Stack } public class Contact { private string Name; private string Email; } List<Contact> Recipients
  • 8. public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] } public class Contact { private string Name; private string Email; } WRONG Stack Heap
  • 9. public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] } public class Contact { private string Name; private string Email; } WRONG Stack Heap
  • 10. public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] } public class Contact { private string Name; private string Email; } WRONG Stack Heap
  • 11. public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] } public class Contact { private string Name; private string Email; } WRONG Stack Heap
  • 12. string Email string Name Contact list string Email string Name Contact list CaffeineAlert alert public class CaffeineAlert { private List<Contact> Recipients; CaffeineCheck(int coffeesRequired) int coffeesRequired int coffeesConsumed CaffeineAlert alert [Return address] Stack } public class Contact { private string Name; private string Email; } List<Contact> Recipients
  • 13. CaffeineAlert alert List<Contact> Recipients String Name String Email String Name String Email
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Small Object Heap (SOH) The Heap Gen 2 Gen 1 Gen 0 Large Object Heap (LOH)
  • 26. Address 1100k 1000k 900k 800k 700k 600k 500k 400k 300k 200k 100k 0
  • 27. Address 1100k 1000k 900k 800k 700k 600k 500k 400k 300k 200k 100k 0
  • 28. public void NaughtyStringMessage(string name) { string name string message = "Hello "; message += name; Console.WriteLine(message); NaughtyStringMessage(string name) ref string name [Return address] Stack Heap } 1 2 3 4 5 6 "Ted"
  • 29. public void NaughtyStringMessage(string name) { string name string message = "Hello "; message += name; Console.WriteLine(message); "Hello" Stack Heap } string message 1 2 3 4 5 6 "Ted" NaughtyStringMessage(string name) ref string message ref string name [Return address]
  • 30. public void NaughtyStringMessage(string name) { string name string message = "Hello "; message += name; Console.WriteLine(message); "Hello Ted" "Hello" Stack Heap } string message 1 2 3 4 5 6 string message "Ted" NaughtyStringMessage(string name) ref string message ref string name [Return address]
  • 31. public static event EventHandler UserLoggedOut; Main form MainForm.UserLoggedOut += HideFormData; Child form
  • 32. MainForm EventHandler UserLoggedOut ChildForm SecretDataWindow Loads Of Other Junk
  • 33. public class UnmanagedClass { ~UnmanagedClass() { //Clear up the unmanaged resources } }
  • 34. public class UnmanagedClass : IDisposable { public void Dispose() { //Clear up the unmanaged resources GC.SuppressFinalize(this); } ~UnmanagedClass() { this.Dispose(); } } Not a complete IDisposable implementation
  • 35.
  • 37.
  • 38.

Editor's Notes

  1. If you’re implementing using this, read http://stackoverflow.com/a/538238/1326403
  2. http://www.red-gate.com/memoryprofiler
  3. Warm up the application Take a baseline snapshot Run a small reproduction Take another snapshot Identify unexpected objects Eliminate cause for objects Repeat!
  4. Also worth reading: https://www.simple-talk.com/books/.net-books/under-the-hood-of-.net-memory-management/