From 47dde92a5b1710c95b174b36d3b29c784a796ff3 Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Sat, 11 Feb 2012 16:29:35 +0100 Subject: [PATCH] Small memory improvements to Metadata classes InjectionMetadata and LifecycleMetadata can end up having mostly empty instance variables. In such cases memory usage can be improved a little bit This patch address this in two ways. - Creating a LinkedHashSet of the "right" size, the default capacity is 16 but the exact capacity needed is known in advance. - If the argument is empty then use Collections.emptySet() which is a constant so no additional memory is used. Since it's immutable there is no need for the Collections.synchronizedSet wrapper. Issue: SPR-9264 --- ...nitDestroyAnnotationBeanPostProcessor.java | 30 ++++++++++++------- .../factory/annotation/InjectionMetadata.java | 17 +++++++---- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java index f584458f440..8101d766cde 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -247,20 +247,28 @@ private class LifecycleMetadata { public LifecycleMetadata(Class targetClass, Collection initMethods, Collection destroyMethods) { - this.initMethods = Collections.synchronizedSet(new LinkedHashSet()); - for (LifecycleElement element : initMethods) { - if (logger.isDebugEnabled()) { - logger.debug("Found init method on class [" + targetClass.getName() + "]: " + element); + if (!initMethods.isEmpty()) { + this.initMethods = Collections.synchronizedSet(new LinkedHashSet(initMethods.size())); + for (LifecycleElement element : initMethods) { + if (logger.isDebugEnabled()) { + logger.debug("Found init method on class [" + targetClass.getName() + "]: " + element); + } + this.initMethods.add(element); } - this.initMethods.add(element); + } else { + this.initMethods = Collections.emptySet(); } - this.destroyMethods = Collections.synchronizedSet(new LinkedHashSet()); - for (LifecycleElement element : destroyMethods) { - if (logger.isDebugEnabled()) { - logger.debug("Found destroy method on class [" + targetClass.getName() + "]: " + element); + if (!destroyMethods.isEmpty()) { + this.destroyMethods = Collections.synchronizedSet(new LinkedHashSet(destroyMethods.size())); + for (LifecycleElement element : destroyMethods) { + if (logger.isDebugEnabled()) { + logger.debug("Found destroy method on class [" + targetClass.getName() + "]: " + element); + } + this.destroyMethods.add(element); } - this.destroyMethods.add(element); + } else { + this.destroyMethods = Collections.emptySet(); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java index 4a1733de307..287175f2007 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,12 +54,17 @@ public class InjectionMetadata { public InjectionMetadata(Class targetClass, Collection elements) { - this.injectedElements = Collections.synchronizedSet(new LinkedHashSet()); - for (InjectedElement element : elements) { - if (logger.isDebugEnabled()) { - logger.debug("Found injected element on class [" + targetClass.getName() + "]: " + element); + if (!elements.isEmpty()) { + this.injectedElements = Collections.synchronizedSet(new LinkedHashSet(elements.size())); + for (InjectedElement element : elements) { + if (logger.isDebugEnabled()) { + logger.debug("Found injected element on class [" + targetClass.getName() + "]: " + element); + } + this.injectedElements.add(element); } - this.injectedElements.add(element); + + } else { + this.injectedElements = Collections.emptySet(); } }