Skip to content

Commit 9b5510a

Browse files
committed
Fix caching memory leak in lazy FormatterSteps.
1 parent 986cda3 commit 9b5510a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
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.
@@ -39,7 +39,7 @@ abstract class FormatterStepImpl<State extends Serializable> extends Strict<Stat
3939
final transient String name;
4040

4141
/** Transient because only the state matters. */
42-
final transient ThrowingEx.Supplier<State> stateSupplier;
42+
transient ThrowingEx.Supplier<State> stateSupplier;
4343

4444
FormatterStepImpl(String name, ThrowingEx.Supplier<State> stateSupplier) {
4545
this.name = Objects.requireNonNull(name);
@@ -53,7 +53,11 @@ public String getName() {
5353

5454
@Override
5555
protected State calculateState() throws Exception {
56-
return stateSupplier.get();
56+
// LazyForwardingEquality guarantees that this will only be called once, and keeping toFormat
57+
// causes a memory leak, see https://github.com/diffplug/spotless/issues/1194
58+
State state = stateSupplier.get();
59+
stateSupplier = null;
60+
return state;
5761
}
5862

5963
static final class Standard<State extends Serializable> extends FormatterStepImpl<State> {

0 commit comments

Comments
 (0)