Skip to content

Small memory fixes #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

marschall
Copy link
Contributor

We analysed the memory usage of our application and found a lot of empty LinkedHashSet/Collections.synchronizedSet in the following three cases:

  • org.springframework.beans.factory.annotation.InjectionMetadata.injectedElements
  • org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata.initMethods
  • org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata.destroyMethods

This fix should address this issue by:

  1. Creating a LinkedHashSet of the "right" size, eg. the default capacity is 16 but we know the exact capacity that we need in advance
  2. If the argument is empty then we use Collections.emptySet() which is a constant so no additional memory is used. Since it's immutable we also save ourselves the Collections.synchronizedSet wrapper.

Should I additionally open an issue?

@cbeams
Copy link
Contributor

cbeams commented Feb 16, 2012

Hi Phillipe, thanks for the submission. We've just added a contributor guidelines doc to the wiki. Please read through that and follow the instructions, particularly with regard to the CLA and commit comment formatting. Note that it's fine for you to collapse these two commits into one given that they are addressing the same concern.

@marschall
Copy link
Contributor Author

  • I signed the CLA. Do you need my confirmation number?
  • I created an issue, SPR-9264
  • I made a new commit with correct comment formatting that also collapse both commits into one (I hope) and updates the year in the license header.

Is there any way to update this pull request or should I close it and make a new one?

@cbeams
Copy link
Contributor

cbeams commented Mar 24, 2012

Just do a forced push (-f) against your existing small-memory-fixes branch

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
@marschall
Copy link
Contributor Author

Ok, there we go, it seems to have worked.

There is a small issue with the patch though. InjectionMetadata.checkConfigMembers and LifecycleMetadata.checkConfigMembers both synchronize of the underlying collection. In the case of new LinkedHashSet this is fine but in the case of Collections.emptySet() we synchronize on a global variable that is accessed from other places. This could be avoided if we first check for #isEmpty() before synchronizing. Let me know if you want me to update the patch accordingly.

@marschall
Copy link
Contributor Author

I did sign the CLA.

cbeams added a commit that referenced this pull request May 16, 2012
* small-memory-fixes:
  Optimize memory usage in factory *Metadata classes
@cbeams
Copy link
Contributor

cbeams commented May 16, 2012

Merged. Note that the commit comment was updated slightly, a line of trailing whitespace removed, and else blocks formatting changed from:

} else {

to

}
else {

for consistency.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants