.Net memory management ndc london

946 views

Published on

Slides from my NDC session Everything You Need To Know About .NET Memory, 4 December 2014.

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
946
On SlideShare
0
From Embeds
0
Number of Embeds
40
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

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

    1. 1. Everything* you need to know about .NET Memory Ben Emmett – NDC London – 4th December 2014 *Most stuff
    2. 2. Some of what I’m about to say is a lie
    3. 3. public void CaffeineCheck(int coffeesConsumed) { int coffeesRequired = 3; CaffeineAlert alert = new CaffeineAlert(); if (coffeesConsumed < coffeesRequired) { alert.Announce("Send help."); } }
    4. 4. The Stack CaffeineCheck (int coffeesRequired) int coffeesRequired int coffeesConsumed ..... [Return address] Alert() CaffeineCheck() ParentMethod() For each thread
    5. 5. eg: byte int char Pointers eg: strings classes objects arrays Stack Heap
    6. 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. 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. 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. 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. 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. 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. 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. 13. CaffeineAlert alert List<Contact> Recipients String Name String Email String Name String Email
    14. 14. Object reference Stack
    15. 15. Object reference Stack
    16. 16. Object reference Stack
    17. 17. Object reference Stack
    18. 18. Small Object Heap (SOH) The Heap Gen 2 Gen 1 Gen 0 Large Object Heap (LOH)
    19. 19. Address 1100k 1000k 900k 800k 700k 600k 500k 400k 300k 200k 100k 0
    20. 20. Address 1100k 1000k 900k 800k 700k 600k 500k 400k 300k 200k 100k 0
    21. 21. 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"
    22. 22. 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]
    23. 23. 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]
    24. 24. public static event EventHandler UserLoggedOut; Main form MainForm.UserLoggedOut += HideFormData; Child form
    25. 25. MainForm EventHandler UserLoggedOut ChildForm SecretDataWindow Loads Of Other Junk
    26. 26. public class UnmanagedClass { ~UnmanagedClass() { //Clear up the unmanaged resources } }
    27. 27. public class UnmanagedClass : IDisposable { public void Dispose() { //Clear up the unmanaged resources GC.SuppressFinalize(this); } ~UnmanagedClass() { this.Dispose(); } } Not a complete IDisposable implementation
    28. 28. 2013 Most awesome 
    29. 29. Questions? ben.emmett@red-gate.com @bcemmett slideshare.net/benemmett

    ×