Saturday, February 15, 2014

How to know how much memory is occupied/free/available from JVM level

import com.eibus.util.logger.CordysLogger;
import com.eibus.util.logger.Severity;

private static Runtime memObj = Runtime.getRuntime();
private static final CordysLogger fileLog = CordysLogger.getCordysLogger(Utilities.class);

fileLog.log(Severity.ERROR, "For the loop "+loop+"--Before firing get Objects: Total Memory : "+getMemory("total")+"--Available Memory: "+getMemory("available")+"--Occupied Memory is "+getMemory("occupied"));

private static long getMemory(String type){
    int mb = 1024*1024;
    long mem = 0;
    if(type.equalsIgnoreCase("total"))
    mem = memObj.totalMemory()/mb;
    else if(type.equalsIgnoreCase("available"))
    mem = memObj.freeMemory()/mb;
    else
    mem = (memObj.totalMemory()-memObj.freeMemory())/mb;
    return mem;
    }

No comments:

Post a Comment