Skip to content

Commit

Permalink
Fix possible IAE in getHeapUsage()
Browse files Browse the repository at this point in the history
The implementation of WorkflowTest#getHeapUsage() is modified to avoid the possibility of summing in individual pools which return `-1` for `#getMax()`

Fixes #14
  • Loading branch information
sroughley authored Mar 5, 2024
1 parent 1fa09ac commit 7109fff
Showing 1 changed file with 1 addition and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
package nl.esciencecenter.e3dchem.knime.testing.core.ng;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;

import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -120,20 +118,6 @@ public void aboutToStart() {
protected static MemoryUsage getHeapUsage() {
System.gc();

long initMem = 0;
long maxMem = 0;
long committedMem = 0;
long usedMem = 0;
for (MemoryPoolMXBean memoryPool : ManagementFactory.getMemoryPoolMXBeans()) {
if (memoryPool.getType().equals(MemoryType.HEAP) && (memoryPool.getCollectionUsage() != null)) {
MemoryUsage usage = memoryPool.getUsage();

initMem += usage.getInit();
maxMem += usage.getMax();
committedMem += usage.getCommitted();
usedMem += usage.getUsed();
}
}
return new MemoryUsage(initMem, usedMem, committedMem, maxMem);
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
}
}

0 comments on commit 7109fff

Please sign in to comment.