java - 一种在 Spring 应用程序中创建和销毁 Prototype-beans 期间跟踪内存情况的方法?

标签 java spring memory-management prototype javabeans

在我们项目的一部分中,我们有一些原型(prototype) bean。我想清楚地了解执行该部分期间发生的情况。

有没有什么方法可以追踪原型(prototype)bean创建过程中的情况,他们开始使用多少内存,以及追踪这些bean是否被成功销毁?

我不仅需要在控制台打印信息,我还想查看实际情况内存或者内存中存在的prototype-beans列表。

最佳答案

如果您需要以编程方式执行此操作,请尝试使用此 public interface Instrumentation

This class provides services needed to instrument Java programming language code. Instrumentation is the addition of byte-codes to methods for the purpose of gathering data to be utilized by tools. Since the changes are purely additive, these tools do not modify application state or behavior. Examples of such benign tools include monitoring agents, profilers, coverage analyzers, and event loggers. There are two ways to obtain an instance of the Instrumentation interface:

When a JVM is launched in a way that indicates an agent class. In that case an Instrumentation instance is passed to the premain method of the agent class.

When a JVM provides a mechanism to start agents sometime after the JVM is launched. In that case an Instrumentation instance is passed to the agentmain method of the agent code.

示例 Instrumentation: querying the memory usage of a Java object :

public class MyAgent {
  private static volatile Instrumentation globalInstr;
  public static void premain(String args, Instrumentation inst) {
    globalInstr = inst;
  }
  public static long getObjectSize(Object obj) {
    if (globalInstr == null)
      throw new IllegalStateException("Agent not initted");
    return globalInstr.getObjectSize(obj);
  }
}

关于java - 一种在 Spring 应用程序中创建和销毁 Prototype-beans 期间跟踪内存情况的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44523862/

相关文章:

objective-c - 从 CVImageBufferRef 获取内存所有权

c - 保存二进制文件是一种标准吗?它仅限于一种类型吗?

java - hibernate “列---不能为空

java - "main"java.lang.NoClassDefFoundError : org/jdom/JDOMException

java - 在 Spring bean 中定义枚举映射

java - 使用 Spring Devtools 和 -parameters 标志

java - 我的辛普森法则计算器正确吗(java)?

java - NoClassDefFoundError斯坦福CoreNLP

java - 当外部事务被标记为回滚时回滚所有嵌套事务

java - JVM 创建 CSV 文件并即时压缩所需的内存