Skip to content

Commit

Permalink
Fix memory leak in Space#flyweights implementations (openrewrite#3368)
Browse files Browse the repository at this point in the history
The `Space#flyweights` implementations all had a memory leak due to the way the `WeakHashMap` was being used: The values all had a hard reference to the key. As a result these entries can never get collected. Additionally, there was no length limitation on the whitespace, so very long spaces could end up in the flyweights table with a very low probability of ever getting reused.
  • Loading branch information
knutwannheden authored Jun 27, 2023
1 parent 65d4c73 commit 068af07
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public static Space build(@Nullable String whitespace, List<Comment> comments) {
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
} else if (whitespace.length() <= 100) {
//noinspection StringOperationCanBeSimplified
return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments));
}
return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments));
}
return new Space(whitespace, comments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public static Space build(@Nullable String whitespace, List<Comment> comments) {
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
} else if (whitespace.length() <= 100) {
//noinspection StringOperationCanBeSimplified
return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments));
}
return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments));
}
return new Space(whitespace, comments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public static Space build(@Nullable String whitespace, List<Comment> comments) {
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
} else if (whitespace.length() <= 100) {
//noinspection StringOperationCanBeSimplified
return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments));
}
return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments));
}
return new Space(whitespace, comments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public static Space build(@Nullable String whitespace, List<Comment> comments) {
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
} else if (whitespace.length() <= 100) {
//noinspection StringOperationCanBeSimplified
return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments));
}
return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments));
}
return new Space(whitespace, comments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ private Space(@Nullable String whitespace) {

@JsonCreator
public static Space build(@Nullable String whitespace) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
if (comments.isEmpty()) {
if (whitespace == null || whitespace.isEmpty()) {
return Space.EMPTY;
} else if (whitespace.length() <= 100) {
//noinspection StringOperationCanBeSimplified
return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments));
}
}
return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace));
}
Expand Down

0 comments on commit 068af07

Please sign in to comment.