From 8a125d9913ec5c7f3647e87f3befed3b58b1f3e0 Mon Sep 17 00:00:00 2001
From: Antonios Printezis <tonyp@openjdk.org>
Date: Fri, 4 Dec 2009 07:44:35 -0500
Subject: [PATCH] 6880903: G1: G1 reports incorrect Runtime.maxMemory()

G1 reports committed memory instead of reserved memory from the Runtime.maxMemory() method

Reviewed-by: ysr, jmasa
---
 .../gc_implementation/g1/g1CollectedHeap.cpp  |  2 +-
 .../gc_implementation/g1/g1CollectedHeap.hpp  |  2 +-
 .../src/share/vm/services/g1MemoryPool.cpp    | 29 ++++++++++---------
 .../src/share/vm/services/g1MemoryPool.hpp    |  3 ++
 4 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp b/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp
index f9cce8b4e6a..0acb1761096 100644
--- a/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp
+++ b/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp
@@ -2130,7 +2130,7 @@ size_t G1CollectedHeap::large_typearray_limit() {
 }
 
 size_t G1CollectedHeap::max_capacity() const {
-  return _g1_committed.byte_size();
+  return g1_reserved_obj_bytes();
 }
 
 jlong G1CollectedHeap::millis_since_last_gc() {
diff --git a/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp b/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp
index 5e07828267f..3c071beed9a 100644
--- a/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp
+++ b/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp
@@ -692,7 +692,7 @@ public:
 
   // Reserved (g1 only; super method includes perm), capacity and the used
   // portion in bytes.
-  size_t g1_reserved_obj_bytes() { return _g1_reserved.byte_size(); }
+  size_t g1_reserved_obj_bytes() const { return _g1_reserved.byte_size(); }
   virtual size_t capacity() const;
   virtual size_t used() const;
   // This should be called when we're not holding the heap lock. The
diff --git a/hotspot/src/share/vm/services/g1MemoryPool.cpp b/hotspot/src/share/vm/services/g1MemoryPool.cpp
index 1a1c337a036..9a124da7a5a 100644
--- a/hotspot/src/share/vm/services/g1MemoryPool.cpp
+++ b/hotspot/src/share/vm/services/g1MemoryPool.cpp
@@ -96,7 +96,7 @@ size_t G1MemoryPoolSuper::old_space_used(G1CollectedHeap* g1h) {
 
 // See the comment at the top of g1MemoryPool.hpp
 size_t G1MemoryPoolSuper::old_space_max(G1CollectedHeap* g1h) {
-  size_t max = g1h->g1_reserved_obj_bytes();
+  size_t max = overall_max(g1h);
   size_t eden_max = eden_space_max(g1h);
   size_t survivor_max = survivor_space_max(g1h);
   max = subtract_up_to_zero(max, eden_max);
@@ -113,11 +113,12 @@ G1EdenPool::G1EdenPool(G1CollectedHeap* g1h) :
 }
 
 MemoryUsage G1EdenPool::get_memory_usage() {
-  size_t maxSize   = max_size();
-  size_t used      = used_in_bytes();
-  size_t committed = eden_space_committed();
+  size_t initial_sz = initial_size();
+  size_t max_sz     = max_size();
+  size_t used       = used_in_bytes();
+  size_t committed  = eden_space_committed();
 
-  return MemoryUsage(initial_size(), used, committed, maxSize);
+  return MemoryUsage(initial_sz, used, committed, max_sz);
 }
 
 G1SurvivorPool::G1SurvivorPool(G1CollectedHeap* g1h) :
@@ -129,11 +130,12 @@ G1SurvivorPool::G1SurvivorPool(G1CollectedHeap* g1h) :
 }
 
 MemoryUsage G1SurvivorPool::get_memory_usage() {
-  size_t maxSize   = max_size();
-  size_t used      = used_in_bytes();
-  size_t committed = survivor_space_committed();
+  size_t initial_sz = initial_size();
+  size_t max_sz     = max_size();
+  size_t used       = used_in_bytes();
+  size_t committed  = survivor_space_committed();
 
-  return MemoryUsage(initial_size(), used, committed, maxSize);
+  return MemoryUsage(initial_sz, used, committed, max_sz);
 }
 
 G1OldGenPool::G1OldGenPool(G1CollectedHeap* g1h) :
@@ -145,9 +147,10 @@ G1OldGenPool::G1OldGenPool(G1CollectedHeap* g1h) :
 }
 
 MemoryUsage G1OldGenPool::get_memory_usage() {
-  size_t maxSize   = max_size();
-  size_t used      = used_in_bytes();
-  size_t committed = old_space_committed();
+  size_t initial_sz = initial_size();
+  size_t max_sz     = max_size();
+  size_t used       = used_in_bytes();
+  size_t committed  = old_space_committed();
 
-  return MemoryUsage(initial_size(), used, committed, maxSize);
+  return MemoryUsage(initial_sz, used, committed, max_sz);
 }
diff --git a/hotspot/src/share/vm/services/g1MemoryPool.hpp b/hotspot/src/share/vm/services/g1MemoryPool.hpp
index 73c12adfb9c..12b3662a1c7 100644
--- a/hotspot/src/share/vm/services/g1MemoryPool.hpp
+++ b/hotspot/src/share/vm/services/g1MemoryPool.hpp
@@ -137,6 +137,9 @@ protected:
   static size_t overall_used(G1CollectedHeap* g1h) {
     return g1h->used_unlocked();
   }
+  static size_t overall_max(G1CollectedHeap* g1h) {
+    return g1h->g1_reserved_obj_bytes();
+  }
 
   static size_t eden_space_committed(G1CollectedHeap* g1h);
   static size_t eden_space_used(G1CollectedHeap* g1h);