Skip to content

Commit 708e032

Browse files
Glavoroj234
andauthored
[release/3.6] 修复多次启动游戏时内存泄露的问题 (#4423)
#4121 Co-authored-by: Roj234 <82699138+roj234@users.noreply.github.com>
1 parent f7a5c7a commit 708e032

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.concurrent.*;
6060
import java.util.concurrent.atomic.AtomicReference;
6161
import java.util.stream.Collectors;
62+
import java.lang.ref.WeakReference;
6263

6364
import static javafx.application.Platform.runLater;
6465
import static javafx.application.Platform.setImplicitExit;
@@ -125,6 +126,9 @@ public void makeLaunchScript(File scriptFile) {
125126
}
126127

127128
private void launch0() {
129+
// https://github.com/HMCL-dev/HMCL/pull/4121
130+
PROCESSES.removeIf(it -> it.get() == null);
131+
128132
HMCLGameRepository repository = profile.getRepository();
129133
DefaultDependencyManager dependencyManager = profile.getDependency();
130134
AtomicReference<Version> version = new AtomicReference<>(MaintainTask.maintain(repository, repository.getResolvedVersion(selectedVersion)));
@@ -209,7 +213,7 @@ private void launch0() {
209213
}
210214
}).thenAcceptAsync(process -> { // process is LaunchTask's result
211215
if (scriptFile == null) {
212-
PROCESSES.add(process);
216+
PROCESSES.add(new WeakReference<>(process));
213217
if (launcherVisibility == LauncherVisibility.CLOSE)
214218
Launcher.stopApplication();
215219
else
@@ -896,10 +900,10 @@ public void onExit(int exitCode, ExitType exitType) {
896900

897901
}
898902

899-
public static final Queue<ManagedProcess> PROCESSES = new ConcurrentLinkedQueue<>();
903+
public static final Queue<WeakReference<ManagedProcess>> PROCESSES = new ConcurrentLinkedQueue<>();
900904

901905
public static void stopManagedProcesses() {
902906
while (!PROCESSES.isEmpty())
903-
Optional.ofNullable(PROCESSES.poll()).ifPresent(ManagedProcess::stop);
907+
Optional.ofNullable(PROCESSES.poll()).map(WeakReference::get).ifPresent(ManagedProcess::stop);
904908
}
905909
}

0 commit comments

Comments
 (0)