简体   繁体   中英

Stop scheduled task in JavaAgent


I trying to make simple java profiler and using javaagent to monitor memory usage. This is part of realization of my 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.
Does anyone knows how to stop javaagent after end of execution of the application?

Use Timer time = new Timer(true); this will make the TimerTasks become daemon. Daemon threads are finished when the main thread exit. More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM