Programming and Garbage Collector

The term “garbage collector” is ultimately returned to the vocabulary of programmers, since this type of functionality that is supported by almost all programming languages ​​and systems development: from Java to Visual Basic and platform .NET framework. Notable exception to this rule is the C/C++, in which the memory management is the sole responsibility of the programmer. The story of the garbage collection it is definitely not recent, in fact, has been implemented for the first time with the LISP interpreter, and this type of systems have been increasingly integrated into development languages ​​since the ’60s.
The “garbage collector” is the collection of waste, that all portions of memory allocated, but no longer used. A garbage collector is a system that at run-time is to search for these portions of memory and make them available to be used again. A mechanism of this type is useful if the language allows you to declare variables in a dynamic way, the allocation of which occurs during the execution of the program. Consider the following piece of code C/C++:

int x;
int * p = new int;

The allocation of the variable “x” in the first line of code is static, its life time, first defined and can be determined at compile time of the source. For example, if the variable is defined at the beginning of a function, it is allocated and deallocated from the beginning to the end (this is not so, but we need not go into details at this time). The allocation instead of “p”, in the second line of code, it is dynamic and is done only when the statement is actually executed. The statement could be part of an if or do not know the space in memory to be allocated because of a non-identifiable. A dynamically allocated object does not have a birth date known a priori and can not be deallocated only because it is no longer used by the program. From this point of view there are only two possibilities, or as in C/C++ that both the programmer to decide when the object should be deallocated or with the garbage collector that controls the execution of the program and occur when an object is no longer reachable and therefore can be deallocated.

Generalizing, we can say that an object can be released if there are no variables that allow you to access it in C/C++ this is done with pointers, while in languages ​​such as Java, VB.NET or C# using references, but the concept does not change. To release an object, then, you must be sure that it can no longer be used. There are several techniques to perform this kind of control, which correspond to different strategies of operation of a garbage collector. Unreferenced objects can be deleted, but these in turn could referenziarli others and so on. This type of control would therefore be extremely costly, and this is used for reference counting, in which each object is kept in a counter that indicates the number of existing references to that object, as well as objects work COM (Component Object Model) with AddRef, and Release methods. Each time you assign to a variable a reference to an object, the object’s reference count is incremented, and when a variable ceases to exist, the reference count of the object referenced is decreased, when the counter has reached zero, the garbage collector destroys the object, but as we will see some problems remain.
In C/C++ most of the programming errors lies in poor management of and access direct memory pointers. When you program in C/C++ the biggest problem, because little detectable, it is the memory leak, which is precisely the occupation of memory and not released. If the memory leak lies in a cycle, the problem could be very serious, because it would take up a lot of memory needlessly and this can be serious even in current computers, which have plenty of RAM.
In Visual Basic to release an object you use the following notion:

Tea f = Nothing

In this case the object is not immediately released by the garbage collector, but only later, when he realizes that this is no longer used. In the .NET framework the algorithm used for the garbage collector is no longer the reference counting, but it is considered the actual accessibility of the object, then you should not use any more instruction to release the references explicitly. In .NET, however, the release of the object is not immediate, indeed necessary to wait some time for the object is destroyed.
The garbage collector can help, but you have to get to know the specific implementation of the system that you are using to avoid being damaged.

This entry was posted in C# (sharp), Java, Programming. Bookmark the permalink.