Garbage Collection In C++
Garbage
collection is a form of automatic memory management. The
garbage
collector or collector attempts to reclaim garbage, or memory
used by
objects that will never be accessed or mutated again by the
application.
Fig:Garbage Collection In C++ |
Advantages and Disadvantages of Manual Memory Management
Advantages
of manual memory management are that the user would have complete control over
both allocating and deallocating operations and also know when a new memory is
allocated and when it is deallocated or released. But in the case of garbage
collection, at the exact same instance after the usage the memory will not
be released it will get released when it encounters it during the periodic
operation.
Also
in the case of manual memory management, the destructor will be called at the
same moment when we call the ‘delete’ command. But in case of garbage collector
that is not implemented.
There
are a few issues associated with using manual memory management. Sometimes we
might tend to double delete the memory occupied. When we delete the already
deleted pointer or memory, there are chances that the pointer might be
referencing some other data and can be in use.
Program:
#include<iostream.h>
#include<conio.h>
class A()
{
int x;
public:
A()
{
x=0;
++x;
}
};
void main()
{
for(int
i=0;i<1000000;++i)
{
A *a=new A();
delete a;
}
std::cout<<”ding!”<<std::endl;
}
No comments:
Post a Comment