Is it possible to kill GC in JAVA


I was doing an interview and the interviewer asked if it was possible to kill GC in Java. To the additional question of whether the app should live, he answered, yes it should. I said that without rebuilding the JDK, this can not be done, or rather, I built some guesses, but for the application to work, I do not know how to do it. The interviewer said that it is possible and what you need to Google, I did not find anything in Google, please tell me.

Author: Иван Гладуш, 2017-11-17

2 answers

No. You cannot kill a GC programmatically. The interviewer is expecting some answer like:

The garbage collection cannot be forced. The garbage collector runs in low memory situations. When it runs, it releases the memory allocated by an unreachable object. The garbage collector runs on a low priority daemon (background) thread. You can nicely ask the garbage collector to collect garbage by calling System.gc() but you can�t force it.

Note: The only way you kill a GC is when you exit your application. System.exit();


No. You can't kill the GC from the code. The interviewer was waiting for an answer like this:

GC cannot be stopped. It starts working when there is little memory left. Starting to work, it frees up memory from forgotten (unreachable, those to which there are no references left) objects. The GC runs on a low-priority daemon thread. The only thing you can do is ask it to collect garbage by calling System.gc(), but you can't stop it.

P. s. Although you can, using one secret command from the development guru System. exit ();

Source code


It's the only thing I could find.

IMHO: GC is enabled when there is little memory left. If the program does not use all the memory, then it will not start working, you can say that it is dead, but it is not. And if all of them kill it in an unknown way, then you will know it by OutOfMemoryError and the application will stop


Following the idea of @ViacheslavVedenin, what will happen to GC if in finalize

  • stop the GC thread
  • loop the GC thread forever
  • throw exception

On the docks, I wrote myself @ViacheslavVedenin, and I decided to go through experiments. And no difference.

Normal operation log

Log from Thread.currentThread().stop(); to finalize

Log with while(true);

Log from throw new RuntimeException(); to finalize

To be honest, I did not notice much difference, so I think GC creating a new thread for finalize does not really care about it and does not follow. When it finishes working, the object will be deleted. The reason for which the thread is completed, he does not care, once it is completed, it means that it has done everything that it could.


I will also add the answers of my friend @Suvitruf, so that they are more noticeable:

 10
Author: Виктор, 2017-11-18 12:37:39

You can (though not kill it, but make it throw OutOfMemory and stop garbage collection, and the application will be able to continue working), here is an example:

import java.io.IOException;

public class Test1 {

    private static class TestClass {
        private byte[] bytes = new byte[100000];
        public int i = 0;
        public static volatile boolean flag = true;

        protected void finalize() throws IOException {
            int i = 1;
            while (flag) {
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestClass t1 = new TestClass();
        new Test1().test(0, t1);
    }

    private void test(int j, TestClass t1) throws InterruptedException {
        try {
            for (int i = 1; i <= 100000; i++) {
                TestClass t = new TestClass();
                System.out.println(i);
            }
        } catch (OutOfMemoryError e) {
            j++;
            t1.i = j;
            System.out.println("I alive!!! " + t1.i);
            if( j > 100) {
                TestClass.flag = false;
            }
            test(j, t1);
        }
    }
}

What's going on here? According to the specification, the collector must execute finalize before deleting objects, because of the infinite loop finalize, it will not be able to delete objects and will start throwing OutOfMemoryError (however, you can just use eternal references to objects). In this case, you will not be able to create new objects, but you can continue work with the old ones and the stack (see the code in catch (OutOfMemoryError e)). Including reusing existing objects, for example, saving new data in old objects (you can even write your own analogs with the new and delete variables, using byte arrays as data storage and "serialized" objects).

After some time, we disable the infinite loop in finalizers and we have garbage collection and object creation performed as usual.

 4
Author: Slava Vedenin, 2017-11-17 22:54:26