java 如何调试JVM资源加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/750793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:37:46  来源:igfitidea点击:

How to debug JVM resources loading?

javadebuggingjvmprofilingclassloader

提问by aows

To debug class loading in a JVM we can use the param -verbose:class, but... Anyone knows how to debug resources loading (e.g. properties files)?

要在 JVM 中调试类加载,我们可以使用 param -verbose:class,但是... 有谁知道如何调试资源加载(例如属性文件)?

回答by Thilo

I suppose you need to look at using a profiler. Or something that uses the instrumentation interface.

我想您需要考虑使用分析器。或者使用仪器接口的东西。

Not sure how stable it is, but there is BTrace, which is kind of a Java version of DTrace.

不确定它有多稳定,但有BTrace,它是 DTrace 的 Java 版本。

BTrace is a safe, dynamic tracing tool for the Java platform. BTrace can be used to dynamically trace a running Java program. BTrace dynamically instruments the classes of the target application to inject tracing code ("bytecode tracing"). Tracing code is expressed in Java programming language.

BTrace 是一种用于 Java 平台的安全、动态的跟踪工具。BTrace 可用于动态跟踪正在运行的 Java 程序。BTrace 动态检测目标应用程序的类以注入跟踪代码(“字节码跟踪”)。跟踪代码用Java 编程语言表示。

If you are doing this on a development machine, and the number of events (resources being loaded) is not too frequent, you could also just set a breakpoint in the debugger.

如果您在开发机器上执行此操作,并且事件(正在加载的资源)数量不是太频繁,您也可以在调试器中设置断点。

回答by Tom Hawtin - tackline

Resources are provided as URLs. So, I guess to do it in "pure" Java: Install a custom ClassLoaderthat copies URLs into a version with a custom URLStreamHandler. Put your monitoring code in the stream handler and forward to the original.

资源作为URLs提供。所以,我想用“纯”Java 来做:安装一个自定义ClassLoader,将URLs复制到一个带有自定义URLStreamHandler. 将您的监控代码放在流处理程序中并转发到原始代码。

回答by mchr

You could use InTraceto instrument the Classloader classes which do the loading.

您可以使用InTrace来检测执行加载的Classloader类。

For the Sun JVM, I would suggest using the following Include patterns to trace appropriate classes:

对于 Sun JVM,我建议使用以下 Include 模式来跟踪适当的类:

  • ClassLoader
  • URLClassPath
  • Loader
  • 类加载器
  • URL类路径
  • 装载机

回答by rmoestl

There exist a couple of techniques to debug classloading problems. Here are good slides from the JRebel creatorssummarizing those techniques: Do you really get class loaders?

有几种技术可以调试类加载问题。下面是JRebel 创建者总结这些技术的好幻灯片:你真的有类加载器吗?

回答by Daniel H.

In a Linux environment you can try:

在 Linux 环境中,您可以尝试:

lsof -p <jvm pid>

It will give you a list with the descriptors used by the program associated with the specified pid.

它将为您提供一个列表,其中包含与指定 pid 关联的程序使用的描述符。

More Info

更多信息