What is a garbage collector in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is a garbage collector in java?

24th Jan 2017, 11:32 AM
Ahmed Abd Elazeem
Ahmed Abd Elazeem - avatar
5 Answers
+ 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)
24th Jan 2017, 11:40 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
could you explain more with example?
24th Jan 2017, 11:47 AM
Ahmed Abd Elazeem
Ahmed Abd Elazeem - avatar
+ 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.
25th Jan 2017, 4:22 AM
Johan Durán
Johan Durán - avatar
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.*/
24th Jan 2017, 12:07 PM
Maksym Zieliński
Maksym Zieliński - avatar
0
that means that garbage collector deals only with object
24th Jan 2017, 12:20 PM
Ahmed Abd Elazeem
Ahmed Abd Elazeem - avatar