我正在创建一个项目,我已经测试了基于 XML 的 spring 配置和基于纯 Java 的配置。我的项目很简单,它将监听 RabbitMQ 队列并执行操作。到目前为止,我还没有在该队列中发布任何内容。两种配置类型的代码和要求相同。但是,在基于 XML 的配置的情况下,我的内存使用量会逐渐增加,一段时间后它将释放所有增加的内存。之后又逐渐增加。
下面的 JProfiler 图像显示了此场景。 有人可以解释为什么会发生这种情况吗?基于 XML 的配置有问题吗? 感谢您的努力。
编辑 1:Spring 版本 -> 5.1.6.RELEASE(基于注释)和 5.2.18.RELEASE(基于 XML)
编辑2:我使用过的代码,
<beans ....>
<bean id="output" class="com.test.Output" />
</beans>
public class XmlConfigTest{
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/applicationContext.xml");
Output output = (Output) ctx.getBean("output");
output.performOperation();
}
}
public class AnnotationConfigTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringContextGenerator.class);
Output output = (Output) ctx.getBean("output");
output.performOperation();
}
}
我使用过类似的代码,从 XML 和注释中获取 bean 并在其中执行操作。
- 当它开始时
- GC 收集所有垃圾对象之前
这是完全正常的 Java 垃圾收集行为(您可以在此处阅读)。
在像 C 这样的编程语言中,分配和释放内存是一个手动过程。在 Java 中,释放内存的过程是由垃圾收集器自动处理的。
此外,现在 70 MB 并不是什么巨大的内存分配。