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?
You can save some memory with a BeanFactory - see 3.8.1. BeanFactory or ApplicationContext:
As the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended that it be used in preference to the BeanFactory, except for a few limited situations such as in an Applet, where memory consumption might be critical and a few extra kilobytes might make a difference.
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 the customizeBeanFactory
method. We did it like this:
@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
super.customizeBeanFactory(beanFactory);
String cacheBeanMetadataSysProp = System.getProperty(CACHE_BEAN_METADATA, "true");
if (cacheBeanMetadataSysProp != null
&& cacheBeanMetadataSysProp.equalsIgnoreCase("false")) {
beanFactory.setCacheBeanMetadata(false);
} else if (cacheBeanMetadataSysProp != null
&& cacheBeanMetadataSysProp.equalsIgnoreCase("true")) {
beanFactory.setCacheBeanMetadata(true);
}
}
Setting the "setCacheBeanMetadata" property to false
causes the BeanDefinitions
(basically the programmatic mirror of your XML based configuration) to be discarded after initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With