SlideShare a Scribd company logo
1 of 7
Classes, Objects, and References
It is important to further clarify the distinction between
classes, objects, and reference variables. Recall that a class is nothing more than a blueprint
that describes how an instance of this type will look and feel in memory. Classes, of course,
are defined within a code file (which in C# takes a *.cs extension by convention). Consider
the following simple Car class defined within a new C# Application project named SimpleGC:
// Car.cs
public class Car
{
public int CurrentSpeed {get; set;}
public string PetName {get; set;}
public Car(){}
public Car(string name, int speed)
{
PetName = name;
CurrentSpeed = speed;
}
public override string ToString()
{
return string.Format("{0} is going {1} MPH",
PetName, CurrentSpeed);
}
}
After a class has been defined, you may allocate any number of objects using the C# new
keyword. Understand, however, that the new keyword returns a reference to the object on
the heap, not the actual
object itself. If you declare the reference variable as a local variable in a method scope, it is
stored on the stack for further use in your application. When you want to invoke members
on the object, apply the C# dot operator to the stored reference, like so:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** GC Basics *****");
// Create a new Car object on
// the managed heap. We are
// returned a reference to this
// object ("refToMyCar").
Car refToMyCar = new Car("Zippy", 50);
// The C# dot operator (.) is used
// to invoke members on the object
// using our reference variable.
Console.WriteLine(refToMyCar.ToString());
Console.ReadLine();
}
}
Figure 13-1 illustrates the class, object, and reference relationship.
Figure 13-1. References to objects on the managed heap
structures are value types that are always allocated directly on the stack and
are never placed on the .NET managed heap. Heap allocation occurs only when you are
creating instances of classes.
The Basics of Object Lifetime
When you are building your C# applications, you are correct to assume that the .NET
runtime environment (a.k.a. the CLR) will take care of the managed heap without your
direct intervention. In fact, the golden rule of .NET memory management is simple:
Rule Allocate a class instance onto the managed heap using the new keyword and forget
about it.
Once instantiated, the garbage collector will destroy an object when it is no longer needed.
The next
obvious question, of course, is, “How does the garbage collector determine when an object
is no longer
needed?” The short (i.e., incomplete) answer is that the garbage collector removes an
object from the
heap only if it is unreachable by any part of your code base. Assume you have a method in
your Program
class that allocates a local Car object as follows:
static void MakeACar()
{
// If myCar is the only reference to the Car object,
// it *may* be destroyed when this method returns.
Car myCar = new Car();
}
Notice that this Car reference (myCar) has been created directly within the MakeACar()
method and
has not been passed outside of the defining scope (via a return value or ref/out
parameters). Thus, once
this method call completes, the myCar reference is no longer reachable, and the associated
Car object is
now a candidate for garbage collection. Understand, however, that you can’t guarantee that
this object
will be reclaimed from memory immediately after MakeACar() has completed. All you can
assume at this
point is that when the CLR performs the next garbage collection, the myCar object could be
safely
destroyed.
As you will most certainly discover, programming in a garbage-collected environment
greatly
simplifies your application development. In stark contrast, C++ programmers are painfully
aware that if
they fail to manually delete heap-allocated objects, memory leaks are never far behind. In
fact, tracking
down memory leaks is one of the most time-consuming (and tedious) aspects of
programming in
unmanaged environments. By allowing the garbage collector to take charge of destroying
objects, the
burden of memory management has been lifted from your shoulders and placed onto those
of the CLR.
The CIL of new
When the C# compiler encounters the new keyword, it emits a CIL newobj instruction into
the method implementation. If you compile the current example code and investigate the
resulting assembly using ildasm.exe, you’d find the following CIL statements within the
MakeACar() method:
.method private hidebysig static void MakeACar() cil managed
{
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] class SimpleGC.Car myCar)
IL_0000: nop
IL_0001: newobj instance void SimpleGC.Car::.ctor()
IL_0006: stloc.0
IL_0007: ret
} // end of method Program::MakeACar
Before we examine the exact rules that determine when an object is removed from the
managed heap, let’s check out the role of the CIL newobj instruction in a bit more detail.
First, understand that the managed heap is more than just a random chunk of memory
accessed by the CLR. The .NET garbage collector is quite a tidy housekeeper of the heap,
given that it will compact empty blocks of memory (when necessary) for purposes of
optimization. To aid in this endeavor, the managed heap maintains a pointer (commonly
referred to as the next object pointer or new object pointer) that identifies exactly where the
next object will be located. That said, the newobj instruction tells the CLR to perform the
following core operations:
• Calculate the total amount of memory required for the object to be allocated
(including the memory required by the data members and the base classes).
• Examine the managed heap to ensure that there is indeed enough room to host
the object to be allocated. If there is, the specified constructor is called and the
caller is ultimately returned a reference to the new object in memory, whose
address just happens to be identical to the last position of the next object pointer.
• Finally, before returning the reference to the caller, advance the next object
pointer to point to the next available slot on the managed heap.
Figure 13-2. The details of allocating objects onto the managed heap
As your application is busy allocating objects, the space on the managed heap may
eventually become full. When processing the newobj instruction, if the CLR determines that
the managed heap does not have sufficient memory to allocate the requested type, it will
perform a garbage collection in an attempt to free up memory. Thus, the next rule of
garbage collection is also quite simple:
Rule If the managed heap does not have sufficient memory to allocate a requested object, a
garbage collection will occur.
Exactly how this garbage collection occurs, however, depends on which version of the .NET
platform your application is running under.
The Role of Application Roots
How the garbage collector determines when an object is no longer needed. To understand the details,
you need to be aware of the notion of application roots. Simply put, a root is a storage location
containing a reference to an object on the managed heap. Strictly speaking, a root can fall into any of
the following categories:
• References to global objects (though these are not allowed in C#, CIL code does
permit allocation of global objects)
• References to any static objects/static fields
• References to local objects within an application’s code base
• References to object parameters passed into a method
• References to objects waiting to be finalized (described later in this chapter)
• Any CPU register that references an object
During a garbage collection process, the runtime will investigate objects on the managed heap to
determine whether they are still reachable (i.e., rooted) by the application. To do so, the CLR will build
an object graph, which represents each reachable object on the heap. Object graphs are explained in
some detail during the discussion of object serialization in Chapter 20. For now, just understand that
object graphs are used to document all reachable objects. As well, be aware that the garbage
collector will never graph the same object twice, thus avoiding the nasty circular reference count
found in COM programming.
Assume the managed heap contains a set of objects named A, B, C, D, E, F, and G. During a
garbage collection, these objects (as well as any internal object references they may contain) are
examined for active roots. After the graph has been constructed, unreachable objects (which we will
assume are objects C and F) are marked as garbage. Figure 13-3 diagrams a possible object graph
for the scenario just described (you can read the directional arrows using the phrase depends on or
requires,
for example,
E depends on G and B, A depends on nothing, and so on).
Figure 13-3. Object graphs are constructed to determine which objects are reachable by application roots
After objects have been marked for termination (C and F in this case—as they are not accounted for
in the object graph), they are swept from memory. At this point, the remaining space on the heap is
compacted, which in turn causes the CLR to modify the set of active application roots (and the
underlying pointers) to refer to the correct memory location (this is done automatically and
transparently). Last but not least, the next object pointer is readjusted to point to the next available
slot.
Figure 13-4 illustrates the resulting readjustment.
Understanding Object Generations
When the CLR is attempting to locate unreachable objects, is does not literally examine each and
every object placed on the managed heap. Doing so, obviously, would involve considerable time,
especially in larger (i.e., real-world) applications.
To help optimize the process, each object on the heap is assigned to a specific “generation.” The
idea behind generations is simple: the longer an object has existed on the heap, the more likely it is to
stay there. For example, the class that defined the main window of a desktop application will be in
memory until the program terminates. Conversely, objects that have only recently been placed on the
heap (such as an object allocated within a method scope) are likely to be unreachable rather quickly.
Given these assumptions, each object on the heap belongs to one of the following generations:
• Generation 0: Identifies a newly allocated object that has never been marked for
collection.
• Generation 1: Identifies an object that has survived a garbage collection (i.e., it was
marked for collection but was not removed due to the fact that the sufficient heap
space was acquired).
• Generation 2: Identifies an object that has survived more than one sweep of the
garbage collector.
Note Generations 0 and 1 are termed ephemeral generations. As explained in the next section, you
will see that the garbage collection process does treat ephemeral generations differently.
The garbage collector will investigate all generation 0 objects first. If marking and sweeping (or said
more plainly, getting rid of) these objects results in the required amount of free memory, any surviving
objects are promoted to generation 1. To see how an object’s generation affects the collection
process, ponder Figure 13-5, which diagrams how a set of surviving generation 0 objects (A, B, and
E) are promoted once the required memory has been reclaimed.
Figure 13-5. Generation 0 objects that survive a garbage collection are promoted to generation 1
If all generation 0 objects have been evaluated, but additional memory is still required, generation 1
objects are then investigated for reachability and collected accordingly. Surviving generation 1 objects
are then promoted to generation 2. If the garbage collector still requires additional memory,
generation 2 objects are evaluated. At this point, if a generation 2 object survives a garbage
collection, it remains a generation 2 object, given the predefined upper limit of object generations.
The bottom line is that by assigning a generational value to objects on the heap, newer objects (such
as local variables) will be removed quickly, while older objects (such as a program’s main Window)
are not “bothered” as often.

More Related Content

What's hot

Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: NotesRoberto Casadei
 
C questions
C questionsC questions
C questionsparm112
 
Practical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaPractical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaRoberto Casadei
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 
Making the most of lambdas
Making the most of lambdasMaking the most of lambdas
Making the most of lambdasRobert Zych
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsKanhaiya Saxena
 

What's hot (20)

Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Java GC
Java GCJava GC
Java GC
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
 
C questions
C questionsC questions
C questions
 
Practical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaPractical Aggregate Programming in Scala
Practical Aggregate Programming in Scala
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Applicative Functor
Applicative FunctorApplicative Functor
Applicative Functor
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Bab3of
Bab3ofBab3of
Bab3of
 
Making the most of lambdas
Making the most of lambdasMaking the most of lambdas
Making the most of lambdas
 
C++lecture9
C++lecture9C++lecture9
C++lecture9
 
Objective c
Objective cObjective c
Objective c
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objects
 

Similar to Memory management

01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptxssuser95922e
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesdannygriff1
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad ideaPVS-Studio
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Virtual Method Table and accident prevention
Virtual Method Table and accident preventionVirtual Method Table and accident prevention
Virtual Method Table and accident preventionAndrey Karpov
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 

Similar to Memory management (20)

01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibrariesIs2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibraries
 
Talking trash
Talking trashTalking trash
Talking trash
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 
Data structures
Data structuresData structures
Data structures
 
Virtual Method Table and accident prevention
Virtual Method Table and accident preventionVirtual Method Table and accident prevention
Virtual Method Table and accident prevention
 
Memory management in c++
Memory management in c++Memory management in c++
Memory management in c++
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
iOS Memory Management
iOS Memory ManagementiOS Memory Management
iOS Memory Management
 
Gc in android
Gc in androidGc in android
Gc in android
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 

Recently uploaded

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

Memory management

  • 1. Classes, Objects, and References It is important to further clarify the distinction between classes, objects, and reference variables. Recall that a class is nothing more than a blueprint that describes how an instance of this type will look and feel in memory. Classes, of course, are defined within a code file (which in C# takes a *.cs extension by convention). Consider the following simple Car class defined within a new C# Application project named SimpleGC: // Car.cs public class Car { public int CurrentSpeed {get; set;} public string PetName {get; set;} public Car(){} public Car(string name, int speed) { PetName = name; CurrentSpeed = speed; } public override string ToString() { return string.Format("{0} is going {1} MPH", PetName, CurrentSpeed); } } After a class has been defined, you may allocate any number of objects using the C# new keyword. Understand, however, that the new keyword returns a reference to the object on the heap, not the actual object itself. If you declare the reference variable as a local variable in a method scope, it is stored on the stack for further use in your application. When you want to invoke members on the object, apply the C# dot operator to the stored reference, like so: class Program { static void Main(string[] args) { Console.WriteLine("***** GC Basics *****"); // Create a new Car object on // the managed heap. We are // returned a reference to this // object ("refToMyCar"). Car refToMyCar = new Car("Zippy", 50); // The C# dot operator (.) is used // to invoke members on the object // using our reference variable. Console.WriteLine(refToMyCar.ToString()); Console.ReadLine();
  • 2. } } Figure 13-1 illustrates the class, object, and reference relationship. Figure 13-1. References to objects on the managed heap structures are value types that are always allocated directly on the stack and are never placed on the .NET managed heap. Heap allocation occurs only when you are creating instances of classes. The Basics of Object Lifetime When you are building your C# applications, you are correct to assume that the .NET runtime environment (a.k.a. the CLR) will take care of the managed heap without your direct intervention. In fact, the golden rule of .NET memory management is simple: Rule Allocate a class instance onto the managed heap using the new keyword and forget about it. Once instantiated, the garbage collector will destroy an object when it is no longer needed. The next obvious question, of course, is, “How does the garbage collector determine when an object is no longer needed?” The short (i.e., incomplete) answer is that the garbage collector removes an object from the heap only if it is unreachable by any part of your code base. Assume you have a method in your Program class that allocates a local Car object as follows: static void MakeACar() { // If myCar is the only reference to the Car object, // it *may* be destroyed when this method returns. Car myCar = new Car(); } Notice that this Car reference (myCar) has been created directly within the MakeACar() method and has not been passed outside of the defining scope (via a return value or ref/out parameters). Thus, once
  • 3. this method call completes, the myCar reference is no longer reachable, and the associated Car object is now a candidate for garbage collection. Understand, however, that you can’t guarantee that this object will be reclaimed from memory immediately after MakeACar() has completed. All you can assume at this point is that when the CLR performs the next garbage collection, the myCar object could be safely destroyed. As you will most certainly discover, programming in a garbage-collected environment greatly simplifies your application development. In stark contrast, C++ programmers are painfully aware that if they fail to manually delete heap-allocated objects, memory leaks are never far behind. In fact, tracking down memory leaks is one of the most time-consuming (and tedious) aspects of programming in unmanaged environments. By allowing the garbage collector to take charge of destroying objects, the burden of memory management has been lifted from your shoulders and placed onto those of the CLR. The CIL of new When the C# compiler encounters the new keyword, it emits a CIL newobj instruction into the method implementation. If you compile the current example code and investigate the resulting assembly using ildasm.exe, you’d find the following CIL statements within the MakeACar() method: .method private hidebysig static void MakeACar() cil managed { // Code size 8 (0x8) .maxstack 1 .locals init ([0] class SimpleGC.Car myCar) IL_0000: nop IL_0001: newobj instance void SimpleGC.Car::.ctor() IL_0006: stloc.0 IL_0007: ret } // end of method Program::MakeACar Before we examine the exact rules that determine when an object is removed from the managed heap, let’s check out the role of the CIL newobj instruction in a bit more detail. First, understand that the managed heap is more than just a random chunk of memory
  • 4. accessed by the CLR. The .NET garbage collector is quite a tidy housekeeper of the heap, given that it will compact empty blocks of memory (when necessary) for purposes of optimization. To aid in this endeavor, the managed heap maintains a pointer (commonly referred to as the next object pointer or new object pointer) that identifies exactly where the next object will be located. That said, the newobj instruction tells the CLR to perform the following core operations: • Calculate the total amount of memory required for the object to be allocated (including the memory required by the data members and the base classes). • Examine the managed heap to ensure that there is indeed enough room to host the object to be allocated. If there is, the specified constructor is called and the caller is ultimately returned a reference to the new object in memory, whose address just happens to be identical to the last position of the next object pointer. • Finally, before returning the reference to the caller, advance the next object pointer to point to the next available slot on the managed heap. Figure 13-2. The details of allocating objects onto the managed heap As your application is busy allocating objects, the space on the managed heap may eventually become full. When processing the newobj instruction, if the CLR determines that the managed heap does not have sufficient memory to allocate the requested type, it will perform a garbage collection in an attempt to free up memory. Thus, the next rule of garbage collection is also quite simple: Rule If the managed heap does not have sufficient memory to allocate a requested object, a garbage collection will occur. Exactly how this garbage collection occurs, however, depends on which version of the .NET platform your application is running under. The Role of Application Roots
  • 5. How the garbage collector determines when an object is no longer needed. To understand the details, you need to be aware of the notion of application roots. Simply put, a root is a storage location containing a reference to an object on the managed heap. Strictly speaking, a root can fall into any of the following categories: • References to global objects (though these are not allowed in C#, CIL code does permit allocation of global objects) • References to any static objects/static fields • References to local objects within an application’s code base • References to object parameters passed into a method • References to objects waiting to be finalized (described later in this chapter) • Any CPU register that references an object During a garbage collection process, the runtime will investigate objects on the managed heap to determine whether they are still reachable (i.e., rooted) by the application. To do so, the CLR will build an object graph, which represents each reachable object on the heap. Object graphs are explained in some detail during the discussion of object serialization in Chapter 20. For now, just understand that object graphs are used to document all reachable objects. As well, be aware that the garbage collector will never graph the same object twice, thus avoiding the nasty circular reference count found in COM programming. Assume the managed heap contains a set of objects named A, B, C, D, E, F, and G. During a garbage collection, these objects (as well as any internal object references they may contain) are examined for active roots. After the graph has been constructed, unreachable objects (which we will assume are objects C and F) are marked as garbage. Figure 13-3 diagrams a possible object graph for the scenario just described (you can read the directional arrows using the phrase depends on or requires, for example, E depends on G and B, A depends on nothing, and so on). Figure 13-3. Object graphs are constructed to determine which objects are reachable by application roots After objects have been marked for termination (C and F in this case—as they are not accounted for in the object graph), they are swept from memory. At this point, the remaining space on the heap is compacted, which in turn causes the CLR to modify the set of active application roots (and the underlying pointers) to refer to the correct memory location (this is done automatically and
  • 6. transparently). Last but not least, the next object pointer is readjusted to point to the next available slot. Figure 13-4 illustrates the resulting readjustment. Understanding Object Generations When the CLR is attempting to locate unreachable objects, is does not literally examine each and every object placed on the managed heap. Doing so, obviously, would involve considerable time, especially in larger (i.e., real-world) applications. To help optimize the process, each object on the heap is assigned to a specific “generation.” The idea behind generations is simple: the longer an object has existed on the heap, the more likely it is to stay there. For example, the class that defined the main window of a desktop application will be in memory until the program terminates. Conversely, objects that have only recently been placed on the heap (such as an object allocated within a method scope) are likely to be unreachable rather quickly. Given these assumptions, each object on the heap belongs to one of the following generations: • Generation 0: Identifies a newly allocated object that has never been marked for collection. • Generation 1: Identifies an object that has survived a garbage collection (i.e., it was marked for collection but was not removed due to the fact that the sufficient heap space was acquired). • Generation 2: Identifies an object that has survived more than one sweep of the garbage collector. Note Generations 0 and 1 are termed ephemeral generations. As explained in the next section, you will see that the garbage collection process does treat ephemeral generations differently. The garbage collector will investigate all generation 0 objects first. If marking and sweeping (or said more plainly, getting rid of) these objects results in the required amount of free memory, any surviving objects are promoted to generation 1. To see how an object’s generation affects the collection process, ponder Figure 13-5, which diagrams how a set of surviving generation 0 objects (A, B, and E) are promoted once the required memory has been reclaimed.
  • 7. Figure 13-5. Generation 0 objects that survive a garbage collection are promoted to generation 1 If all generation 0 objects have been evaluated, but additional memory is still required, generation 1 objects are then investigated for reachability and collected accordingly. Surviving generation 1 objects are then promoted to generation 2. If the garbage collector still requires additional memory, generation 2 objects are evaluated. At this point, if a generation 2 object survives a garbage collection, it remains a generation 2 object, given the predefined upper limit of object generations. The bottom line is that by assigning a generational value to objects on the heap, newer objects (such as local variables) will be removed quickly, while older objects (such as a program’s main Window) are not “bothered” as often.