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

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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
 
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.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
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 Modelsaagamshah0812
 
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
 
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
 

Dernier (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
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 ...
 
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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
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...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
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
 
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
 
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)
 

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