gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:29:46 24 4
gpt4 key购买 nike

在我们项目的一部分中,我们有一些原型(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com