V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
linxiaoziruo
V2EX  ›  Java

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

  •  
  •   linxiaoziruo · Nov 10, 2020 · 1662 views
    This topic created in 2002 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   ·   2557 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 05:29 · PVG 13:29 · LAX 22:29 · JFK 01:29
    ♥ Do have faith in what you're doing.