From 979a1a98104c3f6c1718fdb3768c4c0abaed841d Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Thu, 29 Jun 2017 08:30:01 +0800 Subject: [PATCH] Prevent too much memory used by logging system --- .../main/java/org/jackhuang/hmcl/ui/LogWindow.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java index b10d18de4..e1969efec 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java @@ -17,7 +17,6 @@ */ package org.jackhuang.hmcl.ui; -import java.awt.Font; import java.io.PrintStream; import javax.swing.SwingUtilities; import javax.swing.text.Document; @@ -245,9 +244,17 @@ public class LogWindow extends javax.swing.JFrame { } public void log(final String status, final Level c) { + if (isVisible()) + return; SwingUtilities.invokeLater(() -> { - String newStatus = status.replace("\t", " "); Document d = txtLog.getStyledDocument(); + try { // prevent too much memory used. + if (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() > 1l * 1024 * 1024 * 256) + d.remove(0, d.getLength()); + } catch (Exception ex) { + HMCLog.err("Failed to clear the text component", ex); + } + String newStatus = status.replace("\t", " "); SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, c.COLOR); try {