is2215 lecture5 lecturer_g_cand_classlibraries
TRANSCRIPT
![Page 1: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/1.jpg)
GARBAGE COLLECTIONAutomatic Memory Management
![Page 2: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/2.jpg)
![Page 3: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/3.jpg)
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
![Page 4: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/4.jpg)
Memory Allocation
Use the resources by accessing the class members
student.Name = txtName.Textstudent.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
![Page 5: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/5.jpg)
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
![Page 6: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/6.jpg)
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 Zero
Generation One
Generation Two
![Page 7: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/7.jpg)
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 the
manged heap Add bytes required for an object’s
overhead Each object has two overhead fields:
1.Method Table Pointer2.SyncBlockIndex
![Page 8: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/8.jpg)
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
![Page 9: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/9.jpg)
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
![Page 10: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/10.jpg)
Cont’d
As Garbage Collector starts it cleans up any objects that have been marked for cleaning
Three Objects B has lost its scope B is marked for collection Finally it is removed
A B C
A C
![Page 11: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/11.jpg)
System only allocates memory at the end
Job of garbage collector to compact the memory structure
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!!
A C
![Page 12: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/12.jpg)
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
![Page 13: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/13.jpg)
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
![Page 14: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/14.jpg)
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
![Page 15: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/15.jpg)
CLASS LIBRARIESSharing Classes
![Page 16: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/16.jpg)
Classes Outside Applications We have spent the last couple of
weeks creating classes within our application
How useful is that? A bit useful…
![Page 17: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/17.jpg)
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
![Page 18: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/18.jpg)
Creating a Class Library
![Page 19: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/19.jpg)
![Page 20: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/20.jpg)
Sample Class Library Code
![Page 21: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/21.jpg)
Change some properties
![Page 22: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/22.jpg)
Compile…
![Page 23: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/23.jpg)
Create New Project
![Page 24: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/24.jpg)
Add Form
![Page 25: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/25.jpg)
Add Reference
![Page 26: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/26.jpg)
![Page 27: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/27.jpg)
![Page 28: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/28.jpg)
![Page 29: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/29.jpg)
![Page 30: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/30.jpg)
![Page 31: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/31.jpg)
Using our Class Library
![Page 32: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/32.jpg)
![Page 33: Is2215 lecture5 lecturer_g_cand_classlibraries](https://reader036.vdocuments.mx/reader036/viewer/2022062303/554b9afab4c905764c8b4a69/html5/thumbnails/33.jpg)