+ 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 :)
6 Réponses
+ 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.
+ 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.
+ 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.
+ 2
And the abandoned objects are eligible for garbage collector
+ 1
Thank you Nishanth
0
The Garbage Collector (GC) in Java is a part of the Java Virtual Machine (JVM) responsible for automatic memory management. Its primary role is to identify and remove objects that are no longer in use so that memory can be reclaimed and reused efficiently.
In simple words → GC in Java saves developers from the hassle of manually allocating and deallocating memory (like in C/C++), reducing memory leaks and crashes.
Also read, https://blog.gceasy.io/what-is-java-garbage-collection/