Search code examples
javagarbage-collectionio

Java without gc - io


I would like to run a Java program with garbage collection switched off. Managing memory in my own code is not so difficult. However the program needs quite a lot of I/O. Is there any way (short of using JNI for all I/O operations) that I could achieve this using pure Java?

Thanks Daniel


Solution

  • Managing memory in my own code is not so difficult.

    It's not difficult - It's impossible. For example:

    public void foo() {
      Object o = new Object();
    
      // free(o); // Doh! No "free" keyword in Java.
    }
    

    Without the aid of the garbage collector how can the memory consumed by o be reclaimed?

    I'm assuming from your question that you might want to avoid the sporadic pauses caused by garbage collection due to the high level of I/O being performed by your app. If this is the case there are techniques for minimising the number of objects created (e.g. re-using objects from a pool). You could also consider enabling the Concurrent Mark Sweep Collector.

    The concurrent mark sweep collector, also known as the concurrent collector or CMS, is targeted at applications that are sensitive to garbage collection pauses.