Garbage collector in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Garbage collector in Java

While I was looking into the System class and their methods, I found a garbage collector. System.gc() https://docs.oracle.com/javase/7/docs/api/java/lang/System.html Now I have two questions: 1. Can you create garbage in Java so that you would need a collector? 2. If not, what is the use of this collector? Thank you :)

14th Apr 2019, 3:46 PM
Denise Roßberg
Denise Roßberg - avatar
5 Answers
+ 2
After the execution process,the JVM inside stack area calls garbage collector which is a basic need provided by JVM. When Garbage collector settles inside stack,the JVM pops out of stack Finally the garbage collector cleans the abandoned objects and other trash that is present in the heap area.
17th Jul 2020, 3:31 AM
Nishanth
Nishanth - avatar
+ 4
Garbage here is basically the memory that unnecessary objects take up. In languages like C++, you have destructors for any object that activate whenever the object is destroyed - at the end of the program, or by the following syntax. ~MyObj; //where MyObj is the object. In Java though, you will have to collect all that garbage using a garbage collector. By the way, I am not a Java expert so some of the information may (or may not) be off.
14th Apr 2019, 3:57 PM
Naveen Maurya
Naveen Maurya - avatar
+ 2
Naveen Maurya Thanks for your answer. After some research : It is complicated. ;) I only understood that the JVM collects the garbage. So I don't need to call the garbage collector manually. Most advise against, because you can do a lot wrong.
14th Apr 2019, 5:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
And the abandoned objects are eligible for garbage collector
17th Jul 2020, 3:32 AM
Nishanth
Nishanth - avatar
+ 1
Thank you Nishanth
17th Jul 2020, 10:29 AM
Denise Roßberg
Denise Roßberg - avatar