package org.jfox.system.monitor;

import org.jfox.framework.extra.ActiveComponent;
import org.jfox.framework.extra.Managable;
import org.jfox.framework.extra.ManagementInfo;
import org.jfox.framework.extra.ManagementOperationInfo;
import org.jfox.system.AbstractComponent;

/**
 * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
 */

public class JVMMonitor extends AbstractComponent implements Managable, ActiveComponent {

    protected void doInit() throws Exception {

    }

    protected void doDestroy() throws Exception {

    }

    public long getTotalMemory() {
        return (long) (Runtime.getRuntime().totalMemory() / 1024);
    }

    public long getFreeMemory() {
        return (long) (Runtime.getRuntime().freeMemory() / 1024);
    }

    public long getMaxMemory() {
        return (long) (Runtime.getRuntime().maxMemory() / 1024);
    }

    public int getThreadCount() {
        return Thread.activeCount();
    }

    public void gc() {
        Runtime.getRuntime().gc();
    }

    public ManagementInfo getManagementInfo() {
        ManagementInfo info = ManagementInfo.exposeGet(this.getClass());
        try {
            info.addOperationInfo(new ManagementOperationInfo(this.getClass().getMethod("gc", new Class[0])));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static void main(String[] args) {

    }
}

