views:

108

answers:

4

As we know the java's garbage collector is a low priority thread. And in java we can create any thread with high priority. So is it possible to have our own customized garbage collector thread with variable priority (we can set depending on the level of memory management).

Did anybody tried that. If yes can you share some knowledge about how and its logic? Thanks

A: 

I believe you'd have to deploy your own JRE/JVM. There is no way to turn off the default garbage collection in Java.

Java 7 is going to use a completely new GC, so it should be possible to just swap them out.

jonescb
there is a selection of different garbage collectors, and you get to pick the one most appropriate for your application, however.
Will
+5  A: 

Java's garbage collector isn't "a low priority thread." For example, garbage collection often uses multiple threads in parallel. And, when memory runs low, garbage collection can preempt any application thread.

I recommend studying the Garbage Collection Tuning guide to determine the type of garbage collector best suited to an application, and how to adjust its parameters.

erickson
+3  A: 

Garbage collection in Java has been finely tuned for years. It's possible but unlikely that you're doing something very special that has special needs in this area.

But what I think is more likely is that whatever problem you think you're having with the garbage collector are problems in your application. Some more guesses:

  • You are unnecessarily creating and discarding a lot of objects.
  • You have memory leaks.
  • You have performance problems in your application.
  • You have impossible performance expectations.
Carl Smotricz
True, the garbage collector bundled with the JVM has undergone many many revisions and upgrades. Your best bet would be to analyze your application. Also, related to performance, you could try setting the JVM to server mode instead of client mode; this can give you improved performance. See http://www.coderanch.com/t/385923/Java-General/java/JVM-Server-Mode-vs-Client
Jon
A: 

Yes you can, but it requires you to do some rather hard core JVM work, as you cannot do it from the running Java program. The easiest way to get started is probably using the Jikes RVM as it is written in Java (an IBM project gone Open Source).

http://jikesrvm.org/

Have fun, but expect an uphill battle :)

Thorbjørn Ravn Andersen