[英]Stop scheduled task in JavaAgent
I trying to make simple java profiler and using javaagent to monitor memory usage. 我试图制作简单的java分析器并使用javaagent来监视内存使用情况。 This is part of realization of my javaagent: 这是我的javaagent实现的一部分:
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Timer;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;
public class Transformer implements ClassFileTransformer {
private Notifier notifier;
private Instrumentation inst;
private ArrayList<Class> loadedClasses;
public Transformer(Instrumentation inst) {
this.inst = inst;
loadedClasses = new ArrayList<Class>();
initNotifier();
Timer time = new Timer();
MemoryMonitor mm = new MemoryMonitor(notifier);
time.schedule(mm, 0, 1000);
}
public static void premain(String args, Instrumentation inst) {
inst.addTransformer(new Transformer(inst));
}
private void initNotifier() {
if (notifier != null) return;
try {
Registry registry = LocateRegistry.getRegistry(Const.registryPort);
notifier = (Notifier) registry.lookup(Const.stubName);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
public class MemoryMonitor extends TimerTask {
private Notifier notifier;
public MemoryMonitor(Notifier notifier) {
this.notifier = notifier;
}
@Override
public void run() {
try {
notifier.memoryUsed(monitorMemory());
} catch (Exception e) {
e.printStackTrace();
}
}
private double monitorMemory() {
List memBeans = ManagementFactory.getMemoryPoolMXBeans();
double used = 0;
for (Iterator i = memBeans.iterator(); i.hasNext(); ) {
MemoryPoolMXBean mpool = (MemoryPoolMXBean) i.next();
MemoryUsage usage = mpool.getUsage();
used = usage.getUsed() / 1000;
}
return used;
}
}
This code works good and sends data about memory usage to the profiling tool. 此代码运行良好,并将有关内存使用情况的数据发送到分析工具。 But javaagent don't stops after end of execution of the application. 但javaagent在应用程序执行结束后不会停止。
Does anyone knows how to stop javaagent after end of execution of the application? 有谁知道如何在应用程序执行结束后停止javaagent?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.