如何配置 Spring 以尽可能节省内存?
我们正在有内存限制的半嵌入式设备上部署应用程序。 为了尽可能节省一切,我们正在分析应用程序的堆转储并攻击最大的消费者。
我们将 Spring 2.5 与 Spring DM 1.1 一起使用,我们注意到一些具有更复杂 Spring 上下文的包占用了大量内存,因为 Spring 似乎保留了包含从 XML 解析的所有 BeanDefinition 的整个对象图。 。 我认为一旦应用程序初始化并且所有内容都被注入,其中大部分都是不必要的。
Spring 是否有配置选项允许控制这种行为? 在某种低内存模式下运行? 扔掉所有不需要的东西? 用计算时间换取大小?
We're deploying an application on a semi-embedded device that has memory constraints. Looking to save whatever we can we are analysing heap dumps of the app and attacking the biggest consumers.
We use Spring 2.5 together with Spring DM 1.1 and we notice that some of our bundles with more complex Spring contexts are using up quite a bit of memory since Spring seems to keep around the entire object graph containing all the BeanDefinitions that were parsed from the XML. I would assume that most of this is unnecessary once the app has been initialized and everything is injected.
Are there configuration options for Spring that allow one to control this behaviour? Run in some low memory mode? Throw away all unnecessary things? Trade computation time for size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我让团队成员对此进行了更深入的研究,并得到了一些有趣的结果。 Spring 在其默认配置中对于在内存使用方面特别保守并不感兴趣。 有两个基本方面可以进行调整以获得显着的收益:
OsgiBundleXmlApplicationContext
内的非公开属性,如果您从该类扩展并覆盖customizeBeanFactory<,则可以覆盖该属性。 /代码> 方法。
我们这样做是这样的:
将“setCacheBeanMetadata”属性设置为
false
会导致BeanDefinitions
(基本上是基于XML的配置的编程镜像)在初始化后被丢弃。I had team members have a deeper look at this and had some interesting results. Spring in its default configuration is very much not interested in being especially conservative in its memory usage. There are 2 basic aspects that can be tweaked for significant gains:
OsgiBundleXmlApplicationContext
that you can override if you extend from that class and override thecustomizeBeanFactory
method.We did it like this:
Setting the "setCacheBeanMetadata" property to
false
causes theBeanDefinitions
(basically the programmatic mirror of your XML based configuration) to be discarded after initialization.您可以使用 BeanFactory 节省一些内存 - 请参阅3.8.1。 BeanFactory 或 ApplicationContext:
You can save some memory with a BeanFactory - see 3.8.1. BeanFactory or ApplicationContext:
我不知道有什么方法可以让 Spring 在“轻量”模式下运行。 您可以尝试实现 BeanFactoryPostProcessor 并使用它从上下文中删除某些 bean。 不过,我不知道这是否会导致 Spring 内部错误。
I'm not aware of any way to make Spring run in "light" mode. You could try implementing a BeanFactoryPostProcessor and use it to remove certain beans from the context. I have no idea whether that's going to lead to internal Spring errors however.
如果你只在启动时使用Spring,即所有bean都已连接,然后你不需要应用程序上下文或关闭逻辑,你可以启动你的应用程序,然后清除对应用程序上下文的所有引用。
If you only use Spring at startup, i.e. all beans are wired and then you don't need the application context or the shutdown logic, you can start your app and then clear all references to the application context.
如果您的 Spring 配置使用 AOP 和加载时编织,您可以使用 aop.xml 通过使用 1.6.5 中引入的 AspectJ 类型降级功能从 AspectJ 重新获得一些内存。
分析你的堆,如果你发现很多 RefType 对象,上面的技巧会有所帮助。
If your Spring configuration uses AOP and load time weaving, you could use aop.xml to regain some memory from AspectJ by using AspectJ type demotion feature that was introduced in 1.6.5.
Analyse your heap, if you find many RefType objects, the trick above will help.