Method Area Expansion Demonstration

This section provides some details of about HotSpot Method Area and an example program to demonstrate the expansion of the Method Area.

From the previous section, we learned that JVM runtime has 6 types of data areas: Method Area, Heap, Direct Memory, PC Register, JVM Stack and Native Method Stack.

Here are my understandings of the first data area, Method Area, based readings available on the Internet:

To validate some of these understandings and demonstrate the Method Area expansion, I wrote the following sample program, MethodAreaLoadJar.java, which loads all classes from the rt.jar:

/* MethodAreaLoadJar.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.io.Console;
import java.util.*;
import java.util.jar.*;
class MethodAreaLoadJar {
   public static void main(String[] a) {
      heapCheck();
      loadRtJar();
      heapCheck();
      Runtime rt = Runtime.getRuntime();
      rt.gc();
      heapCheck();
   }
   public static void loadRtJar() {
      String jarName = System.getProperty("java.home")
         +"\\lib\\"+"rt.jar";
      System.out.println("Loading all classes in: "+jarName);
      ClassLoader loader = ClassLoader.getSystemClassLoader();
      try {
     	 JarFile jar = new JarFile(jarName); 
         Enumeration all = jar.entries();
         int count = 0;
         while (all.hasMoreElements()) {
            JarEntry one = (JarEntry) all.nextElement();
            String name = one.getName();
            if (name.indexOf(".class")>=0) {
               name = name.replaceAll(".class$", "");
               name = name.replaceAll("/", ".");
               loader.loadClass(name);
               count++;
            }
         }
         System.out.println("# of classes loaded: "+count);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static void heapCheck() {
      Runtime rt = Runtime.getRuntime();
      Console con = System.console();
      long total = rt.totalMemory();
      long free = rt.freeMemory();
      long used = total - free;
      con.format("Total memory: %s%n",total);
      con.format(" Free memory: %s%n",free);
      con.format(" Used memory: %s%n",used);
      String str = con.readLine("Press ENTER key to continue: ");
   }
}

This program is designed to:

Test results are discussed in the next section.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

JVM Runtime Data Areas

 What Are Runtime Data Areas?

Method Area Expansion Demonstration

 OutOfMemoryError on the Method Area

 Method Area Growth with Dynamically Generated Classes

 Garbage Collection Issue with Dynamically Generated Classes

 Interned Strings Stored in Heap

 Heap Expansion Demonstration

 Direct Memory Expansion Demonstration

 allocateMemory() Method on Direct Memory

 JVM Stack Expansion Demonstration

 PC Register and Native Method Stack

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 Outdated Tutorials

 References

 PDF Printing Version