1

I am trying to upgrade an eclipse project from jdk 1.6 to jdk 1.7 .

The following error shows up during compilation for

import sun.management.ManagementFactory;

The type sun.management.ManagementFactory is not visible.

I tried importing java.lang.management.ManagementFactory to resolve the error; but then the following line of code is throwing an error.

ManagementFactory.getDiagnosticMXBean().dumpHeap

Error : The method getDiagnosticMXBean() is undefined for the type ManagementFactory.

Can someone please help understand what is causing this issue and how to resolve it?

webermaster
  • 209
  • 1
  • 13
IS_EV
  • 988
  • 2
  • 15
  • 29
  • 3
    Why are you explicitly using an internal, non-public API? – chrylis -cautiouslyoptimistic- Dec 30 '13 at 21:23
  • @chrylis : I am new to this project and have no real idea why this was used. From what I see , the purpose of the code is to look at the Heap memory usage and if it is above a specific percentage , the application tries to invoke the gc (using .gc() method in runtime) and also does a dump of the heap. – IS_EV Dec 30 '13 at 21:30
  • 1
    The only correct way to resolve the problem is to eliminate the use of internal APIs. Not only can they change between versions, but simply trying to run on a different VM might cause classloader errors. – chrylis -cautiouslyoptimistic- Dec 30 '13 at 21:32
  • 1
    I think this answer may also apply here: http://stackoverflow.com/a/4184068/772385 In general it's not a great idea to monkeying around with the internal APIs of the JVM like this. To analyze the heap it might be worth looking at something like Eclipse MAT(http://www.eclipse.org/mat/) or VisualVM(http://visualvm.java.net/) – Durandal Dec 30 '13 at 21:35

1 Answers1

2

In Java 7 (using officially released java.lang.management.ManagementFactory) you may want to try the combination of getMemoryMXBean() with getHeapMemoryUsage() as following:

ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

Please note: the above has not been tested.

EDIT:

System.out.println(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString());

actually works.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111