SlideShare a Scribd company logo
Summer School 2012




Resources
& Memory
      Yuriy Guts
     R&D Engineer

  yuriy.guts@eleks.com
Summer School 2012




   .NET uses managed storage…
So why do I need do know this stuff?
Summer School 2012




Process memory layout (typical)
Summer School 2012




Storage management is hard
Summer School 2012




  Manual             vs.               Automatic
• Fast                           • Easy to program
• Precise                        • Keeps you focused
• Deterministic                  • Heuristically optimized

       BUT                                  BUT

• Error-prone                    • Unpredictable
• Distracting                    • Causes noticeable delays
• Hard to debug                  • Sometimes leaks as well
Summer School 2012




Common GC strategies

      Mark & Sweep

       Stop & Copy

    Reference Counting
Summer School 2012




Mark & Sweep
Summer School 2012




                 Advantages
• Keeps objects in their places
• Will work well with pointer arithmetics
Summer School 2012




               Disadvantages
• Can cause heap fragmentation
• Must process every object in the heap
• May require O(n) space to execute
Summer School 2012




Stop & Copy
Summer School 2012




                 Advantages
• Fast allocation and collection
• Operates only on live objects
• Does not cause fragmentation
Summer School 2012




              Disadvantages
• May require some time and space to rearrange
  pointers on live objects
• Unsuitable for C / C++
Summer School 2012




               Reference Counting
• Remember the number of references to each object
• Adjust it accordingly on every assignment
Summer School 2012




                Advantages
• Easy to implement
• Collects garbage incrementally without
  significant delays
Summer School 2012




              Disadvantages
• Unable to clean up circular structures
• Requires atomic operations (expensive)
• Slows down assignment operations
Summer School 2012




            ??      ?
Which approach does .NET use?
Summer School 2012




            .NET CLR Managed Heap
                                                             NextObjPtr




                                                                          Managed
 Object A   Object B   Object C    Object D   Object E   Object F
                                                                           heap



• Items are allocated consecutively.

• The only allocation overhead is about incrementing the pointer!

• When objects are destroyed, the managed heap gets automatically
  condensed (except the Large Object Heap!).
Summer School 2012




       CLR Object Lifecycle
Allocation (IL newobj)

   Construction (.ctor)

      Member usage (your code)

         Cleanup (finalizer)

            Deallocation (GC)
Object construction algorithm
                    Calculate the number of bytes required.



                     Append two system fields of type int.
           Type pointer                                SyncBlockIndex



 Make sure there is enough free storage space available in the managed heap.




  Fill the block with zero bytes, call the constructor (pass NextObjPtr as this).




     Increase NextObjPtr and return the address of newly created object.
Summer School 2012




Garbage Collection Triggers

1   • System is running low on memory




2   • A threshold of acceptable memory usage
      has been exceeded on the managed heap.



3   • GC.Collect() has been called
Summer School 2012




 Garbage Collection Algorithm
        Suspend all threads except the one that triggered GC

 Thread 1 [GC’ing]        Thread 2 [suspended]        Thread 3 [suspended]




                 Define application roots (live objects)

Stack roots          GC handles       Static objects       CPU registers



                            Mark & Sweep



               Heap Defragmentation (relocate objects).
Summer School 2012




GC Generations
Summer School 2012




 Deterministic Cleanup: IDisposable
[ComVisibleAttribute(true)]
public interface IDisposable
{
  void Dispose();
}



Dispose() — Performs application-defined tasks associated with freeing, releasing,
or resetting resources.
Summer School 2012




                ‘using’: syntactic sugar
using (Bitmap bitmap = new Bitmap(100, 100))
{
  Console.WriteLine(bitmap.Height);
}


                               …is equivalent to:
Bitmap bitmap = null;
try
{
  bitmap = new Bitmap(100, 100);
  Console.WriteLine(bitmap.Height);
}
finally
{
  if (bitmap != null)
  {
    (IDisposable)bitmap.Dispose();
  }
}
Summer School 2012




    Deterministic Cleanup: Finalization
Do NOT confuse with C++ destructor!!!

class UnmanagedResourceWrapper
{
  // Declare and use an unmanaged resource...

    ~UnmanagedResourceWrapper()
    {
      // Clean up...
    }
}
Summer School 2012
Summer School 2012




              Some Best Practices

1   • DON’T use IDisposable and/or Finalize unless you really have to (see below).
    • DON’T use Finalize if you do not have unmanaged resources (such as handles).



    • Use IDisposable if your type works with managed resources or contains an
2     IDisposable member.
    • Make sure you call base.Dispose() if base class is IDisposable.




3   • Use IDisposable AND Finalize if your type works with unmanaged resources.
    • Finalize() should always release the resource and not throw any exceptions.
Summer School 2012




 GCHandle Tricks

Lifetime monitoring & control


Pinning objects in memory


Weak references
Summer School 2012




   ??      ?
  Q&A
yuriy.guts@eleks.com
Summer School 2012




Thank you!

More Related Content

PPT
Coms30123 Synthesis 3 Projector
PPT
Speech Technology Overview
PPTX
OPTIAL FIBRE COMMUNICATION
PPTX
Digital speech processing lecture1
ODP
Optical drive
PPT
Unit – 2
PPTX
8251 USART
Coms30123 Synthesis 3 Projector
Speech Technology Overview
OPTIAL FIBRE COMMUNICATION
Digital speech processing lecture1
Optical drive
Unit – 2
8251 USART

Viewers also liked (7)

PPTX
Cryptography
PPTX
Encryption
PPTX
Speech synthesis technology
PPT
Introduction to computer hardware
PPT
Computer hardware component. ppt
ODP
Encryption presentation final
PPTX
Data communication and network Chapter -1
Cryptography
Encryption
Speech synthesis technology
Introduction to computer hardware
Computer hardware component. ppt
Encryption presentation final
Data communication and network Chapter -1
Ad

Similar to ELEKS Summer School 2012: .NET 04 - Resources and Memory (20)

PPTX
ELEKS Summer School 2012: .NET 06 - Multithreading
PPTX
ELEKS Summer School 2012: .NET 09 - Databases
PPT
Ios development
PDF
Pitfalls of Object Oriented Programming by SONY
PDF
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
PDF
A Quick and Dirty D3.js Tutorial
PDF
The Good, the Bad and the Ugly things to do with android
KEY
Dojo for programmers (TXJS 2010)
PDF
Web Development with Delphi and React - ITDevCon 2016
PDF
Mobile Fest 2018. Алексей Лизенко. Make your project great again
PPTX
Understanding memory management in xamarin forms
PPTX
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
PPTX
Chronicles Of Garbage Collection (GC)
PPT
Angular js meetup
PPT
angularjsmeetup-150303044616-conversion-gate01
PDF
A healthy diet for your Java application Devoxx France.pdf
PDF
Building React Applications with Redux
PPTX
Is2215 lecture5 lecturer_g_cand_classlibraries
KEY
MacRuby for Fun and Profit
PPTX
2CPP03 - Object Orientation Fundamentals
ELEKS Summer School 2012: .NET 06 - Multithreading
ELEKS Summer School 2012: .NET 09 - Databases
Ios development
Pitfalls of Object Oriented Programming by SONY
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
A Quick and Dirty D3.js Tutorial
The Good, the Bad and the Ugly things to do with android
Dojo for programmers (TXJS 2010)
Web Development with Delphi and React - ITDevCon 2016
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Understanding memory management in xamarin forms
Nagios Conference 2012 - Nathan Vonnahme - Monitoring the User Experience
Chronicles Of Garbage Collection (GC)
Angular js meetup
angularjsmeetup-150303044616-conversion-gate01
A healthy diet for your Java application Devoxx France.pdf
Building React Applications with Redux
Is2215 lecture5 lecturer_g_cand_classlibraries
MacRuby for Fun and Profit
2CPP03 - Object Orientation Fundamentals
Ad

More from Yuriy Guts (17)

PDF
Target Leakage in Machine Learning (ODSC East 2020)
PDF
Automated Machine Learning
PDF
Target Leakage in Machine Learning
PDF
Paraphrase Detection in NLP
PDF
UCU NLP Summer Workshops 2017 - Part 2
PDF
Natural Language Processing (NLP)
PDF
NoSQL (ELEKS DevTalks #1 - Jan 2015)
PDF
Experiments with Machine Learning - GDG Lviv
PDF
A Developer Overview of Redis
PDF
[JEEConf 2015] Lessons from Building a Modern B2C System in Scala
PDF
Redis for .NET Developers
PDF
Aspect-Oriented Programming (AOP) in .NET
PDF
Non-Functional Requirements
PDF
Introduction to Software Architecture
PDF
UML for Business Analysts
PDF
Intro to Software Engineering for non-IT Audience
PPTX
ELEKS DevTalks #4: Amazon Web Services Crash Course
Target Leakage in Machine Learning (ODSC East 2020)
Automated Machine Learning
Target Leakage in Machine Learning
Paraphrase Detection in NLP
UCU NLP Summer Workshops 2017 - Part 2
Natural Language Processing (NLP)
NoSQL (ELEKS DevTalks #1 - Jan 2015)
Experiments with Machine Learning - GDG Lviv
A Developer Overview of Redis
[JEEConf 2015] Lessons from Building a Modern B2C System in Scala
Redis for .NET Developers
Aspect-Oriented Programming (AOP) in .NET
Non-Functional Requirements
Introduction to Software Architecture
UML for Business Analysts
Intro to Software Engineering for non-IT Audience
ELEKS DevTalks #4: Amazon Web Services Crash Course

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Tartificialntelligence_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mushroom cultivation and it's methods.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
A comparative study of natural language inference in Swahili using monolingua...
Getting Started with Data Integration: FME Form 101
Tartificialntelligence_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
cloud_computing_Infrastucture_as_cloud_p
Building Integrated photovoltaic BIPV_UPV.pdf
1. Introduction to Computer Programming.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Spectroscopy.pptx food analysis technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
TLE Review Electricity (Electricity).pptx
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
Mushroom cultivation and it's methods.pdf

ELEKS Summer School 2012: .NET 04 - Resources and Memory

  • 1. Summer School 2012 Resources & Memory Yuriy Guts R&D Engineer yuriy.guts@eleks.com
  • 2. Summer School 2012 .NET uses managed storage… So why do I need do know this stuff?
  • 3. Summer School 2012 Process memory layout (typical)
  • 4. Summer School 2012 Storage management is hard
  • 5. Summer School 2012 Manual vs. Automatic • Fast • Easy to program • Precise • Keeps you focused • Deterministic • Heuristically optimized BUT BUT • Error-prone • Unpredictable • Distracting • Causes noticeable delays • Hard to debug • Sometimes leaks as well
  • 6. Summer School 2012 Common GC strategies Mark & Sweep Stop & Copy Reference Counting
  • 8. Summer School 2012 Advantages • Keeps objects in their places • Will work well with pointer arithmetics
  • 9. Summer School 2012 Disadvantages • Can cause heap fragmentation • Must process every object in the heap • May require O(n) space to execute
  • 11. Summer School 2012 Advantages • Fast allocation and collection • Operates only on live objects • Does not cause fragmentation
  • 12. Summer School 2012 Disadvantages • May require some time and space to rearrange pointers on live objects • Unsuitable for C / C++
  • 13. Summer School 2012 Reference Counting • Remember the number of references to each object • Adjust it accordingly on every assignment
  • 14. Summer School 2012 Advantages • Easy to implement • Collects garbage incrementally without significant delays
  • 15. Summer School 2012 Disadvantages • Unable to clean up circular structures • Requires atomic operations (expensive) • Slows down assignment operations
  • 16. Summer School 2012 ?? ? Which approach does .NET use?
  • 17. Summer School 2012 .NET CLR Managed Heap NextObjPtr Managed Object A Object B Object C Object D Object E Object F heap • Items are allocated consecutively. • The only allocation overhead is about incrementing the pointer! • When objects are destroyed, the managed heap gets automatically condensed (except the Large Object Heap!).
  • 18. Summer School 2012 CLR Object Lifecycle Allocation (IL newobj) Construction (.ctor) Member usage (your code) Cleanup (finalizer) Deallocation (GC)
  • 19. Object construction algorithm Calculate the number of bytes required. Append two system fields of type int. Type pointer SyncBlockIndex Make sure there is enough free storage space available in the managed heap. Fill the block with zero bytes, call the constructor (pass NextObjPtr as this). Increase NextObjPtr and return the address of newly created object.
  • 20. Summer School 2012 Garbage Collection Triggers 1 • System is running low on memory 2 • A threshold of acceptable memory usage has been exceeded on the managed heap. 3 • GC.Collect() has been called
  • 21. Summer School 2012 Garbage Collection Algorithm Suspend all threads except the one that triggered GC Thread 1 [GC’ing] Thread 2 [suspended] Thread 3 [suspended] Define application roots (live objects) Stack roots GC handles Static objects CPU registers Mark & Sweep Heap Defragmentation (relocate objects).
  • 22. Summer School 2012 GC Generations
  • 23. Summer School 2012 Deterministic Cleanup: IDisposable [ComVisibleAttribute(true)] public interface IDisposable { void Dispose(); } Dispose() — Performs application-defined tasks associated with freeing, releasing, or resetting resources.
  • 24. Summer School 2012 ‘using’: syntactic sugar using (Bitmap bitmap = new Bitmap(100, 100)) { Console.WriteLine(bitmap.Height); } …is equivalent to: Bitmap bitmap = null; try { bitmap = new Bitmap(100, 100); Console.WriteLine(bitmap.Height); } finally { if (bitmap != null) { (IDisposable)bitmap.Dispose(); } }
  • 25. Summer School 2012 Deterministic Cleanup: Finalization Do NOT confuse with C++ destructor!!! class UnmanagedResourceWrapper { // Declare and use an unmanaged resource... ~UnmanagedResourceWrapper() { // Clean up... } }
  • 27. Summer School 2012 Some Best Practices 1 • DON’T use IDisposable and/or Finalize unless you really have to (see below). • DON’T use Finalize if you do not have unmanaged resources (such as handles). • Use IDisposable if your type works with managed resources or contains an 2 IDisposable member. • Make sure you call base.Dispose() if base class is IDisposable. 3 • Use IDisposable AND Finalize if your type works with unmanaged resources. • Finalize() should always release the resource and not throw any exceptions.
  • 28. Summer School 2012 GCHandle Tricks Lifetime monitoring & control Pinning objects in memory Weak references
  • 29. Summer School 2012 ?? ? Q&A yuriy.guts@eleks.com