Garbage collection in asp.net

How Garbage Collector(GC) works?

How Garbage Collector (GC) works?
Microsoft .NET CLR environment provides automatic memory management using Garbage Collection (GC). Garbage Collection (GC) basically a process that frees or releases the object from the memory which is no longer is in use by program. When we create new object Common Language Runtime (CLR) allocates the memory to new object on managed heap. Till the time memory has free space runtime allocates it to a new object but when memory gets full Garbage Collection comes into the picture. Garbage Collection GC basically performs memory clean up in order to make space available for other newly created object.

Benefits of Garbage Collections are that it allows us to develop application without implementing logic to free memory. Also it allocates objects on memory very efficiently. It reclaims the objects those are no longer in use and frees the memory for new objects to be allocated. Also provides safe memory operations by making sure that one object cannot use other object

When Garbage collection(GC) Occurs or run?

Garbage collection happens for below conditions.
  • If system has low memory
  • If the memory allocated to object exceeds.
  •  if GC.Collect get called.
How Garbage Collector(GC) keep track of object's life?
The heap is organized in generations so that it can keep track of short time spaned object and long time spaned objects i.e. (short-lived and long lived objects). Garbage collection occures for short time spaned object who occupies small part of heap. There are three types of generations on the heap:

  • Gen 0
              It contains short spaned objects.GC collection mostly happen in Gen0. Newly allocated object are  implicitly comes under Gen0 if object is not a large object. Example: Temp variables or local variables to the process.
  • Gen 1
             It is buffer between short spaned object memory and long spaned object memory. if Gen0  space is not enough for newly allocated object then data moved to Gen1.
  • Gen 2
           It contains objects with long span and going to be used for long period. Example: global variables those are going to be used through out the applicaion life.


How to clear unmanaged resources?
Unmanaged resources are the resources that does not taking care by CLR. Means those are not cleared from memory using Garbage Colletion(GC). File system object, database connection objects, windows handlers...etc are the examples of unmanaged resources.
We can use both implicit and explicit way to free such unmanages resources. Implicit way is to implement Finalize Method on an object. GC (Garbage Collector) calls this method when object is no longer referenced.

Explicit way is to implement  IDisposable Interface and it's  Dispose method.

jQuery Events Part - II : Mouse Events - Continue....

jQuery mouse evemts

.hover()
hover mouse event triggers to the element when user take mouse over the perticular element.

Example of hover mouse event:

<div> id="hoverDiv">Change Color </div>
$(document).ready(function() 
{  
$("hoverDiv").hover( 
function () { 
$(this).css("backgroundcolor", 'blue'); 
} 
);
});



.mousedown()
mousedown() mouse event triggers when user clicks on the the perticular element.

Example of mousedown event:

    <div> id="hoverDiv">Change Color</div>
    $(document).ready(function() 
    {  
    $("hoverDiv").mousedown( 
    function () {
    alert('Mouse Down event fired for div')
    } 
    );
    });
    


.mouseenter()
mouseenter() mouse event triggers when user enter to the area of perticular element. This is different from .hover in the way the event bubbling happens. If we have element inside element and mouse enters to inside element the mouseenter event will not fire for ouside element. But in case of .mouseover() mouse event it will fire for both inner and outter element. For more info: http://api.jquery.com/mouseenter/

Example that shows mouse enter event

    <div> class="out enter">
        <p>move your mouse</p>
        <p>0</p>
    </div>
    var i = 0;
    $("div.enter").mouseenter(function(){
    $("p:first",this).text("mouse enter");
    $("p:last",this).text(++i);
    }); 
    


.mouseleave()
mouseleave() mouse event triggers when user leaves the area of perticular element. This is different from .hover in the way the event bubbling happens. If we have element inside element and mouse leaves from inside element the mouseleave event will not fire for ouside element. But in case of .mouseout() mouse event it will fire for both inner and outter element. For more info: http://api.jquery.com/mouseenter/

Example that shows mouse leave event

    <div> class="out leave">
        <p>move your mouse</p>
        <p>0</p>
    </div>

    var i = 0;
    $("div.leave").mouseleave(function(){
    $("p:first",this).text("mouse leave");
    $("p:last",this).text(++i);
    }); 
    

jQuery Events Part - II : Mouse Events

jQuery mouse events

In this post we will look how jQuery mouse events works. jQuery library provides mouse events like click, double click, mouseup, mousedown, toggle..etc. Using this jQuery events one can easily perform the required operations or actions. We will look jQuery events samples in this post.

.click()
click event get triggered to an element when mouse button is pressed and released on that element. Any HTML element can trigger this event.

Sample code for jQuery click event:


<a id="mouseclick" href="mouseclickdemo.aspx">Click here</a>

$(document).ready(function() 
{  
$("#mouseclick").click(function(){
alert("This is jQuery Mouse Click event.")
});
});

In above jQuery code for click event will triggered and alter with “This is jQuery Mouse click event” will get displayed.

.dblclick()
dblclick event get triggered to an element when mouse button is pressed and released twice on that element. Any HTML element can trigger this event.

Below sample code shows how jQuery double click (dblclick) event works:

<div id="mousedblclick"> Click here</div>
$(document).ready(function() 
{  
$("#mousedblclick".dblclick(function(){
alert("This is jQuery mouse dblClick event.")
});
});

Above code shows that after double clicking on div tag it will trigger jQuery dblclick event and will show alert message “This is mouse dblclick event.”

.mousedown()
mousedown event get triggered to an element when mouse button is moved over the element and mouse button is pressed . Any HTML element can trigger this event.

Below sample code shows how jQuery mouse down event works:

    <span id="mousedown">Click here</span>

    $(document).ready(function() 
    {  
    $("#mousedown").mousedown(function(){
    alert("jQuery mousedown event.")
    });
    });

In above sample of jQuery, when mouse down event triggers then it will show alert message “jQuery mousedown event.”


.mouseup()
mouseup event get triggered to an element when mouse button is pressed and then released. Any HTML element can trigger this event.

Below is sample jQuery code for mouseup event:

<div id="mouseup"> Click here</ div>
$(document).ready(function() 
{  
$("#mouseup").mouseup(function(){
alert("jQuery mouseup event.")
});
});
When mouse button is released then jQuery mouse event will get triggered and it will show alert message “jQuery mouseup event.”


.toggle()
toggle event used to do the alternate actions for perticular operation. Which is used in association with click.

code shows how jQuery toggle event works :

<div id="toogleDiv"> Change Color </div>

$(document).ready(function() 
{  
$("toogleDiv").toggle( 
function() { 
$(this).css("backgroundcolor", 'blue'); 
}, 
function () { 
$(this).css("backgroundcolor",'green');
});
});
jQuery toggle event used to change the state of object from one state to other. for example we can show or hide div element using toggle event. Toggle event works in association with click event. In above code sample when we click on div element it will change color from blue to green and if it already green then changes it to blue.