-
Notifications
You must be signed in to change notification settings - Fork 5
Fix memory leak in dependency nodes #26
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,14 @@ public class #E#InlineDependencyCache extends #E#InlineCacheImpl implements Depe | |
| */ | ||
| private Set<Reference<DependencyNode>> dependentNodes; | ||
|
|
||
| /** | ||
| * Number of elements in dependentNodes after which all the set should be checked for the presence of | ||
| * references to GC'ed objects. | ||
| * | ||
| * This threshold is required in order to evict such references as they pollute memory and never GC'ed otherwise. | ||
| */ | ||
| private int cleanupThreshold = 10; | ||
|
|
||
| private Reference<DependencyNode> selfReference; | ||
|
|
||
| public #E#InlineDependencyCache(Object owner, #E#Calculatable calculable, MutableStatistics statistics) { | ||
|
|
@@ -68,6 +76,26 @@ public class #E#InlineDependencyCache extends #E#InlineCacheImpl implements Depe | |
| dependentNodes = new THashSet<Reference<DependencyNode>>(); | ||
| } | ||
| dependentNodes.add(node.getSelfReference()); | ||
| cleanupIfNeeded(); | ||
| } | ||
|
|
||
| private void cleanupIfNeeded() { | ||
| if (dependentNodes.size() >= cleanupThreshold) { | ||
| for (Iterator<Reference<DependencyNode>> it = dependentNodes.iterator(); it.hasNext(); ) { | ||
| if (it.next().get() == null) { | ||
| it.remove(); | ||
| } | ||
| } | ||
| // It's important to increase cleanup threshold according to the number of elements in a set | ||
| // in order to maintain the balance between CPU-overhead and memory-overhead | ||
|
|
||
| // The cleanup has O(N) complexity, so doing this on addition of N new elements would lead to constant | ||
| // small overhead and thus would not affect the asymptotic behaviour of operations. | ||
|
|
||
| // The memory overhead could be significant but it's guaranteed that memory usage would not be more than | ||
| // 2 * peak memory usage for alive elements. | ||
| cleanupThreshold = dependentNodes.size() * 2; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange, very strange. After cleaning dependentNodes can be empty. And must be immediately cleaned after it. Very strange approach. Cleanup occurs while reading cache. So if somebody add 1M dependencies (they all alive while adding) and read cache after, than next cleanup occurs almost never.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why do you think so? Dependencies are never reset ;-)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check by time?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That might help, however:
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why it is not in AbstractCache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two different concepts:
Cache- takes care of storing the data and accessing it;DependencyNode- takes care about dependencies.There are few implementations
XXXInlineDependencyCachethat implement bothCacheandDependencyNode. This reduces the memory overhead of caching in some simple cases.But
cleanupIfNeededstuff is not part ofCachecontract, soAbstractCachehas nothing to do with that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be create helper class? Too much code duplicating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is a template, others are autogenerated from this template.
It doesn't seem reasonable to create some kind of helper for a single method or add one more class to the hierarchy.
Code generation is bad, I know. But there's no other way to have high performance with primitives in Java.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok