1

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

8
  • 4
    You can't disable the garbage collector. Unless you mean something else? Apr 30, 2010 at 14:00
  • 4
    If you want to manage your own memory, use a different language and platform for your application.
    – Yishai
    Apr 30, 2010 at 14:21
  • If it were possible to disable GC then I could do it in Java by using my own caches. Why can't GC be disabled by a flag of some sort?
    – Dan
    Apr 30, 2010 at 14:27
  • @Dan, Java has no way of freeing memory, with your own caches or otherwise. If you disabled GC, then the memory footprint would grow (probably surprisingly quickly) and the JVM would crash very quickly.
    – Yishai
    Apr 30, 2010 at 15:07
  • 2
    @Dan: If you really need predictable real time behaviour, you should take a look at Java RTS: java.sun.com/javase/technologies/realtime/index.jsp . You will not be able to get where you want with the standard Java VM (although I'm not 100% sure if you really know what you want).
    – jarnbjo
    Apr 30, 2010 at 16:06

8 Answers 8

9

What you are trying to achieve is frequently done in investment banking to develop low-latency real-time systems.

To avoid GC you simply need to make sure not to allocate memory after the startup and warm-up phase of your application. As you seem to have noticed Java NIO internally does unwanted memory allocation. Unfortunately, you have no choice but write JNI replacements for the problematic calls. You need at least to write a replacement for the NIO Selector.

You will have to avoid using most of the Java libraries due to similar unwanted memory allocations. For example you will have to avoid using immutable object like String, avoid Boxing, re-implement Collections that preallocate enough entries for the whole lifetime of your program.

Writing Java code this way is not easy, but certainly possible. I am developing a platform to do just so.

1
  • is there any sort of alternative for the NIO Selector? I wrote a multicast udp receiver thinking that the multiplex would be great for not needing to handle multithreading, but I m not anywhere close to no gc. Is the only option to rewrite using a multicastSocket? Mar 2 at 9:16
8

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.

9
  • 1
    No I would always use a cache to reuse objects.
    – Dan
    Apr 30, 2010 at 14:22
  • What about temporary objects that you don't see or control? There's a lot going on inside the various libraries that aren't going to know squat about your memory scheme.
    – Joe
    Apr 30, 2010 at 14:31
  • That may be the case but the library code you'll be using for I/O will be probably be creating objects somewhere. Also, have you actually established this is a problem? Avoiding object creation for the sake of speed is a "code smell" IMO. Most of the time your app will be I/O bound anyway, not CPU bound.
    – Adamski
    Apr 30, 2010 at 14:34
  • Agreed but my main issue is that I don't want any GC at all - its all about predictability not straight line speed.
    – Dan
    Apr 30, 2010 at 14:37
  • 2
    @Dan, you just need to be using a different language. Sounds like C or C++ are the obvious choices here, although you could probably hack enough of a solution in .NET with unmanaged code. With modern JVMs you can minimize the "program stops responding" situation, but you cannot eliminate it - and you cannot not use the GC.
    – Yishai
    Apr 30, 2010 at 15:11
4

It's very hard (but not impossible) to disable GC in a JVM.

Look at the JNI "critical" functions for hints.

You can also essentially ensure you don't GC by not allocating any more objects (write a JVMTI agent that slaps you if you do, and instrument your code).

Finally, you can force a fatal OutOfMemoryError by ensuring that every object you allocate is never freed, thus when you hit -Xmx memory used, you'll fall over as GC won't be able to reclaim anything (mind you, you'll GC one or more times at this point before you fall over in a heap).

The real question is why you'd want to? What upside do you see in doing it? Is it for realtime? If so, I'd consider looking at one of the several realtime JVMs available on the market (Oracle, IBM, & others all sell them). I can't honestly think of another reason to do this while still using Java.

1

The only way you are going to be able to turn off garbage collection is to modify the JVM. This is should be feasible with OpenJDK 6 codebase.

However, the what you will get at the end is a JVM that leaks memory like crazy, with no reasonable hope of fixing the leaks. The Java class library APIs are designed and implemented on the assumption that there is a GC taking care of memory management. This is so fundamental that any serious attempt to "fix" it would lead to a language / library that is not recognizable as Java.

If you want a non-garbage collected language, use C or C++.

1

Modern JVM's are so good at handling short-lived objects that any scheme you devise on your own will be slower.

This is because the objects you handle yourself will become long-lived and receive extra deluxe treatment from the JVM in terms of being moved around etc. Of course, this is by the garbage collector, which you want to turn off, but you can do very little without any gc.

So, before you start considering what optimization to use, then establish a baseline where you have a large unoptimized, program and profile it. Then do your tweaks, and see if it helps, but you will never know if you do not have a baseline.

1

As other people have mentioned you can't disable the GC. However, you can choose to use the experimental 'Epsilon' garbage collector which will never actually perform any garbage collections. Warning: it will crash if your JVM runs out of memory (because it's not doing any garbage collections).

There's more info (including the command-line switch to use) at:

http://openjdk.java.net/jeps/318

Good luck!

0

GarbageCollection is automated memory management in java.So you can not disable GC

1
  • I thought there was some strategy of flags that could be set to ensure that nothing was garbage collected - maybe I'm wrong.
    – Dan
    Apr 30, 2010 at 14:23
0

Since you say, "its all about predictability not straight line speed," you should look at using a realtime Java system with deterministic garbage collection.

1
  • That link is dead
    – ACyclic
    Apr 10, 2019 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.