从jconsole摘出来的GC代码

import static java.lang.management.ManagementFactory.MEMORY_MXBEAN_NAME;
import static java.lang.management.ManagementFactory.newPlatformMXBeanProxy;


import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;


import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;




class SnapshotInvocationHandler implements InvocationHandler {


private final MBeanServerConnection conn;
private Map<ObjectName, NameValueMap> cachedValues = newMap();
private Map<ObjectName, Set<String>> cachedNames = newMap();


@SuppressWarnings("serial")
private static final class NameValueMap extends HashMap<String, Object> {
}


SnapshotInvocationHandler(MBeanServerConnection conn) {
this.conn = conn;
}


synchronized void flush() {
cachedValues = newMap();
}


public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
final String methodName = method.getName();
if (methodName.equals("getAttribute")) {
return getAttribute((ObjectName) args[0], (String) args[1]);
} else if (methodName.equals("getAttributes")) {
return getAttributes((ObjectName) args[0], (String[]) args[1]);
} else if (methodName.equals("flush")) {
flush();
return null;
} else {
try {
return method.invoke(conn, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}


private Object getAttribute(ObjectName objName, String attrName)
throws MBeanException, InstanceNotFoundException,
AttributeNotFoundException, ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(objName,
Collections.singleton(attrName));
Object value = values.get(attrName);
if (value != null || values.containsKey(attrName)) {
return value;
}
// Not in cache, presumably because it was omitted from the
// getAttributes result because of an exception. Following
// call will probably provoke the same exception.
return conn.getAttribute(objName, attrName);
}


private AttributeList getAttributes(ObjectName objName, String[] attrNames)
throws InstanceNotFoundException, ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(objName,
new TreeSet<String>(Arrays.asList(attrNames)));
final AttributeList list = new AttributeList();
for (String attrName : attrNames) {
final Object value = values.get(attrName);
if (value != null || values.containsKey(attrName)) {
list.add(new Attribute(attrName, value));
}
}
return list;
}


private synchronized NameValueMap getCachedAttributes(ObjectName objName,
Set<String> attrNames) throws InstanceNotFoundException,
ReflectionException, IOException {
NameValueMap values = cachedValues.get(objName);
if (values != null && values.keySet().containsAll(attrNames)) {
return values;
}
attrNames = new TreeSet<String>(attrNames);
Set<String> oldNames = cachedNames.get(objName);
if (oldNames != null) {
attrNames.addAll(oldNames);
}
values = new NameValueMap();
final AttributeList attrs = conn.getAttributes(objName,
attrNames.toArray(new String[attrNames.size()]));
for (Attribute attr : attrs.asList()) {
values.put(attr.getName(), attr.getValue());
}
cachedValues.put(objName, values);
cachedNames.put(objName, attrNames);
return values;
}


// See http://www.artima.com/weblogs/viewpost.jsp?thread=79394
private static <K, V> Map<K, V> newMap() {
return new HashMap<K, V>();
}
}




class Snapshot {
    private Snapshot() {
    }
    public static SnapshotMBeanServerConnection
            newSnapshot(MBeanServerConnection mbsc) {
        final InvocationHandler ih = new SnapshotInvocationHandler(mbsc);
        return (SnapshotMBeanServerConnection) Proxy.newProxyInstance(
                Snapshot.class.getClassLoader(),
                new Class[] {SnapshotMBeanServerConnection.class},
                ih);
    }
}




interface SnapshotMBeanServerConnection extends MBeanServerConnection {
/**
* Flush all cached values of attributes.
*/
public void flush();
}
public class Main {


   public static void main(String args[]){

  gc();
  
   }




public static void gc() {
new Thread("MemoryPanel.gc") {
public void run() {
try {
MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
SnapshotMBeanServerConnection server = Snapshot.newSnapshot(mbsc);
MemoryMXBean memoryMBean;
memoryMBean = newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME,
                      MemoryMXBean.class);
memoryMBean.gc();
} catch (IOException e) {
e.printStackTrace();
}


}
}.start();
}
}

你可能感兴趣的:(从jconsole摘出来的GC代码)