Retrieve the maximum JVM heap size

 

+

Search Tips   |   Advanced Search

 

The JVM_Info class retrieves the maximum heap size of the JVM.

package test;

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;

import java.net.*;

/**
 * The JVM_Info class retrieves the maximum heap size of the JVM. 
 * It writes a message in the test log with the hostname where the
 * JVM is running and the JVM's maximum heap size in megabytes.
 */

/**
 * @author IBM Custom Code Samples
 */

public class JVM_Info implements
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{

    public JVM_Info() 
    {
    }

    public String exec(ITestExecutionServices tes, String[] args) 
    {
        Runtime rt = Runtime.getRuntime();

        // maxMemory() size is in bytes
        long maxMB = rt.maxMemory()/(1024*1024); 

        String hostName = "Unknown";        
                
        try 
        {
            hostName = InetAddress.getLocalHost().getHostName();
        } 
        catch (UnknownHostException e1) 
        {
            tes.getTestLogManager().reportMessage("Can't get hostname");
            return null;
        }
                
        tes.getTestLogManager().reportMessage("JVM maximum heap size for host " 
                                             + hostName 
                                             + " is " 
                                             + maxMB 
                                             + " MB");
        return null;
     }
}