Skip to content
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

issue 515 - fix StringCache memory leak #516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions src/main/java/com/univocity/parsers/common/StringCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ public void setSizeLimit(int sizeLimit) {
this.sizeLimit = sizeLimit;
}

/**
* Associates a value to a string
*
* @param input the string to be associated with a given value
* @param value the value associated with the given string
*/
public void put(String input, T value) {
if (input == null || input.length() > maxStringLength) {
return;
}
if (stringCache.size() >= sizeLimit) {
stringCache.clear();
}
stringCache.put(input, new SoftReference<T>(value));

}

/**
* Returns the value associated with the given string. If it doesn't exist,
* or if it has been evicted, a value will be populated using {@link #process(String)}
Expand All @@ -108,6 +91,9 @@ public T get(String input) {
T out;
if (ref == null || ref.get() == null) {
out = process(input);
if (stringCache.size() >= sizeLimit) {
stringCache.clear();
}
ref = new SoftReference<T>(out);
stringCache.put(input, ref);
} else {
Expand Down