linxiaoziruo
V2EX  ›  Java

类 jprofiler 是怎么在 jvm 运行时获得 jvm 相关信息的

  •  
  •   linxiaoziruo · Nov 10, 2020 · 1708 views
    This topic created in 2022 days ago, the information mentioned may be changed or developed.

    Jvm 启动的时候可以通过启动参数加载 agent 相关代码,从而获得 JVM 相关信息。 但是我看到 jprofiler 可以用 attach 的方式,在 jvm 运行的时候获得 Jvm 相关信息。这是怎么实现的,怎么实现这种无侵入式的获得 JVM 相关信息的?

    senninha
        1
    senninha  
       Nov 10, 2020
    另起一个普通的 Jvm 进程,获取目标进程的 pid,传入 agent jar 的路径,目标进程就会 load 进定制的 agnet jar 包。
    ```
    VirtualMachine vm = VirtualMachine.attach(pid);
    try {
    vm.loadAgent(agentPath, agentServerPath);
    } finally {
    vm.detach();
    }
    ```
    fantastM
        2
    fantastM  
       Nov 10, 2020
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3186 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    ♥ Do have faith in what you're doing.