SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

多治見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
 

Was ist angesagt? (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
 

Ähnlich wie .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
 

Ähnlich wie .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
 

Kürzlich hochgeladen

Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 

Kürzlich hochgeladen (20)

Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 

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

Hinweis der Redaktion

  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/