在 Spring 应用程序中创建和销毁 Prototype-beans 期间跟踪内存情况的方法?
A way to track the situation with memory during creation and destruction of Prototype-beans in Spring application?
在我们项目的一部分中,我们有一些原型 bean。我想清楚地了解那部分执行过程中发生了什么。
有没有什么方法可以跟踪prototype-bean创建过程中的情况,它们开始使用多少内存,以及跟踪这些bean是否被成功销毁?
我不仅需要在控制台打印信息,我还想看实际情况内存或者内存中存在的prototype-beans列表
任何标准分析器都可以帮助您收集您正在寻找的数据。快速浏览一下:http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html
但是我可以帮助解释原型生命周期,这可能会澄清您的一些问题。
在大多数软件解决方案中,您拥有通常作为单例实现的管理对象,并且您拥有通常根据需要实例化的数据对象。
在spring中,创建的所有bean都被标记为单例,并由spring内部管理。
当您创建原型 bean 时,spring 只是 returns 该 bean 的一个新实例,但它确实管理该 bean 的依赖注入。
所以:
@Bean
public String importantString(){
return "Super Important String used throughout the system";
}
当您需要原型中的重要字符串时:
@Scope("prototype")
public class MyPrototypeBean{
@Bean("importantString")
private String myString;
public String getString(){
return importantString;
}
}
Spring 将缓存重要的字符串,但您的应用程序现在将负责处理原型生命周期,就像处理任何 new MyObject()
public static void main(String[] args){
MyProtoTypeBean myPrototypeBean = context.getBean(MyPrototypeBean.class);
MyProtoTypeBean myPrototypeBean2 = context.getBean(MyPrototypeBean.class);
String importantString = myPrototypeBean.getString();
// each prototoype is reconstructed
assert myProtoTypeBean != myPrototypeBean2;
myPrototypeBean = null;
myPrototypeBean2 = null;
// Since we are now handling the lifecycle of the prototype beans, as soon
//as we clear them and set them to null garbage collection will clear the
//prototype beans but not the importantString singleton
//however spring will still have reference to the singleton string
assert importantString == context.getBean("importantString");
}
希望对您有所帮助!
如果您需要以编程方式执行此操作,请尝试使用此 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);
}
}
我找到了一种方法来查看有关创建的原型 bean 的实际情况。
我使用免费的 VisualVM 内存分析器。
在 Sampler 选项卡中,您可以看到创建的 类 的所有实例,包括单例和原型 bean。
您将看到您自己的包的名称和 类。在这种情况下:
prototype 是一个包含我的原型 bean 的包。
singleton 是一个包含我的 singleton-beans 的包。
new类 是由 new 创建的包含 类 的包运算符.
此外,在 垃圾收集器 将清理内存后,您将在此处看到结果。
在我们项目的一部分中,我们有一些原型 bean。我想清楚地了解那部分执行过程中发生了什么。
有没有什么方法可以跟踪prototype-bean创建过程中的情况,它们开始使用多少内存,以及跟踪这些bean是否被成功销毁?
我不仅需要在控制台打印信息,我还想看实际情况内存或者内存中存在的prototype-beans列表
任何标准分析器都可以帮助您收集您正在寻找的数据。快速浏览一下:http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html
但是我可以帮助解释原型生命周期,这可能会澄清您的一些问题。
在大多数软件解决方案中,您拥有通常作为单例实现的管理对象,并且您拥有通常根据需要实例化的数据对象。
在spring中,创建的所有bean都被标记为单例,并由spring内部管理。
当您创建原型 bean 时,spring 只是 returns 该 bean 的一个新实例,但它确实管理该 bean 的依赖注入。
所以:
@Bean
public String importantString(){
return "Super Important String used throughout the system";
}
当您需要原型中的重要字符串时:
@Scope("prototype")
public class MyPrototypeBean{
@Bean("importantString")
private String myString;
public String getString(){
return importantString;
}
}
Spring 将缓存重要的字符串,但您的应用程序现在将负责处理原型生命周期,就像处理任何 new MyObject()
public static void main(String[] args){
MyProtoTypeBean myPrototypeBean = context.getBean(MyPrototypeBean.class);
MyProtoTypeBean myPrototypeBean2 = context.getBean(MyPrototypeBean.class);
String importantString = myPrototypeBean.getString();
// each prototoype is reconstructed
assert myProtoTypeBean != myPrototypeBean2;
myPrototypeBean = null;
myPrototypeBean2 = null;
// Since we are now handling the lifecycle of the prototype beans, as soon
//as we clear them and set them to null garbage collection will clear the
//prototype beans but not the importantString singleton
//however spring will still have reference to the singleton string
assert importantString == context.getBean("importantString");
}
希望对您有所帮助!
如果您需要以编程方式执行此操作,请尝试使用此 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);
}
}
我找到了一种方法来查看有关创建的原型 bean 的实际情况。 我使用免费的 VisualVM 内存分析器。
在 Sampler 选项卡中,您可以看到创建的 类 的所有实例,包括单例和原型 bean。
您将看到您自己的包的名称和 类。在这种情况下:
prototype 是一个包含我的原型 bean 的包。
singleton 是一个包含我的 singleton-beans 的包。
new类 是由 new 创建的包含 类 的包运算符.
此外,在 垃圾收集器 将清理内存后,您将在此处看到结果。