Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ways of making an object to explicitly garbage collected

Tags:

java

I have read about making an object explicitly garbage collected, that in many ways. So i wanted to know some of the methods of making it explicitly garbage collected

like image 618
GuruKulki Avatar asked Dec 29 '22 03:12

GuruKulki


1 Answers

There is no way for explicit garbage collection.

You can "politely ask" the virtual machine to do garbage collection by calling:

System.gc();

but it is not guaranteed it will.

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

and "best effort" might be to postpone the garbage collection.

For how to make objects elligible for garbage collection read Effective Java, Chapter 2

like image 80
Bozho Avatar answered Jan 18 '23 20:01

Bozho