+ 2
What is a garbage collector in java?
5 Respuestas
+ 2
When you instantiate something, like an object, you ask the computer to give you a small amount of memory to store the instantiation. The garbage collector free this memory when you do not need it anymore (but it is far from perfect)
+ 1
could you explain more with example?
+ 1
In some languaje like C or C++ there is no garbage collector, it means you are the garbage collector and there is a destructor in other languajes like java it has it's own garbage collector who is encharge of look what is been used and what is not. In java you just create and java delete not used objects but there is a reason why java can do it, it has a virtual machine and the code is precompiled it make that situations are more predictables. If you are a good C C++ programmer there is a huge efficiency difference becouse you control the moment you want to delete objects, but java has kind of daemon(collector) that is running and looking always it is not efficient. Sorry for my english.
0
Example:
int number = 5; //here we declaring a variable
System.out.println(number);//here we use that variable
System.out.println("variable is no more");
/*variable was collected by the garbage collector and memory was freed, because we don't use it anymore.*/
0
that means that garbage collector deals only with object