SlideShare una empresa de Scribd logo
1 de 33
Automatic Memory Management

GARBAGE COLLECTION
 In order to set aside a piece of memory for our
  resource we use the new keyword

          Dim myStudent As New Student
 This create a   newobj instruction in the MSIL
  code generated from your application
 The constructor of class will then set up the
  initial state of the object

                      Sub New()
                        Course = 1
                       End Sub
Memory Allocation
 Use the resources by accessing the class members

     student.Name = txtName.Text
     student.studentID = txtID.Text

 Traditionally the programmer had to manually free up
  system resources
 For example in C++ you use a special Destructor
 Programmers often forget to kill objects, or try to
  access a piece of memory that has already been wiped

            myStudent = Nothing
Garbage Collection in .NET

 Garbage Collection is operating as a separate
  thread in the background
 GC itself requires resources to run
 It is assigned low priority
 Running out of memory?
   Garbage Collection given REALTIME priority and
    collect all unwanted objects
How does it Locate
Garbage?
 When an application is loaded a portion of
  memory is assigned to that particular
  program.
 This piece of memory is called the managed
  heap.
 The memory is seperated into three parts:


Generation      Generation       Generation
Zero            One              Two
Memory Allocation

   When you create an object using the new contructor
   Newobj called in MSIL
   When it executes:
       Calculates the number of bytes for the object or type
        needs to be loaded into themanged heap
       Add bytes required for an object’s
        overhead
       Each object has two overhead fields:

        1. Method Table Pointer
        2. SyncBlockIndex
Memory Allocation Cont’d

   CLR checks to see if the bytes required
    to allocate the object are available
   If it fits it is allocated at the address
    pointed to by NextObjPtr
   The address of the object is returned
   NextObjPtr navigates past the
    object and finds where the next object
    will be placed in the heap
Cont’d
 All these processes happen in Generation
  Zero level
 When generation zero does not have enough
  space to allocate to other objects
 Garbage Collector given real time priority
 Garbage Collector will monitor level zero again
  to check objects scope
 It will mark items that are no longer needed
Cont’d

   As Garbage Collector starts it cleans up any objects
    that have been marked for cleaning


      AB C

   Three Objects
   B has lost its scope
   B is marked for collection
   Finally it is removed
      A     C
 System only allocates memory at the end
 Job of garbage collector to compact the memory
  structure

      A    C

 Garbage collection has not ended
 Looks at all the objects that have survived the sweep
 Those objects will be moved to Generation one
 Generation Zero can be used to store new objects.
 If Generation One doesn’t have enough space the
  process will be carried out there too!!
Problems with Garbage
Collection
 Garbage Collection is run non-
  deterministically
 In VB 6.0 you could set a object = Nothing and
  the Class Terminate Event was raised.
 In VB .NET you can still set an object =
  Nothing, but this will not actually kill your
  object it will only mark it for cleaning
GC Cont’d

 If you don’t know when GC is going to run
  how can you clear up extra resources
  associate with a class instance?
 Extra Resources such as files, network
  connections, database connections are not
  handled very well by garbage collection
 Invoke the Sub Finalize
Finilize

 This is the method that the Garbage Collector must
  run on the object to clear any unmanaged
  resources, prior to reclaiming memory used by the
  object
 By default the finilize method doesn’t do anything
 We can override it and put in code to explicitly clean
  up unmanaged resources
Sharing Classes

CLASS LIBRARIES
Classes Outside Applications

 We have spent the last couple of weeks
  creating classes within our application
 How useful is that?
 A bit useful…
Seperating Classes from your
Application
 You can place your project in a class library
 This is a project that gets compiled into a DLL
 You can then reference it from another
  project and access its functionality
 It allows you to reuse code
Creating a Class Library
Sample Class Library Code
Change some properties
Compile…
Create New Project
Add Form
Add Reference
Using our Class Library
Is2215 lecture5 lecturer_g_cand_classlibraries
Is2215 lecture5 lecturer_g_cand_classlibraries

Más contenido relacionado

La actualidad más candente

Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchasAlec Tucker
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Larry Nung
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage CollectionAbhishek Sur
 
Object Repository
Object RepositoryObject Repository
Object RepositoryG.C Reddy
 

La actualidad más candente (6)

Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchas
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
 
Introduction of c# day2
Introduction of c# day2Introduction of c# day2
Introduction of c# day2
 
RxSubject And Operators
RxSubject And OperatorsRxSubject And Operators
RxSubject And Operators
 
Object Repository
Object RepositoryObject Repository
Object Repository
 

Destacado

Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesdannygriff1
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)dannygriff1
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)dannygriff1
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introdannygriff1
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessdannygriff1
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1dannygriff1
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)dannygriff1
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)dannygriff1
 
Learn VB.NET at ASIT
Learn VB.NET at ASITLearn VB.NET at ASIT
Learn VB.NET at ASITASIT
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)dannygriff1
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npvdannygriff1
 

Destacado (12)

Is2215 lecture8 relational_databases
Is2215 lecture8 relational_databasesIs2215 lecture8 relational_databases
Is2215 lecture8 relational_databases
 
Is2215 lecture4 student (1)
Is2215 lecture4 student (1)Is2215 lecture4 student (1)
Is2215 lecture4 student (1)
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
 
Is2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_introIs2215 lecture7 lecturer_ado_intro
Is2215 lecture7 lecturer_ado_intro
 
Is2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_accessIs2215 lecture6 lecturer_file_access
Is2215 lecture6 lecturer_file_access
 
Stocks&bonds2214 1
Stocks&bonds2214 1Stocks&bonds2214 1
Stocks&bonds2214 1
 
Mcq sample
Mcq sampleMcq sample
Mcq sample
 
Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)Ec2204 tutorial 2(2)
Ec2204 tutorial 2(2)
 
Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)Ec2204 tutorial 6(1)
Ec2204 tutorial 6(1)
 
Learn VB.NET at ASIT
Learn VB.NET at ASITLearn VB.NET at ASIT
Learn VB.NET at ASIT
 
Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)Ec2204 tutorial 4(1)
Ec2204 tutorial 4(1)
 
Profitability&npv
Profitability&npvProfitability&npv
Profitability&npv
 

Similar a Is2215 lecture5 lecturer_g_cand_classlibraries

01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptxssuser95922e
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxPavanKumarPNVS
 
Constructors_this keyword_garbage.pptx
Constructors_this keyword_garbage.pptxConstructors_this keyword_garbage.pptx
Constructors_this keyword_garbage.pptxrevathi s
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection TechniquesAn Khuong
 
Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionDiya Singh
 
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
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4aminmesbahi
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5aminmesbahi
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdfSudhanshiBakre1
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Techizzaa
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Kuntal Bhowmick
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 

Similar a Is2215 lecture5 lecturer_g_cand_classlibraries (20)

Memory management
Memory managementMemory management
Memory management
 
01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
 
Thread in java.pptx
Thread in java.pptxThread in java.pptx
Thread in java.pptx
 
Talking trash
Talking trashTalking trash
Talking trash
 
Constructors_this keyword_garbage.pptx
Constructors_this keyword_garbage.pptxConstructors_this keyword_garbage.pptx
Constructors_this keyword_garbage.pptx
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage Collection
 
7494609
74946097494609
7494609
 
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
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
C# basics
C# basicsC# basics
C# basics
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5.NET Core, ASP.NET Core Course, Session 5
.NET Core, ASP.NET Core Course, Session 5
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 

Más de dannygriff1

Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)dannygriff1
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)dannygriff1
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)dannygriff1
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopolydannygriff1
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysisdannygriff1
 
4 production and cost
4  production and cost4  production and cost
4 production and costdannygriff1
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choicedannygriff1
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticitydannygriff1
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firmdannygriff1
 
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)Is2215 lecture3 student (1)
Is2215 lecture3 student (1)dannygriff1
 

Más de dannygriff1 (15)

Risk08a
Risk08aRisk08a
Risk08a
 
Npvrisk
NpvriskNpvrisk
Npvrisk
 
Npv2214(1)
Npv2214(1)Npv2214(1)
Npv2214(1)
 
Irr(1)
Irr(1)Irr(1)
Irr(1)
 
Npv rule
Npv ruleNpv rule
Npv rule
 
Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)Ec2204 tutorial 8(2)
Ec2204 tutorial 8(2)
 
Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)Ec2204 tutorial 3(1)
Ec2204 tutorial 3(1)
 
Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)Ec2204 tutorial 1(2)
Ec2204 tutorial 1(2)
 
6 price and output determination- monopoly
6 price and output determination- monopoly6 price and output determination- monopoly
6 price and output determination- monopoly
 
5 industry structure and competition analysis
5  industry structure and competition analysis5  industry structure and competition analysis
5 industry structure and competition analysis
 
4 production and cost
4  production and cost4  production and cost
4 production and cost
 
3 consumer choice
3 consumer choice3 consumer choice
3 consumer choice
 
2 demand-supply and elasticity
2  demand-supply and elasticity2  demand-supply and elasticity
2 demand-supply and elasticity
 
1 goals of the firm
1  goals of the firm1  goals of the firm
1 goals of the firm
 
Is2215 lecture3 student (1)
Is2215 lecture3 student (1)Is2215 lecture3 student (1)
Is2215 lecture3 student (1)
 

Is2215 lecture5 lecturer_g_cand_classlibraries

  • 2.
  • 3.  In order to set aside a piece of memory for our resource we use the new keyword Dim myStudent As New Student  This create a newobj instruction in the MSIL code generated from your application  The constructor of class will then set up the initial state of the object Sub New() Course = 1 End Sub
  • 4. Memory Allocation  Use the resources by accessing the class members student.Name = txtName.Text student.studentID = txtID.Text  Traditionally the programmer had to manually free up system resources  For example in C++ you use a special Destructor  Programmers often forget to kill objects, or try to access a piece of memory that has already been wiped myStudent = Nothing
  • 5. Garbage Collection in .NET  Garbage Collection is operating as a separate thread in the background  GC itself requires resources to run  It is assigned low priority  Running out of memory?  Garbage Collection given REALTIME priority and collect all unwanted objects
  • 6. How does it Locate Garbage?  When an application is loaded a portion of memory is assigned to that particular program.  This piece of memory is called the managed heap.  The memory is seperated into three parts: Generation Generation Generation Zero One Two
  • 7. Memory Allocation  When you create an object using the new contructor  Newobj called in MSIL  When it executes:  Calculates the number of bytes for the object or type needs to be loaded into themanged heap  Add bytes required for an object’s overhead  Each object has two overhead fields: 1. Method Table Pointer 2. SyncBlockIndex
  • 8. Memory Allocation Cont’d  CLR checks to see if the bytes required to allocate the object are available  If it fits it is allocated at the address pointed to by NextObjPtr  The address of the object is returned  NextObjPtr navigates past the object and finds where the next object will be placed in the heap
  • 9. Cont’d  All these processes happen in Generation Zero level  When generation zero does not have enough space to allocate to other objects  Garbage Collector given real time priority  Garbage Collector will monitor level zero again to check objects scope  It will mark items that are no longer needed
  • 10. Cont’d  As Garbage Collector starts it cleans up any objects that have been marked for cleaning AB C  Three Objects  B has lost its scope  B is marked for collection  Finally it is removed A C
  • 11.  System only allocates memory at the end  Job of garbage collector to compact the memory structure A C  Garbage collection has not ended  Looks at all the objects that have survived the sweep  Those objects will be moved to Generation one  Generation Zero can be used to store new objects.  If Generation One doesn’t have enough space the process will be carried out there too!!
  • 12. Problems with Garbage Collection  Garbage Collection is run non- deterministically  In VB 6.0 you could set a object = Nothing and the Class Terminate Event was raised.  In VB .NET you can still set an object = Nothing, but this will not actually kill your object it will only mark it for cleaning
  • 13. GC Cont’d  If you don’t know when GC is going to run how can you clear up extra resources associate with a class instance?  Extra Resources such as files, network connections, database connections are not handled very well by garbage collection  Invoke the Sub Finalize
  • 14. Finilize  This is the method that the Garbage Collector must run on the object to clear any unmanaged resources, prior to reclaiming memory used by the object  By default the finilize method doesn’t do anything  We can override it and put in code to explicitly clean up unmanaged resources
  • 16. Classes Outside Applications  We have spent the last couple of weeks creating classes within our application  How useful is that?  A bit useful…
  • 17. Seperating Classes from your Application  You can place your project in a class library  This is a project that gets compiled into a DLL  You can then reference it from another project and access its functionality  It allows you to reuse code
  • 18. Creating a Class Library
  • 19.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. Using our Class Library

Notas del editor

  1. Public Class clsMyClass Private strName as String Public Property Name as String Get Return str
  2. Public Class clsMyClass Private strName as String Public Property Name as String Get Return str