Skip to content

Commit c50ccb8

Browse files
committed
Fix GC.stat on Native Image
* ManagementFactory.getMemoryPoolMXBeans() is empty on Native Image
1 parent 31f8c4c commit c50ccb8

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/main/java/org/truffleruby/core/GCNodes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.lang.management.MemoryPoolMXBean;
1515
import java.lang.management.MemoryUsage;
1616
import java.time.Duration;
17+
import java.util.Arrays;
1718

1819
import com.oracle.truffle.api.dsl.Cached;
1920
import org.jcodings.specific.UTF8Encoding;
@@ -178,6 +179,8 @@ protected RubyArray stat(
178179

179180
// Get memory usage values from relevant memory pools (2-3 / ~8 are relevant)
180181
memoryPools = new Object[memoryPoolNames.length];
182+
// On Native Image, ManagementFactory.getMemoryPoolMXBeans() is empty
183+
Arrays.fill(memoryPools, nil);
181184
for (int i = 0; i < memoryPoolNames.length; i++) {
182185
String memoryPoolName = memoryPoolNames[i];
183186
for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {

src/main/ruby/truffleruby/core/gc.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,29 @@ def self.stat(option = nil)
102102
heap_free_slots: committed - used,
103103
}
104104

105-
(0...memory_pool_names.length).each do |i|
105+
memory_pool_names.each_with_index do |memory_pool_name, i|
106106
# Populate memory pool specific stats
107107
info = memory_pool_info[i]
108-
stat[memory_pool_names[i]] = {
109-
committed: info[0],
110-
init: info[1],
111-
max: info[2],
112-
used: info[3],
113-
peak_committed: info[4],
114-
peak_init: info[5],
115-
peak_max: info[6],
116-
peak_used: info[7],
117-
last_committed: info[8],
118-
last_init: info[9],
119-
last_max: info[10],
120-
last_used: info[11],
121-
}
122-
123-
# Calculate stats across memory pools
124-
stat[memory_pool_names[i]].each_pair do |key, value|
125-
stat[key] += value
108+
if info
109+
stat[memory_pool_name] = data = {
110+
committed: info[0],
111+
init: info[1],
112+
max: info[2],
113+
used: info[3],
114+
peak_committed: info[4],
115+
peak_init: info[5],
116+
peak_max: info[6],
117+
peak_used: info[7],
118+
last_committed: info[8],
119+
last_init: info[9],
120+
last_max: info[10],
121+
last_used: info[11],
122+
}
123+
124+
# Calculate stats across memory pools
125+
data.each_pair do |key, value|
126+
stat[key] += value
127+
end
126128
end
127129
end
128130

0 commit comments

Comments
 (0)