One of the major causes of program failure today, particularly in applications that run for long time is due to manual memory management. Which leads two main problems
First one is, a programmer allocates a block of memory in a data storage area Of operating system (i.e. in Random Accesses Memory) intending to Free it latter, some time he mistakenly forget to release memory it No longer needs, this condition known as ‘Memory leak’
If this application run long enough, these leaks accumulate and the application runs out of memory, that is not big deal in a programs like ‘notepad’, that user runs for a few minutes an then shutdown. But a fatal application like web server that are supposed to run continuously for days or week this will leads accumulation of memory leaks and which leads to application fail.
Second one is programmer manually delete an object but then mistakenly or other objects try to access this memory location later. This will leads to hanging of application, and more over some times transistor that had make up the deleted object memory would still contain ‘Plausible’ values, and program continue to run with corrupted data.
These two above mentioned bugs are worse than most other application bugs because what the consequences will be and when those consequences will occur are typically unpredictable. That is making our application perform in unpredictable ways at unpredictable times. For other bugs, when we see our application misbehaving, we can just fix it
Microsoft.NET Made solution for above mentioned bugs. That is Microsoft made automatic memory management has part of .Net common language runtime (CLR), which allows it is to be used in any .Net language. That is in .Net application if CLR detects that, a Application is no longer using the memory and that it no longer needed. Then CLR release that memory. (i.e. Application does not have to explicitly free Memory that allocated). This mechanism runs automatically in background and is known as Garbage Collection
It solves the problem of manual memory management with out having to write any single line of code. You can’t forget to delete an object because the system cleans it for you when it is not required and you can’t access deleted object through an invalid reference because the object won’t be deleted as long as you hold a reference it
This Garbage Collection is like an automatic seat belt. The passengers couldn’t forget to buckle it. More over we know that automatic belt require more space and mechanism than manual one. Similarly .net application Require more system resources than ordinary application.
Really the garbage collection changes programming. we don't have all that Free What we Create overhead, so our code is smaller and clearer and easier to write. Garbage collection also makes programming even more object oriented.
Main aim of .Net is that faster development with fewer bugs That is we want programmer only think about program logic. Not any other Things like memory management or else.
NOTE: The memory used by heap variables is freed via a process garbage collection. Stack variables is automatically freed when the method in which they were created returns( ie Grabage collection is applied only for Heap Variable)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment