JDK-7173044 : Memory monitor demo hangs the system if MemoryUsage obj returns -1 .
  • Type: Enhancement
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-05-31
  • Updated: 2013-07-23
  • Resolved: 2012-06-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u40Fixed 8 b43Fixed
Description
A DESCRIPTION OF THE REQUEST :
In MemoryMonitor.java: plotMemoryUsage method.

This piece of code  "
 MemoryPoolMXBean mp = mpools.get(npool);
 float usedMemory =  mp.getUsage().getUsed();
 float totalMemory =  mp.getUsage().getMax();  "

 is followed by

 "
 // .. Memory Free ..
 big.setColor(mfColor);
 int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10);
 int i = 0;
 for ( ; i < MemUsage ; i++) {
        mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight,
                 blockWidth, blockHeight-1);
        big.fill(mfRect);
 }
"

If totalMemory is -1, MemUsage will become a very large value, and the demo hangs in the loop.

In order to reproduce the problem, another jdk other than ri is required because these beans are jdk dependent. It can be reproduced using ibm jdk to run the MemoryMonitor demo.


JUSTIFICATION :
Spec says it is allowed for MemoryUsage.getMax to return -1 if the maximum memory size is undefined.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The demo is still working when MemoryUsage.getMax returns -1 .
ACTUAL -
The demo hangs.

Comments
EVALUATION http://hg.openjdk.java.net/jdk8/tl/jdk/rev/4573662cb28c
05-06-2012

SUGGESTED FIX src/share/demo/management/MemoryMonitor/MemoryMonitor.java *** 120,129 **** --- 120,130 ---- private BufferedImage bimg; private Graphics2D big; private Font font = new Font("Times New Roman", Font.PLAIN, 11); private int columnInc; private float usedMem[][]; + private float usedMemMax[]; private int ptNum[]; private int ascent, descent; private Rectangle graphOutlineRect = new Rectangle(); private Rectangle2D mfRect = new Rectangle2D.Float(); private Rectangle2D muRect = new Rectangle2D.Float(); *** 140,149 **** --- 141,154 ---- public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); usedMem = new float[numPools][]; + usedMemMax = new float[numPools]; + for (int i = 0; i < numPools; i++) { + usedMemMax[i] = 1024f * 1024f ; + } ptNum = new int[numPools]; } @Override public Dimension getMinimumSize() { *** 192,201 **** --- 197,212 ---- public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) { MemoryPoolMXBean mp = mpools.get(npool); float usedMemory = mp.getUsage().getUsed(); float totalMemory = mp.getUsage().getMax(); + if (totalMemory < 0) { + if (usedMemory > usedMemMax[npool]) { + usedMemMax[npool] = usedMemory; + } + totalMemory = usedMemMax[npool]; + } // .. Draw allocated and used strings .. big.setColor(Color.green); // Print Max memory allocated for this memory pool.
04-06-2012

EVALUATION The demo should anticipate that getMax() can return -1.
04-06-2012