66.2k views
4 votes
I am making a java application, and I need to forcethe garbage collection, how can I

achieve that, if not why?

1 Answer

3 votes

Answer:

Hi!

  1. Runtime objectRuntime = Runtime.getRuntime();
  2. objectRuntime.gc();

Step-by-step explanation:

First, you have to create an instance of a Java Runtime object. (see the first step of the answer).

Then, after creating the instance of Runtime object you have to invoke gc() method of the Runtime class. (see the second step of the answer).

User Oletha
by
5.6k points