What Is Garbage Collection?


  • Memory Management technique. 
  • Process of freeing objects, No longer referenced by the program 


Why Garbage Collection?


  • Free unreferenced objects. 
  • Combat heap fragmentation. 
  • Relieves programmer from manual freeing the memory.
  • Helps in ensuring program integrity. 


Disadvantages Of Garbage Collection


  • Extra Overhead.
  • Less control over scheduling of CPU time

The Garbage Collection Algorithm in .NET

  • Microsofts .NET common language runtime requires that all resources be allocated from the managed heap. 
  • Managed heap has a pointer NextObjPtr which indicates where the next object is to be allocated within the heap. The Garbage Collection Algorithm 
  • The garbage collector checks to see if there are any objects in the heap that are no longer 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, then the new operator throws an OutOfMemoryException.

How GC knows if app. Is using object or not

  • 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. 
  • 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. 
  • GC constructs graph of all reachable objects based on assumption that all objects are garbage 
  • The GC walks through the heap linearly, looking for garbage objects (free space now). • GC then shifts non garbage objects down in the memory, removing all the gaps in the heap.




Finalization

  • By using finalization, a resource representing a file or network connection is able to clean itself up properly when the garbage collector decides 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. 
  • Finalize is very different from destructors. 
  • Finalizable objects get promoted to older generations, which increases memory pressure. 
  • All objects referred to directly or indirectly by this object get promoted as well. 
  • Forcing the garbage collector to execute a Finalize method can significantly hurt performance
  • Finalizable objects may refer to other (non-finalizable) objects, prolonging their lifetime unnecessarily. 
  • 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. 
  • If you determine that your type must implement a Finalize method, then make sure the code executes as quickly as possible. Avoid all actions that would block the Finalize method 
  • Each entry in the queue points to an object that should have its Finalize method called before the object's memory can be reclaimed. 
  • At this point, the garbage collector has finished identifying garbage. 
  • There is a special runtime thread dedicated to calling Finalize methods. 

Garbage Collection in .NET Framework

 .NET Framework components 


  • Common Language Runtime(CLR) 
  • NET Framework Class Library 
  • Common Type System(CTS) 
  • Common Language Specification(CLS) 
  • Assemblies 
  • Windows Forms 
  • ASP.NET 
  • ActiveX Data Objects(ADO.NET) 
  • Windows Workflow Foundation(WF) 
  • Windows Presentation Foundation(WPF) 
  • Windows Communication Foundation(WCF)
  • Windows CardSpace
  • Language Integrated Query (LINQ)
  • Parallel Programming

Common Language Runtime (CLR)


 CLR ensures that the language-neutral development and execution environment that provides services to help "manage" application execution. 
  • A common runtime environment for all .NET languages. 
  • Use of Common Type System (strict-type & code-verification). 
  • Use of metadata for safe execution. 
  • Memory allocation and garbage collection.
  • Intermediate Language (IL) to native code compilers. 
  • Compiles MSIL code into native executable code. 
  • Security and interoperability of the code with other languages. 
CLR: Execution Mode

.NET Framework Class Library

  • The Class Library is a comprehensive, object-oriented collection of reusable types. 
  • The Class Library is organized in a hierarchy of namespaces. 
  1. Traditional command-line applications.
  2. Graphical User Interface (GUI) applications. 
  3. Applications based on the latest innovations provided by ASP.NET. 

Common Type System 

  1. Defines how types are declared, used, and managed in runtime. 
  2. Is also an important part of the runtime's support for crosslanguage integration. 
  3. Performs the following functions:
    1. Establishes a framework that helps enable cross-language integration, type safety, and high performance code execution. 
    2. Provides an object-oriented model that supports the complete implementation of many programming languages. 
    3. Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.

Common Language Specification (CLS) 

  • The CLS is a set of basic language features needed by many applications. 
  • The CLS rules define a subset of the Common Type System. All the rules that apply to the Common Type System apply to the CLS, except where stricter rules are defined in the CLS. 
  • The CLS helps to enhance and ensure language interoperability by defining a set of features that the developers can rely on to be available in a wide variety of languages. 
  • Components that adhere to the CLS rules and use only the features included in the CLS are said to be CLS-compliant components. 
  • Most of the members defined by types in the .NET Framework Class Library are CLS-compliant.

Assemblies

  • Assemblies are the building blocks of .NET Framework applications. 
  • They form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. 
  • An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. 
  • They provide the CLR with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly. 
  • An assembly can be Shared or Private:
    • A Private Assembly is available only to the application for which it is created. 
    •  A Shared Assembly is available to more than one (multiple) applications. Shared Assemblies are stored in the assembly cache (GAC) using the .NET utility Gacutil.exe or Regasm.exe.

Components Of .NET Framework (CLR/CTS/CLS/W/PF/LINQ)

.NET Framework 


  • NET Framework is an integral windows component that supports building and running the next generation of applications and Web services. 
  • The .NET Framework provides a managed execution environment, simplified development and deployment and integration with a wide variety of programming languages. 
  • Microsoft Visual Studio® 2013 and the Microsoft .NET Framework allow developers to develop traditional windows client applications, distributed components, client server applications and much more.


Objectives Of .NET Framework


  •  Provide a consistent object-oriented programming environment. –
  •  Provide a code-execution environment that minimizes software deployment and versioning conflicts. Provide a code-execution environment that promotes safe execution of code, including code created by unknown or semi-trusted third parties. 
  • Provide a code-execution environment that eliminates the performance problems of scripted or interpreted environments. 
  • Provide consistency across widely varying types of applications, such as Windows-based applications and Web-based applications. 
  • Build all communication to industry standards ensuring that code based on the .NET Framework can integrate with any other code.


.NET Framework

What is .NET Framework And Its Objectives