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

Contenu connexe

Tendances

多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy GrailsTsuyoshi Yamamoto
 
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 editionEleanor McHugh
 
Session 02 python basics
Session 02 python basicsSession 02 python basics
Session 02 python basicsbodaceacat
 
DevOpsCon 2021: Go Web Development 101
DevOpsCon 2021: Go Web Development 101DevOpsCon 2021: Go Web Development 101
DevOpsCon 2021: Go Web Development 101Jan Stamer
 
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 101Jan Stamer
 
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 ÖkosystemJan Stamer
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my dayTor Ivry
 
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 PerspectiveEleanor McHugh
 
Scala - den smarta kusinen
Scala - den smarta kusinenScala - den smarta kusinen
Scala - den smarta kusinenRedpill Linpro
 
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)Kazuki Yoshida
 
Tcl2012 8.6 Changes
Tcl2012 8.6 ChangesTcl2012 8.6 Changes
Tcl2012 8.6 Changeshobbs
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014"PostgreSQL and Python" Lightning Talk @EuroPython2014
"PostgreSQL and Python" Lightning Talk @EuroPython2014Henning Jacobs
 

Tendances (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
 

Similaire à .Net memory management ndc london

KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxIan Robertson
 
GeeCON 2014 - Functional Programming without Lambdas
GeeCON 2014 - Functional Programming without LambdasGeeCON 2014 - Functional Programming without Lambdas
GeeCON 2014 - Functional Programming without LambdasMattias Severson
 
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 20160116Paulo Morgado
 
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 6Moaid Hathot
 
Why Scala is the better Java
Why Scala is the better JavaWhy Scala is the better Java
Why Scala is the better JavaThomas Kaiser
 
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 2014Scott Wlaschin
 
Linq - an overview
Linq - an overviewLinq - an overview
Linq - an overviewneontapir
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
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 examplesNebojša Vukšić
 
Kotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresKotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresiMasters
 

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

Dernier

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
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 ...harshavardhanraghave
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 

Dernier (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
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 ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

.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.

Notes de l'éditeur

  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/