try to fix memory over flow

This commit is contained in:
huangyuhui
2017-02-23 18:19:14 +08:00
parent a6768c361f
commit 44735b3d4e
4 changed files with 8 additions and 49 deletions

View File

@@ -50,10 +50,10 @@ import org.jackhuang.hmcl.util.lang.SupportedLocales;
import org.jackhuang.hmcl.util.log.Configuration; import org.jackhuang.hmcl.util.log.Configuration;
import org.jackhuang.hmcl.util.log.appender.ConsoleAppender; import org.jackhuang.hmcl.util.log.appender.ConsoleAppender;
import org.jackhuang.hmcl.util.log.layout.DefaultLayout; import org.jackhuang.hmcl.util.log.layout.DefaultLayout;
import org.jackhuang.hmcl.util.sys.ProcessManager;
import org.jackhuang.hmcl.util.ui.MyRepaintManager; import org.jackhuang.hmcl.util.ui.MyRepaintManager;
import org.jackhuang.hmcl.util.upgrade.IUpgrader; import org.jackhuang.hmcl.util.upgrade.IUpgrader;
import org.jackhuang.hmcl.laf.BeautyEyeLNFHelper; import org.jackhuang.hmcl.laf.BeautyEyeLNFHelper;
import org.jackhuang.hmcl.util.sys.JavaProcess;
/** /**
* *
@@ -194,7 +194,7 @@ public final class Main implements Runnable {
} }
LogWindow.INSTANCE.clean(); LogWindow.INSTANCE.clean();
LogWindow.INSTANCE.setTerminateGame(ProcessManager::stopAllProcesses); LogWindow.INSTANCE.setTerminateGame(new Main()::run);
Settings.UPDATE_CHECKER.upgrade.register(IUpgrader.NOW_UPGRADER); Settings.UPDATE_CHECKER.upgrade.register(IUpgrader.NOW_UPGRADER);
Settings.UPDATE_CHECKER.process(false).reg(t -> Main.invokeUpdate()).execute(); Settings.UPDATE_CHECKER.process(false).reg(t -> Main.invokeUpdate()).execute();
@@ -218,7 +218,8 @@ public final class Main implements Runnable {
@Override @Override
public void run() { public void run() {
ProcessManager.stopAllProcesses(); for (Process p : JavaProcess.processes)
p.destroy();
} }
public static void invokeUpdate() { public static void invokeUpdate() {

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.util.sys;
import org.jackhuang.hmcl.api.IProcess; import org.jackhuang.hmcl.api.IProcess;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
/** /**
@@ -27,6 +28,7 @@ import java.util.List;
* @author huangyuhui * @author huangyuhui
*/ */
public class JavaProcess implements IProcess { public class JavaProcess implements IProcess {
public static HashSet<Process> processes = new HashSet<>();
private final List<String> commands; private final List<String> commands;
private final Process process; private final Process process;
@@ -35,7 +37,7 @@ public class JavaProcess implements IProcess {
public JavaProcess(List<String> commands, Process process) { public JavaProcess(List<String> commands, Process process) {
this.commands = commands; this.commands = commands;
this.process = process; this.process = process;
ProcessManager.registerProcess(this); processes.add(process);
} }
public JavaProcess(String[] commands, Process process) { public JavaProcess(String[] commands, Process process) {

View File

@@ -1,44 +0,0 @@
/*
* Hello Minecraft!.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util.sys;
import java.util.HashSet;
import org.jackhuang.hmcl.api.IProcess;
/**
*
* @author huangyuhui
*/
public class ProcessManager {
private static final HashSet<IProcess> GAME_PROCESSES = new HashSet<>();
public static void registerProcess(IProcess jp) {
GAME_PROCESSES.add(jp);
}
public static void stopAllProcesses() {
for (IProcess jp : GAME_PROCESSES)
jp.stop();
GAME_PROCESSES.clear();
}
public static void onProcessStopped(IProcess p) {
GAME_PROCESSES.remove(p);
}
}

View File

@@ -66,7 +66,7 @@ public class ProcessThread extends Thread {
System.out.println("MC: " + line); System.out.println("MC: " + line);
p.getStdOutLines().add(line); p.getStdOutLines().add(line);
} }
ProcessManager.onProcessStopped(p); JavaProcess.processes.remove(p.getRawProcess());
stopEvent.fire(new SimpleEvent<>(this, p)); stopEvent.fire(new SimpleEvent<>(this, p));
} catch (IOException e) { } catch (IOException e) {
HMCLog.err("An error occured when reading process stdout/stderr.", e); HMCLog.err("An error occured when reading process stdout/stderr.", e);