This discussion is archived
5 Replies Latest reply: Feb 16, 2012 7:06 AM by gimbal2 RSS

Clarification on G1 Garbage collection in JDK 7

Manjuvp Newbie
Currently Being Moderated
I have written a simple program to compare CMS GC and G1 GC. I am using Visual VM to visualize the used heap

public class GCTestInfiniteLoopString {  
public static void main(String[] args) throws InterruptedException {
String str = "";
Thread.sleep(5000);
for (int i=0;;i++) {
str = str+i;//New string is added to the String pool
}
}
}

When I used the following command -
java -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:GCTimeRatio=49 -XX:MaxGCPauseMillis=50 GCTestInfiniteLoopString
highest used heap is 53MB

When I used the following command -
java -Xmx512m -XX:+UseG1GC -XX:+PrintGCDetails -Xloggc:G1GC.log -XX:GCTimeRatio=49 -XX:MaxGCPauseMillis=50 GCTestInfiniteLoopString
highest used heap is 350MB

When I reduced the max heap size t0 -Xmx256m,-Xmx128m, the used memory come down considerably
Why is the used heap different when I use G1 collector?

The only logical explanation that I could think of is that when a region is used by JVM, entire region space is considered “used”.
For example a Region space is of 20 MB. Even though only 1 MB of space is used the Used memory in Visual VM shows as 20 MB.
Is my understanding correct?
  • 1. Re: Clarification on G1 Garbage collection in JDK 7
    gimbal2 Guru
    Currently Being Moderated
    914935 wrote:
    Why is the used heap different when I use G1 collector?
    You confuse "used heap" with "reserved heap". It might be that the JVM reserves larger blocks of memory to for example reduce fragmentation. But that's me spewing baseless conjecture.
  • 2. Re: Clarification on G1 Garbage collection in JDK 7
    Manjuvp Newbie
    Currently Being Moderated
    This is the Visual VM result for the test with G1 Collector
    Size = 512MB
    Max = 512MB
    Used = 350MB(highest point)

    So i believe the reserved space is 512 MB
  • 3. Re: Clarification on G1 Garbage collection in JDK 7
    gimbal2 Guru
    Currently Being Moderated
    Good point, I retract my earlier statement.

    But I can still be partially right. For example Windows 7 tends to use up spare system memory for its own purposes; as long as nobody or nothing is using it, why not? Who is to say that the JVM doesn't do something similar. The fact that it doesn't go above 350mb just passed out hints, it being 70% of the total available space. Even if I keep spouting guesses, that is also my point; you can't really make any assumptions about how things happen or should happen. The JVM is a complex piece of machinery and who knows what tricks have been built in to make it as efficient as it is. The fact that you see high heap space usage isn't necessarily a sign that something is up; getting out of memory errors is more proof of that.

    But then there are people like EJP and JVerd who have been around the block far longer than I have; I'm sure this thread will catch their eyes sooner or later and I'll probably be sent to stand in the newbie corner again :)
  • 4. Re: Clarification on G1 Garbage collection in JDK 7
    Manjuvp Newbie
    Currently Being Moderated
    >
    Good point, I retract my earlier statement.

    But I can still be partially right. For example Windows 7 tends to use up spare system memory for its own purposes; as long as nobody or nothing is using it, why not? Who is to say that the JVM doesn't do something similar. The fact that it doesn't go above 350mb just passed out hints, it being 70% of the total available space. Even if I keep spouting guesses, that is also my point; you can't really make any assumptions about how things happen or should happen. The JVM is a complex piece of machinery and who knows what tricks have been built in to make it as efficient as it is. The fact that you see high heap space usage isn't necessarily a sign that something is up; getting out of memory errors is more proof of that.

    But then there are people like EJP and JVerd who have been around the block far longer than I have; I'm sure this thread will catch their eyes sooner or later and I'll probably be sent to stand in the newbie corner again :)
    Hi Gimbal2.. Thanks for your reply. Interesting observation... 70% of the total space is true for CMS collector as well.
    However, When I change the region Space -XX:G1HeapRegionSize=4M for G1 collector, the used space is reduced to 250 MB.

    As for Spare system memory for Windows 7(and why JVM doesn't do that), I have my own reservations on that point. When I use CMS collector, the max allocated heap is 65 MB, and used is 40 MB. I think it has got to do with the region HeapRegionSize , because when I decrese the HeapRegionSize , the used heap is lower.
  • 5. Re: Clarification on G1 Garbage collection in JDK 7
    gimbal2 Guru
    Currently Being Moderated
    914935 wrote:
    As for Spare system memory for Windows 7(and why JVM doesn't do that), I have my own reservations on that point. When I use CMS collector, the max allocated heap is 65 MB, and used is 40 MB. I think it has got to do with the region HeapRegionSize , because when I decrese the HeapRegionSize , the used heap is lower.
    That is "a" result that you see. Without making any accusations or assumptions about what you are thinking, I repeat my point: you may also see other results such as poorer GC performance as well. You can't really know what impact you are really making by looking at single statistics only.

    I'll jump out now by stating one last thing: I've never had to deal with these command line switches myself and I've never had troubles with performance or resources even under heavy load. Out of the box the modern (server) JVM does really well on stock settings IMO, I see no need to try and get in its way myself. That being said, I would still be interested to see what the more seasoned Java developers that visit this forum have to comment on all this.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points