dot net unit i language
Post on 03-Apr-2018
218 Views
Preview:
TRANSCRIPT
-
7/28/2019 Dot Net Unit i Language
1/24
VB Language
Visual Basic, the name makes me feel that it is something special. In the History ofComputing world no other product sold more copies than Visual Basic did. Such is
the importance of that language which clearly states how widely it is used fordeveloping applications. Visual Basic is very popular for it's friendly working
(graphical) environment. Visual Basic. NET is an extension of Visual Basic
programming language with many new features in it. The changes from VB to VB.NET are huge, ranging from the change in syntax of the language to the types of
projects we can create now and the way we design applications. Visual Basic .NETwas designed to take advantage of the .NET Framework base classes and runtime
environment. It comes with power packed features that simplify application
development.
Briefly on some changes:
The biggest change from VB to VB .NET is, VB .NET is Object-Oriented now. VB.NET now supports all the key OOP features like Inheritance, Polymorphism,
Abstraction and Encapsulation. We can now create classes and objects, deriveclasses from other classes and so on. The major advantage of OOP is code reusability
The Command Button now is Button and the TextBox is TextBox instead of Textas in VB6
Many new controls have been added to the toolbar to make applicationdevelopment more efficient
VB .NET now adds Console Applications to it apart from Windows and WebApplications. Console applications are console oriented applications that run in the
DOS version
All the built-in VB functionality now is encapsulated in a Namespace (collection ofdifferent classes) called System
New keywords are added and old one's are either removed or renamed
VB .NET is strongly typed which means that we need to declare all the variables bydefault before using them
VB .NET now supports structured exception handling using Try...Catch...Finallysyntax
The syntax for procedures is changed. Get and Let are replaced by Get and SetEvent handling procedures are now passed only two parameters
The way we handle data with databases is changed as well. VB .NET now uses ADO.NET, a new data handling model to communicate with databases on local machines
or on a network and also it makes handling of data on the Internet easy. All the datain ADO .NET is represented in XML format and is exchanged in the same format.
Representing data in XML format allows us for sending large amounts of data on theInternet and it also reduces network traffic when communicating with the database
VB .NET now supports Multithreading. A threaded application allows to do number
of different things at once, running different execution threads allowing to usesystem resources
Web Development is now an integral part of VB .NET making Web Forms and Web
Services two major types of applications
Namespaces
A namespace is a collection of different classes. All VB applications are developed
using classes from the .NET System namespace. The namespace with all the built-in
http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/ -
7/28/2019 Dot Net Unit i Language
2/24
VB functionality is the System namespace. All other namespaces are based on thisSystem namespace.
Some Namespaces and their use:
System: Includes essential classes and base classes for commonly used data types,
events, exceptions and so onSystem.Collections: Includes classes and interfaces that define various collection of
objects such as list, queues,hash tables, arrays, etc
System.Data: Includes classes which lets us handle data from data sourcesSystem.Data.OleDb: Includes classes that support the OLEDB .NET provider
System.Data.SqlClient: Includes classes that support the SQL Server .NET providerSystem.Diagnostics: Includes classes that allow to debug our application and to step
through our codeSystem.Drawing: Provides access to drawing methods
System.Globalization: Includes classes that specify culture-related informationSystem.IO: Includes classes for data access with Files
System.Net: Provides interface to protocols used on the internetSystem.Reflection: Includes classes and interfaces that return information about
types, methods and fieldsSystem.Security: Includes classes to support the structure of common language
runtime security systemSystem.Threading: Includes classes and interfaces to support multithreaded
applicationsSystem.Web: Includes classes and interfaces that support browser-server
communication
System.Web.Services: Includes classes that let us build and use Web ServicesSystem.Windows.Forms: Includes classes for creating Windows based forms
System.XML: Includes classes for XML support
Assemblies
An assembly is the building block of a .NET application. It is a self describing
collection of code, resources, and metadata (data about data, example, name, size,version of a file is metadata about that file). An Assembly is a complied and
versioned collection of code and metadata that forms an atomic functional unit.Assemblies take the form of a dynamic link library (.dll) file or executable program
file (.exe) but they differ as they contain the information found in a type library andthe information about everything else needed to use an application or component.
All .NET programs are constructed from these Assemblies. Assemblies are made oftwo parts: manifest, contains information about what is contained within the
assembly and modules, internal files of IL code which are ready to run. When
programming, we don't directly deal with assemblies as the CLR and the .NETframework takes care of that behind the scenes. The assembly file is visible in theSolution Explorer window of the project.
An assembly includes:
o Information for each public class or type used in the assembly information
includes class or type names, the classes from which an individual class is
derived, etc
http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/http://www.startvbdotnet.com/language/ -
7/28/2019 Dot Net Unit i Language
3/24
o Information on all public methods in each class, like, the method name and
return values (if any)
o Information on every public parameter for each method like the parameter's
name and type
o Information on public enumerations including names and values
o Information on the assembly version (each assembly has a specific version
number)o Intermediate language code to execute
o A list of types exposed by the assembly and list of other assemblies required
by the assembly
Image of a Assembly file is displayed below.
-
7/28/2019 Dot Net Unit i Language
4/24
The System.Net namespace provides a simple programming interface for many of the protocols used on
networks today. TheWebRequest and WebResponse classes form the basis of what are called pluggable
protocols, an implementation of network services that enables you to develop applications that use
Internet resources without worrying about the specific details of the individual protocols.
Introduction
Microsoft .NET provides several ways to think of your code as more than just a bunch of disconnectedlines. As a Visual Basic programmer, you're already familiar with the concept of a class, a section of codethat defines an object and its behavior. But two of the higher-level groupings may be unfamiliar to you:
An assembly provides a fundamental unit of physical code grouping.
A namespace provides a fundamental unit of logical code grouping.
As you'll see in this document, you can use Visual Basic .NET to create both assemblies and namespaces.
You'll need to understand both of these concepts to be a productive Visual Basic .NET developer.
Assemblies
An assembly is a collection of types and resources that forms a logical unit of functionality. All types in the.NET Framework must exist in assemblies; the common language runtime does not support types outsideof assemblies. Each time you create a Microsoft Windows Application, Windows Service, Class Library, orother application with Visual Basic .NET, you're building a single assembly. Each assembly is stored asan .exe or .dll file.
Note Although it's technically possible to create assemblies that span multiple files, you're not likely to
use this technology in most situations.
The .NET Framework uses assemblies as the fundamental unit for several purposes:
Security
Type Identity
Reference Scope
Versioning
Deployment
Security
An assembly is the unit at which security permissions are requested and granted. Assemblies are also thelevel at which you establish identity and trust. The .NET Framework provides two mechanisms for thislevel of assembly security: strong names and Signcode.exe. You can also manage security by specifyingthe level of trust for code from a particular site or zone.
Signing an assembly with a strong name adds public key encryption to the assembly. This ensures nameuniqueness and prevents substituting another assembly with the same name for the assembly that youprovided.
The signcode.exe tool embeds a digital certificate in the assembly. This allows users of the assembly toverify the identity of the assembly's developer by using a public or private trust hierarchy.
You can choose to use either strong names, Signcode.exe, or both, to strengthen the identity of yourassembly.
The common language runtime also uses internal hashing information, in conjunction with strong namesand signcode, to verify that the assembly being loaded has not been altered after it was built.
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspxhttp://msdn.microsoft.com/en-us/library/system.net.webrequest.aspxhttp://msdn.microsoft.com/en-us/library/system.net.webresponse.aspxhttp://msdn.microsoft.com/en-us/library/system.net.webrequest.aspxhttp://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx -
7/28/2019 Dot Net Unit i Language
5/24
Type Identity
The identity of a type depends on the assembly where that type is defined. That is, if you define a typenamed DataStore in one assembly, and a type named DataStore in another assembly, the .NETFramework can tell them apart because they are in two different assemblies. Of course you can't definetwo different types with the same name in the same assembly.
Reference Scope
The assembly is also the location of reference information in general. Each assembly contains informationon references in two directions:
The assembly contains metadata that specifies the types and resources within the assembly that
are exposed to code outside of the assembly. For example, a particular assembly could expose a
public type named Customer with a public property named AccountBalance.
The assembly contains metadata specifying the other assemblies on which it depends. For
example, a particular assembly might specify that it depends on the System.Windows.Forms.dll
assembly.
Versioning
Each assembly has a 128-bit version number that is presented as a set of four decimal pieces:Major.Minor.Build.Revision
For example, an assembly might have the version number 3.5.0.126.
By default, an assembly will only use types from the exact same assembly (name and version number)
that it was built and tested with. That is, if you have an assembly that uses a type from version 1.0.0.2 ofanother assembly, it will (by default) not use the same type from version 1.0.0.4 of the other assembly.This use of both name and version to identify referenced assemblies helps avoid the "DLL Hell" problem ofupgrades to one application breaking other applications.
Tip An administrator or developer can use configuration files to relax this strict version checking. Look
for information on publisher policy in the .NET Framework Developer's Guide.
Deployment
Assemblies are the natural unit of deployment. The Windows Installer Service 2.0 can install individual
assemblies as part of a larger setup program. You can also deploy assemblies in other ways, including bya simple xcopy to the target system or via code download from a web site. When you start an application,it loads other assemblies as a unit as types and resources from those assemblies are needed.
The Assembly Manifest
Every assembly contains an assembly manifest, a set of metadata with information about the assembly.The assembly manifest contains these items:
The assembly name and version
The culture or language the assembly supports (not required in all assemblies)
The public key for any strong name assigned to the assembly (not required in all assemblies)
A list of files in the assembly with hash information
Information on exported types
Information on referenced assemblies
The Global Assembly Cache
Assemblies can be either private or shared. By default, assemblies are private, and types contained withinthose assemblies are only available to applications in the same directory as the assembly. But everycomputer with the .NET Framework installed also has a global assembly cache (GAC) containing
http://msdn.microsoft.com/en-us/library/dz32563a.aspxhttp://msdn.microsoft.com/en-us/library/dz32563a.aspx -
7/28/2019 Dot Net Unit i Language
6/24
assemblies that are designed to be shared by multiple applications. There are three ways to add anassembly to the GAC:
Install them with the Windows Installer 2.0
Use the Gacutil.exe tool
Drag and drop the assemblies to the cache with Windows Explorer
Note that in most cases you should plan to install assemblies to the GAC on end-user computers by usingthe Windows Installer. The gacutil.exe tool and the drag and drop method exist for use during thedevelopment cycle. You can view the contents of your global assembly cache by using Windows Explorerto navigate to the WINNT\assembly folder, as shown in Figure 1.
Figure 1. Viewing the Global Assembly Cache in Windows Explorer
Assembly Info.vb
When you create a new project using Visual Basic .NET, one of the components of the project will be a filenamed AssemblyInfo.vb. This file contains all of the assembly attributes that describe the assembly. Tocharacterize your assembly, you should customize this information. You can edit the AssemblyInfo.vb file
just like any other Visual Basic .NET source file. The default contents of this file look like this:
Copy Code
Imports System.Reflection
Imports System.Runtime.InteropServices
' General Information about an assembly is controlled through
the following
' set of attributes. Change these attribute values to modify
the information
' associated with an assembly.
-
7/28/2019 Dot Net Unit i Language
7/24
' Review the values of the assembly attributes
'The following GUID is for the ID of the typelib if this project
is exposed to COM
' Version information for an assembly consists of the following
four values:
'
' Major Version
' Minor Version
' Build Number
' Revision
'
' You can specify all the values or you can default the Build
and Revision Numbers
' by using the '*' as shown below:
Namespaces
Another way to organize your Visual Basic .NET code is through the use of namespaces. Namespaces arenot a replacement for assemblies, but a second organizational method that complements assemblies.Namespaces are a way of grouping type names and reducing the chance of name collisions. A namespacecan contain both other namespaces and types. The full name of a type includes the combination of
namespaces that contain that type.
The Namespace Hierarchy and Fully-Qualified Names
You're probably already familiar with namespaces from the .NET Framework Class Library. For example,
the Button type is contained in the System.Windows.Forms namespace. That's actually shorthand for thesituation shown in Figure 2, shows that the Button class is contained in the Forms namespace that iscontained in the Windows namespace that is contained in the root System namespace.
-
7/28/2019 Dot Net Unit i Language
8/24
Figure 2. A namespace and class hierarchy
The fully qualified name of a class is constructed by concatenating the names of all the namespaces that
contain the type. For example, the fully qualified name of the Button class isSystem.Windows.Forms.Button. The namespace hierarchy helps distinguish types with the same name
from one another. For example, you might define your own class named Button, but it might be containedin the ControlPanel Namespace within the PowerPlant namespace, making its fully qualified namePowerPlant.ControlPanel.Button.
Tip There's no need to use fully qualified names in your code unless you need to resolve an ambiguity
between two types with the same type name used in the same project.
Declaring Namespaces
You can use the Namespace statement to declare a namespace in your own code. Namespace statementscan be nested. For example, a Visual Basic .NET module could contain these lines of code:
Copy Code
Namespace PowerPlant
Namespace ControlPanel
Public Class Button
' Statements to implement Button
End Class
End Namespace
End Namespace
An alternative way to express this same hierarchy is to combine the Namespace statements:
Copy Code
Namespace PowerPlant.ControlPanel
Public Class Button
' Statements to implement Button
End Class
End Namespace
By default, a Visual Basic .NET project declares a root namespace that has the same name as the project.If the above declaration was in a project called PowerLib, then the fully qualified name of the Button class
would be PowerLib.PowerPlant.ControlPanel.Button. You can change the name of the root namespace byfollowing these steps:
-
7/28/2019 Dot Net Unit i Language
9/24
1. In Project Explorer, right-click the project and select Properties.
2. Click Common Properties.
3. Enter a new name for the Root Namespace. It's good practice to use a name such as
CompanyName.Technology for the Root Namespace, to avoid conflicts with namespaces
defined by other developers.
4. Click OK.
Note Strictly speaking, assemblies and namespaces are orthogonal. That is, you can declare
members of a single namespace across multiple assemblies, or declare multiple namespaces in a
single assembly. Unless you have a good reason for such an arrangement, though, it's best to
keep things simple with one namespace per assembly and vice versa.
Practice Creating an Assembly
In the following example, you'll create a class library containing a class that exposes a single method anda single event. Then you'll use assembly attributes to customize the assembly information.
Create the Class Library
Follow these steps to create the class library that will raise the events:
1. Open Microsoft Visual Studio .NET, click Start, and then click New Project.
2. In the left pane tree view, select Visual Basic Project.
3. Select Class Library as the project template.
4. Set the name of the application to PowerLib and click OK.
5. In Solution Explorer, highlight the class called Class1.vb and rename it to Button.vb.
6. Select the code for Class1 in Button.vb (this be an empty class definition) and replace it with the
following code:
Copy Code
Public Class Button
Private mfState As Boolean
Public Sub Toggle()
mfState = Not mfState
End Sub
Public Property State() As Boolean
Get
State = mfState
End Get
Set(ByVal Value As Boolean)
mfState = Value
End Set
-
7/28/2019 Dot Net Unit i Language
10/24
End Property
End Class
Customize the Assembly
Follow these steps to customize the Assembly attributes:
1. In Solution Explorer, double-click the AssemblyInfo.vb file to open it in the code editor.
2. Select the first block of assembly attributes and modify them as follows:
Copy Code
3. Select the version attribute and modify it as follows:
Copy Code
Resource Allocation
The Microsoft .NET common language runtime requires that all resources beallocated from the managed heap. Objects are automatically freed when they are no
longer needed by the application.
When a process is initialized, the runtime reserves a contiguous region of addressspace that initially has no storage allocated for it. This address space region is the
managed heap. The heap also maintains a pointer. This pointer indicates where thenext object is to be allocated within the heap. Initially, the pointer is set to the baseaddress of the reserved address space region.
An application creates an object using the new operator. This operator first makessure that the bytes required by the new object fit in the reserved region (committing
storage if necessary). If the object fits, then pointer points to the object in the heap,this object's constructor is called, and the new operator returns the address of the
object.
-
7/28/2019 Dot Net Unit i Language
11/24
Above fig shows a managed heap consisting of three objects: A, B, and C. The next
object to be allocated will be placed where NextObjPtr points (immediately afterobject C).
When an application calls the new operator to create an object, there may not beenough address space left in the region to allocate to the object. The heap detects
this by adding the size of the new object to NextObjPtr. If NextObjPtr is beyond theend of the address space region, then the heap is full and a collection must be
performed.
In reality, a collection occurs when generation 0 is completely full. Briefly, a
generation is a mechanism implemented by the garbage collector in order to improveperformance. The idea is that newly created objects are part of a young generation,
and objects created early in the application's lifecycle are in an old generation.Separating objects into generations can allow the garbage collector to collect specific
generations instead of collecting all objects in the managed heap.
The Garbage Collection Algorithm
The garbage collector checks to see if there are any objects in the heap that are nolonger being used by the application. If such objects exist, then the memory used by
these objects can be reclaimed. (If no more memory is available for the heap, thenthe new operator throws an OutOfMemoryException.)
Every application has a set of roots. Roots identify storage locations, which refer to
objects on the managed heap or to objects that are set to null. For example, all the
global and static object pointers in an application are considered part of theapplication's roots. In addition, any local variable/parameter object pointers on a
thread's stack are considered part of the application's roots. Finally, any CPUregisters containing pointers to objects in the managed heap are also considered part
of the application's roots. The list of active roots is maintained by the just-in-time(JIT) compiler and common language runtime, and is made accessible to the garbage
collector's algorithm.
When the garbage collector starts running, it makes the assumption that all objects
in the heap are garbage. In other words, it assumes that none of the application'sroots refer to any objects in the heap. Now, the garbage collector starts walking the
-
7/28/2019 Dot Net Unit i Language
12/24
roots and building a graph of all objects reachable from the roots. For example, thegarbage collector may locate a global variable that points to an object in the heap.
Following fig shows a heap with several allocated objects where the application's
roots refer directly to objects A, C, D, and F. All of these objects become part of thegraph. When adding object D, the collector notices that this object refers to object H,
and object H is also added to the graph. The collector continues to walk through allreachable objects recursively.
Once this part of the graph is complete, the garbage collector checks the next root
and walks the objects again. As the garbage collector walks from object to object, ifit attempts to add an object to the graph that it previously added, then the garbage
collector can stop walking down that path. This serves two purposes. First, it helpsperformance significantly since it doesn't walk through a set of objects more than
once. Second, it prevents infinite loops should you have any circular linked lists ofobjects.
Once all the roots have been checked, the garbage collector's graph contains the set
of all objects that are somehow reachable from the application's roots; any objectsthat are not in the graph are not accessible by the application, and are thereforeconsidered garbage.
The garbage collector now walks through the heap linearly, looking for contiguousblocks of garbage objects (now considered free space). The garbage collector then
shifts the non-garbage objects down in memory (using the standard memcpyfunction), removing all of the gaps in the heap. Of course, moving the objects in
memory invalidates all pointers to the objects. So the garbage collector must modifythe application's roots so that the pointers point to the objects' new locations. In
addition, if any object contains a pointer to another object, the garbage collector isresponsible for correcting these pointers as well.
Following fig shows the managed heap after a collection.
-
7/28/2019 Dot Net Unit i Language
13/24
After all the garbage has been identified, all the non-garbage has been compacted,and all the non-garbage pointers have been fixed-up, the NextObjPtr is positioned
just after the last non-garbage object. At this point, the new operation is tried againand the resource requested by the application is successfully created.
GC generates a significant performance hit, and this is the major downside of using a
managed heap. However, keep in mind that GCs only occur when the heap is fulland, until then, the managed heap is significantly faster than a C-runtime heap. Theruntime's garbage collector also offers some optimizations using Generations that
greatly improve the performance of garbage collection.
You no longer have to implement any code that manages the lifetime of any
resources that your application uses. Now it is not possible to leak resources, sinceany resource not accessible from your application's roots can be collected at some
point. Also it is not possible to access a resource that is freed, since the resourcewon't be freed if it is reachable. If it's not reachable, then your application has no
way to access it.
Following code demonstrates how resources are allocated and managed:
class Application
{
publicstaticint Main(String[] args){
// ArrayList object created in heap, myArray is now in rootArrayList myArray = new ArrayList();
// Create 10000 objects in the heapfor (int x = 0; x < 10000; x++)
{myArray.Add(new Object());// Object object created in heap
}// Right now, myArray is a root (on the thread's stack). So,
// myArray is reachable and the 10000 objects it points to are also reachable.Console.WriteLine(myArray.Count);
// After the last reference to myArray in the code, myArray is not a root.// Note that the method doesn't have to return, the JIT compiler knows
// to make myArray not a root after the last reference to it in the code.// Since myArray is not a root, all 10001 objects are not reachable
// and are considered garbage. However, the objects are not// collected until a GC is performed.
}}
If GC is so great, you might be wondering why it isn't in ANSI C++. The reason isthat a garbage collector must be able to identify an application's roots and must alsobe able to find all object pointers. The problem with C++ is that it allows casting a
pointer from one type to another, and there's no way to know what a pointer refers
to. In the common language runtime, the managed heap always knows the actualtype of an object, and the metadata information is used to determine which
members of an object refer to other objects.
Generations
-
7/28/2019 Dot Net Unit i Language
14/24
One feature of the garbage collector that exists purely to improve performance iscalled generations. A generational garbage collector (also known as an ephemeral
garbage collector) makes the following assumptions:
The newer an object is, the shorter its lifetime will be.
The older an object is, the longer its lifetime will be.
Newer objects tend to have strong relationships to each other and arefrequently accessed around the same time.
Compacting a portion of the heap is faster than compacting the whole heap.
When initialized, the managed heap contains no objects. Objects added to the heap
are said to be in generation 0, as you can see in following fig. Stated simply, objectsin generation 0 are young objects that have never been examined by the garbage
collector.
Now, if more objects are added to the heap, the heap fills and a garbage collectionmust occur. When the garbage collector analyzes the heap, it builds the graph ofgarbage (shown here in Green) and non-garbage objects. Any objects that survive
the collection are compacted into the left-most portion of the heap. These objectshave survived a collection, are older, and are now considered to be in generation 1.
-
7/28/2019 Dot Net Unit i Language
15/24
As even more objects are added to the heap, these new, young objects are placed in
generation 0. If generation 0 fills again, a GC is performed. This time, all objects ingeneration 1 that survive are compacted and considered to be in generation 2 (see
following fig). All survivors in generation 0 are now compacted and considered to bein generation 1. Generation 0 currently contains no objects, but all new objects will
go into generation 0.
-
7/28/2019 Dot Net Unit i Language
16/24
Currently, generation 2 is the highest generation supported by the runtime's garbage
collector. When future collections occur, any surviving objects currently in generation2 simply stay in generation 2.
Generational GC Performance Optimizations
Generational garbage collecting improves performance. When the heap fills and a
collection occurs, the garbage collector can choose to examine only the objects ingeneration 0 and ignore the objects in any greater generations. After all, the newer
an object is, the shorter its lifetime is expected to be. So, collecting and compactinggeneration 0 objects is likely to reclaim a significant amount of space from the heap
and be faster than if the collector had examined the objects in all generations.
A generational collector can offer more optimizations by not traversing every object
in the managed heap. If a root or object refers to an object in an old generation, the
garbage collector can ignore any of the older objects' inner references, decreasingthe time required to build the graph of reachable objects. Of course, it is possiblethat an old object refers to a new object. So that these objects are examined, the
collector can take advantage of the system's write-watch support (provided by theWin32 GetWriteWatch function in Kernel32.dll). This support lets the collector know
which old objects (if any) have been written to since the last collection. Thesespecific old objects can have their references checked to see if they refer to any new
objects.
-
7/28/2019 Dot Net Unit i Language
17/24
If collecting generation 0 doesn't provide the necessary amount of storage, then thecollector can attempt to collect the objects from generations 1 and 0. If all else fails,
then the collector can collect the objects from all generations-2, 1, and 0.
One of the assumptions stated earlier was that newer objects tend to have strongrelationships to each other and are frequently accessed around the same time. Since
new objects are allocated contiguously in memory, you gain performance fromlocality of reference. More specifically, it is highly likely that all the objects can residein the CPU's cache. Your application will access these objects with phenomenal speed
since the CPU will be able to perform most of its manipulations without having cachemisses which forces RAM access.
Microsoft's performance tests show that managed heap allocations are faster thanstandard allocations performed by the Win32 HeapAlloc function. These tests also
show that it takes less than 1 millisecond on a 200 MHz Pentium to perform a full GCof generation 0. It is Microsoft's goal to make GCs take no more time than an
ordinary page fault.
Disadvantages of Win32 heap:
Most heaps (like the C runtime heap) allocate objects wherever they find free
space. Therefore, if I create several objects consecutively, it is quite possible
that these objects will be separated by megabytes of address space.However, in the managed heap, allocating several objects consecutively
ensures that the objects are contiguous in memory.
When memory is allocated from a Win32 heap, the heap must be examined to
find a block of memory that can satisfy the request. This is not required inmanaged heap, since here objects are contiguous in memory.
In Win32 heap, data structures that the heap maintains must be updated. The
managed heap, on the other hand, only needs to increment the heap pointer.
Finalization
The garbage collector offers an additional feature that you may want to take
advantage of: finalization. Finalization allows a resource to gracefully clean up afteritself when it is being collected. By using finalization, a resource representing a file or
network connection is able to clean itself up properly when the garbage collectordecides to free the resource's memory.
When the garbage collector detects that an object is garbage, the garbage collector
calls the object's Finalize method (if it exists) and then the object's memory is
reclaimed. For example, let's say you have the following type (in C#):
publicclass BaseObj
{public BaseObj()
{}
protectedoverridevoid Finalize(){
// Perform resource cleanup code here
-
7/28/2019 Dot Net Unit i Language
18/24
// Example: Close file/Close network connectionConsole.WriteLine("In Finalize.");
}}
Now you can create an instance of this object by calling:
BaseObj bo = new BaseObj();
Some time in the future, the garbage collector will determine that this object isgarbage. When that happens, the garbage collector will see that the type has a
Finalize method and will call the method, causing "In Finalize" to appear in theconsole window and reclaiming the memory block used by this object.
Many developers who are used to programming in C++ draw an immediatecorrelation between a destructor and the Finalize method. However, object
finalization and destructors have very different semantics and it is best to forgeteverything you know about destructors when thinking about finalization. Managed
objects never have destructors.
When designing a type it is best to avoid using a Finalize method. There are several
reasons for this:
Finalizable objects get promoted to older generations, which increases
memory pressure and prevents the object's memory from being collected
when the garbage collector determines the object is garbage. In addition, all
objects referred to directly or indirectly by this object get promoted as well.
Finalizable objects take longer to allocate.
Forcing the garbage collector to execute a Finalize method can significantly
hurt performance. Remember, each object is finalized. So if I have an array of10,000 objects, each object must have its Finalize method called.
Finalizable objects may refer to other (non-finalizable) objects, prolonging
their lifetime unnecessarily. In fact, you might want to consider breaking atype into two different types: a lightweight type with a Finalize method that
doesn't refer to any other objects, and a separate type without a Finalizemethod that does refer to other objects.
You have no control over when the Finalize method will execute. The object
may hold on to resources until the next time the garbage collector runs.
When an application terminates, some objects are still reachable and will not
have their Finalize method called. This can happen if background threads are
using the objects or if objects are created during application shutdown orAppDomain unloading. In addition, by default, Finalize methods are not called
for unreachable objects when an application exits so that the application mayterminate quickly. Of course, all operating system resources will be reclaimed,
but any objects in the managed heap are not able to clean up gracefully. Youcan change this default behavior by calling the System.GC type's
RequestFinalizeOnShutdown method. However, you should use this methodwith care since calling it means that your type is controlling a policy for the
entire application.
-
7/28/2019 Dot Net Unit i Language
19/24
The runtime doesn't make any guarantees as to the order in which Finalize
methods are called. For example, let's say there is an object that contains a
pointer to an inner object. The garbage collector has detected that bothobjects are garbage. Furthermore, say that the inner object's Finalize method
gets called first. Now, the outer object's Finalize method is allowed to accessthe inner object and call methods on it, but the inner object has been
finalized and the results may be unpredictable. For this reason, it is stronglyrecommended that Finalize methods not access any inner, member objects.
If you determine that your type must implement a Finalize method, then make surethe code executes as quickly as possible. Avoid all actions that would block the
Finalize method, including any thread synchronization operations. Also, if you let any
exceptions escape the Finalize method, the system just assumes that the Finalizemethod returned and continues calling other objects' Finalize methods.
When the compiler generates code for a constructor, the compiler automatically
inserts a call to the base type's constructor. Likewise, when a C++ compilergenerates code for a destructor, the compiler automatically inserts a call to the base
type's destructor. Finalize methods are different from destructors. The compiler hasno special knowledge about a Finalize method, so the compiler does not
automatically generate code to call a base type's Finalize method. If you want thisbehavior-and frequently you do-then you must explicitly call the base type's Finalize
method from your type's Finalize method:
publicclass BaseObj
{public BaseObj()
{}
protectedoverridevoid Finalize(){
Console.WriteLine("In Finalize.");base.Finalize();// Call base type's Finalize}
}
Note that you'll usually call the base type's Finalize method as the last statement inthe derived type's Finalize method. This keeps the base object alive as long as
possible. Since calling a base type Finalize method is common, C# has a syntax thatsimplifies your work. In C#, the following code:
class MyObject{
MyObject(){
}}
causes the compiler to generate this code:
class MyObject
{
-
7/28/2019 Dot Net Unit i Language
20/24
protectedoverridevoid Finalize(){
base.Finalize();}
}
Note that this C# syntax looks identical to the C++ language's syntax for defining adestructor. But remember, C# doesn't support destructors. Don't let the identicalsyntax fool you.
Finalization Internals
When an application creates a new object, the new operator allocates the memory
from the heap. If the object's type contains a Finalize method, then a pointer to theobject is placed on the finalization queue. The finalization queue is an internal data
structure controlled by the garbage collector. Each entry in the queue points to anobject that should have its Finalize method called before the object's memory can be
reclaimed.
Following fig shows a heap containing several objects. Some of these objects arereachable from the application's roots, and some are not. When objects C, E, F, I,
and J were created, the system detected that these objects had Finalize methods and
pointers to these objects were added to the finalization queue.
-
7/28/2019 Dot Net Unit i Language
21/24
When a GC occurs, objects B, E, G, H, I, and J are determined to be garbage. Thegarbage collector scans the finalization queue looking for pointers to these objects.
When a pointer is found, the pointer is removed from the finalization queue andappended to the freachable queue (pronounced "F-reachable"). The freachable queue
is another internal data structure controlled by the garbage collector. Each pointer inthe freachable queue identifies an object that is ready to have its Finalize method
called.
After the collection, the managed heap looks like following fig. Here, you see that the
memory occupied by objects B, G, and H has been reclaimed because these objectsdid not have a Finalize method that needed to be called. However, the memory
occupied by objects E, I, and J could not be reclaimed because their Finalize methodhas not been called yet.
There is a special runtime thread dedicated to calling Finalize methods. When the
freachable queue is empty (which is usually the case), this thread sleeps. But when
entries appear, this thread wakes, removes each entry from the queue, and callseach object's Finalize method. Because of this, you should not execute any code in aFinalize method that makes any assumption about the thread that's executing the
code. For example, avoid accessing thread local storage in the Finalize method.
The interaction of the finalization queue and the freachable queue is quite
fascinating. First, let me tell you how the freachable queue got its name. The f isobvious and stands for finalization; every entry in the freachable queue should have
its Finalize method called. The "reachable" part of the name means that the objects
-
7/28/2019 Dot Net Unit i Language
22/24
are reachable. To put it another way, the freachable queue is considered to be a rootjust like global and static variables are roots. Therefore, if an object is on the
freachable queue, then the object is reachable and is not garbage.
In short, when an object is not reachable, the garbage collector considers the objectgarbage. Then, when the garbage collector moves an object's entry from the
finalization queue to the freachable queue, the object is no longer consideredgarbage and its memory is not reclaimed. At this point, the garbage collector hasfinished identifying garbage. Some of the objects identified as garbage have been
reclassified as not garbage. The garbage collector compacts the reclaimable memoryand the special runtime thread empties the freachable queue, executing each
object's Finalize method.
The next time the garbage collector is invoked, it sees that the finalized objects are
truly garbage, since the application's roots don't point to it and the freachable queueno longer points to it. Now the memory for the object is simply reclaimed. The
important thing to understand here is that two GCs are required to reclaim memory
used by objects that require finalization. In reality, more than two collections may benecessary since the objects could get promoted to an older generation. Above fig
shows what the managed heap looks like after the second GC.
Dispose Method
Use this method to close or release unmanaged resources such as files, streams, and
handles held by an instance of the class that implements this interface. This methodis, by convention, used for all tasks associated with freeing resources held by an
object, or preparing an object for reuse.
-
7/28/2019 Dot Net Unit i Language
23/24
When implementing this method, objects must seek to ensure that all held resourcesare freed by propagating the call through the containment hierarchy. For example, if
an object A allocates an object B, and object B allocates an object C, then A'sDispose implementation must call Dispose on B, which must in turn call Dispose on
C. Objects must also call the Dispose method of their base class if the base classimplements IDisposable.
If an object's Dispose method is called more than once, the object must ignore allcalls after the first one. The object must not throw an exception if its Dispose
method is called multiple times. Dispose can throw an exception if an error occursbecause a resource has already been freed and Dispose had not been called
previously.
Because the Dispose method must be called explicitly, objects that implement
IDisposable must also implement a finalizer to handle freeing resources whenDispose is not called. By default, the garbage collector will automatically call an
object's finalizer prior to reclaiming its memory. However, once the Dispose methodhas been called, it is typically unnecessary for the garbage collector to call the
disposed object's finalizer. To prevent automatic finalization, Disposeimplementations can call the GC.SuppressFinalize method.
Direct Control with System.GC
The System.GC type allows your application some direct control over the garbage
collector. You can query the maximum generation supported by the managed heapby reading the GC.MaxGeneration property. Currently, the GC.MaxGeneration
property always returns 2.
It is also possible to force the garbage collector to perform a collection by calling one
of the two methods shown here:
void GC.Collect(Int32 Generation)
void GC.Collect()
The first method allows you to specify which generation to collect. You may pass any
integer from 0 to GC.MaxGeneration, inclusive. Passing 0 causes generation 0 to be
collected; passing 1 cause generation 1 and 0 to be collected; and passing 2 causesgeneration 2, 1, and 0 to be collected. The version of the Collect method that takes
no parameters forces a full collection of all generations and is equivalent to calling:
GC.Collect(GC.MaxGeneration);
Under most circumstances, you should avoid calling any of the Collect methods; it is
best to just let the garbage collector run on its own accord. However, since yourapplication knows more about its behavior than the runtime does, you could help
matters by explicitly forcing some collections. For example, it might make sense foryour application to force a full collection of all generations after the user saves his
data file. I imagine Internet browsers performing a full collection when pages areunloaded. You might also want to force a collection when your application is
performing other lengthy operations; this hides the fact that the collection is taking
-
7/28/2019 Dot Net Unit i Language
24/24
processing time and prevents a collection from occurring when the user is interactingwith your application.
The GC type also offers a WaitForPendingFinalizers method. This method simply
suspends the calling thread until the thread processing the freachable queue hasemptied the queue, calling each object's Finalize method. In most applications, it is
unlikely that you will ever have to call this method.
Lastly, the garbage collector offers two methods that allow you to determine whichgeneration an object is currently in:
Int32 GetGeneration(Object obj)Int32 GetGeneration(WeakReference wr)
The first version of GetGeneration takes an object reference as a parameter, and thesecond version takes a WeakReference reference as a parameter. Of course, the
value returned will be somewhere between 0 and GC.MaxGeneration, inclusive.
top related