How to determine occupied space

I want to create a MemoryWarningSystem as described in many articles: for example, as in http://www.javaspecialists.eu/archive/Issue092.html .

So I want to identify the space used like this:

    private MemoryPoolMXBean findTenuredGenPool() {
        for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {

                 if(pool is tenured space)
                    return tenured

        }
    }

I saw two different ways to identify the occupied space.

  • Check if the pool name matches "PS Old Gen" and / or "Tenured Gen".
  • Make sure " pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()"

Problem with 1 : what about CMS Old Gen? What about other spaces? Should I add them all to the list of assigned names?

Problem with 2 . Is this a "safe" way to retrieve the space on which data is stored? Is it reliable?

Thank!

+5
1

JSM HotSpot

  • Tenured Gen
  • PS Old Gen
  • CMS Old Gen
  • G1 Old Gen

JRockit

  • Old Space
+1

All Articles