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 the operating system (i.e. in Random Accesses Memory) intending to free it later, but forgets to free the memory it no longer needs. This condition popular as ‘Memory leak’
If an application runs long enough, these leaks accumulate and the application runs out of memory, which may not be a big deal in light programs like ‘notepad’, which the user runs for only a few minutes an then shuts-down. But a large applications, like web servers, that are supposed to run continuously for days or weeks, accumulation of memory leaks lead to crashes.
Second one: A programmer manually deletes an object but then mistakenly or other objects tries to access this memory location later. The application tends to hang, and more over transistors that had make up the deleted object memory would still contain ‘Plausible’ values, and the process continues to run with corrupted data.
The above mentioned problems are worse than most other application bugs because of the consequences and their unpredictable occurrences. That is, making our application unpredictable at unpredictable times.
Microsoft.NET provides a solution. It has made automatic memory management a part of .Net common language runtime (CLR). This allows it to be used in any .NET language. That is in .NET application if CLR detects that an application is no longer using the memory or that it is no longer needed, memory is released (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 without having to write any code. You can afford to forget to delete an object because the system cleans it for you when it is not required and you can’t access deleted objects through an invalid reference because the object won’t be deleted as long as you hold a reference to it.
This Garbage Collection is like an automatic seat belt. The passengers couldn’t forget to buckle it. Moreover, we know that automatic belt require more space and mechanisms than manual one. Similarly .NET applications require more system resources than ordinary applications.
The garbage collection feature changes the way we code. We don't have the Free All That we Create overhead, so our code is smaller and clearer and easier to write. Garbage collection also makes programming more object oriented.
Main aim of .Net is to facilitate faster development with fewer bugs. NOTE: The memory used by heap variables is freed via a process called garbage collection. Stack variables is automatically freed when the method in which they were created returns( ie Grabage collection is applied only for Heap Variables)
In the next article, i will explain the working of Garbage Collection.
Psaj, NITC
|
No responses found. Be the first to respond and make money from revenue sharing program.
|