Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection change from Java 1.4 to Java 6?

We have recently upgraded one of our applications from Java 1.4 to Java 6.

With some load & performance tests, we observed that available memory stayed in general at much lower levels in Java 6 than at what it used to be with Java 1.4.

After some profiling on the app with Java 6, we noticed that many objects no longer referenced by any other objects (i.e. candidates for garbage collection) stayed in memory and were apparently never garbage collected. We took that as the explanation for the lower available memory.

Question is: did the way garbage collection behaves changed from Java 1.4 to Java 6?

like image 791
b.roth Avatar asked Aug 20 '10 09:08

b.roth


People also ask

What is garbage collector in Java?

Garbage collector destroys these objects. The main objective of Garbage Collector is to free heap memory by destroying unreachable objects. The garbage collector is the best example of the Daemon thread as it is always running in the background. How Does Garbage Collection in Java works?

How does garbage collection make Java memory efficient?

It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.

How do I enable garbage collection logging for Java 8 and earlier?

There is a difference in terms of how you activate garbage collection logging for Java 8 and earlier and for the newer Java versions. For Java 8 and earlier you should add the following flags to your JVM-based application startup parameters: Where the PATH_TO_GC_LOG_FILE is the location of the garbage collector log file. For example:

What the JVM tells us about the garbage collector’s work?

By using logs we can understand what the JVM tells us about the garbage collectors’ work. The garbage collector log is a text file produced by the Java Virtual Machine that describes the work of the garbage collector. It contains all the information you could need to see how the memory cleaning process works.


2 Answers

did the way garbage collection behaves changed from Java 1.4 to Java 6?

Definitely!

Java 1.4 to Java 6 is a pretty long timespan (almost 5 years between the initial releases and more than 8 years between the initial 1.4 release and the current Java 6 release, according to this wiki article).

Many changes and optimizations are applied in that time and you should not really care as long as your program still works.

Having more used memory only means that the JVM doesn't waste time with garbage collection when it doesn't need to. If you want it to use less memory, then you should reduce the maximum heap (or otherwise tweak the JVM parameters; this article explains how to do that in Java 5, much of the advice is still applicable).

It's somewhat different if you actually get OutOfMemoryError that you didn't get previously. Then you should check how you use weak and soft references or as a last resort try to find out if you hit a JVM bug.

like image 96
Joachim Sauer Avatar answered Oct 04 '22 02:10

Joachim Sauer


There have been several optimizations on garbage collecting between 1.4 and 5 and between 5 and 6.

Oracle/Sun have some whitepapers on the performance differences online:

Java 5 Performance White Paper

Java 6 Performance White Paper

like image 38
Nikolaus Gradwohl Avatar answered Oct 04 '22 04:10

Nikolaus Gradwohl