Skip to content

Commit 39f74b2

Browse files
committed
Merge pull request #37 from marschall/small-memory-fixes
* small-memory-fixes: Optimize memory usage in factory *Metadata classes
2 parents 46bdb2d + cdb6d74 commit 39f74b2

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -247,20 +247,30 @@ private class LifecycleMetadata {
247247
public LifecycleMetadata(Class<?> targetClass, Collection<LifecycleElement> initMethods,
248248
Collection<LifecycleElement> destroyMethods) {
249249

250-
this.initMethods = Collections.synchronizedSet(new LinkedHashSet<LifecycleElement>());
251-
for (LifecycleElement element : initMethods) {
252-
if (logger.isDebugEnabled()) {
253-
logger.debug("Found init method on class [" + targetClass.getName() + "]: " + element);
250+
if (!initMethods.isEmpty()) {
251+
this.initMethods = Collections.synchronizedSet(new LinkedHashSet<LifecycleElement>(initMethods.size()));
252+
for (LifecycleElement element : initMethods) {
253+
if (logger.isDebugEnabled()) {
254+
logger.debug("Found init method on class [" + targetClass.getName() + "]: " + element);
255+
}
256+
this.initMethods.add(element);
254257
}
255-
this.initMethods.add(element);
258+
}
259+
else {
260+
this.initMethods = Collections.emptySet();
256261
}
257262

258-
this.destroyMethods = Collections.synchronizedSet(new LinkedHashSet<LifecycleElement>());
259-
for (LifecycleElement element : destroyMethods) {
260-
if (logger.isDebugEnabled()) {
261-
logger.debug("Found destroy method on class [" + targetClass.getName() + "]: " + element);
263+
if (!destroyMethods.isEmpty()) {
264+
this.destroyMethods = Collections.synchronizedSet(new LinkedHashSet<LifecycleElement>(destroyMethods.size()));
265+
for (LifecycleElement element : destroyMethods) {
266+
if (logger.isDebugEnabled()) {
267+
logger.debug("Found destroy method on class [" + targetClass.getName() + "]: " + element);
268+
}
269+
this.destroyMethods.add(element);
262270
}
263-
this.destroyMethods.add(element);
271+
}
272+
else {
273+
this.destroyMethods = Collections.emptySet();
264274
}
265275
}
266276

spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,12 +54,17 @@ public class InjectionMetadata {
5454

5555

5656
public InjectionMetadata(Class targetClass, Collection<InjectedElement> elements) {
57-
this.injectedElements = Collections.synchronizedSet(new LinkedHashSet<InjectedElement>());
58-
for (InjectedElement element : elements) {
59-
if (logger.isDebugEnabled()) {
60-
logger.debug("Found injected element on class [" + targetClass.getName() + "]: " + element);
57+
if (!elements.isEmpty()) {
58+
this.injectedElements = Collections.synchronizedSet(new LinkedHashSet<InjectedElement>(elements.size()));
59+
for (InjectedElement element : elements) {
60+
if (logger.isDebugEnabled()) {
61+
logger.debug("Found injected element on class [" + targetClass.getName() + "]: " + element);
62+
}
63+
this.injectedElements.add(element);
6164
}
62-
this.injectedElements.add(element);
65+
}
66+
else {
67+
this.injectedElements = Collections.emptySet();
6368
}
6469
}
6570

0 commit comments

Comments
 (0)