Skip to content

Commit

Permalink
add memory not enough warning
Browse files Browse the repository at this point in the history
  • Loading branch information
qouteall committed Feb 6, 2021
1 parent 6ba1bca commit cee6f7e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.qouteall.immersive_portals.chunk_loading.EntitySync;
import com.qouteall.immersive_portals.chunk_loading.NewChunkTrackingGraph;
import com.qouteall.immersive_portals.chunk_loading.WorldInfoSender;
import com.qouteall.immersive_portals.miscellaneous.GcMonitor;
import com.qouteall.immersive_portals.my_util.MyTaskList;
import com.qouteall.immersive_portals.my_util.Signal;
import com.qouteall.immersive_portals.portal.PortalExtension;
Expand Down Expand Up @@ -56,6 +57,8 @@ public static void init() {

PortalExtension.init();

GcMonitor.initCommon();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.qouteall.hiding_in_the_bushes.MyNetworkClient;
import com.qouteall.hiding_in_the_bushes.O_O;
import com.qouteall.immersive_portals.miscellaneous.GcMonitor;
import com.qouteall.immersive_portals.my_util.MyTaskList;
import com.qouteall.immersive_portals.optifine_compatibility.OFBuiltChunkStorageFix;
import com.qouteall.immersive_portals.optifine_compatibility.OFGlobal;
Expand Down Expand Up @@ -122,6 +123,8 @@ public static void init() {
showOptiFineWarning();
}

GcMonitor.initClient();

Helper.log(OFInterface.isOptifinePresent ? "Optifine is present" : "Optifine is not present");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.qouteall.immersive_portals.McHelper;
import com.qouteall.immersive_portals.ModMain;
import com.qouteall.immersive_portals.ducks.IEEntity;
import com.qouteall.immersive_portals.miscellaneous.GcMonitor;
import com.qouteall.immersive_portals.my_util.SignalBiArged;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
Expand Down Expand Up @@ -268,7 +269,14 @@ private static boolean shouldUnload(long currTime, PlayerWatchRecord record) {
if (record.player.removed) {
return true;
}
return currTime - record.lastWatchTime > (long) Global.chunkUnloadDelayTicks;
long unloadDelay = Global.chunkUnloadDelayTicks;

if (GcMonitor.isMemoryNotEnough()) {
// does not delay unloading
unloadDelay = 21;
}

return currTime - record.lastWatchTime > unloadDelay;
}

private static void tick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.qouteall.immersive_portals.render;
package com.qouteall.immersive_portals.miscellaneous;

import java.util.ArrayDeque;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.qouteall.immersive_portals.miscellaneous;

import com.qouteall.hiding_in_the_bushes.O_O;
import com.qouteall.immersive_portals.Helper;
import com.qouteall.immersive_portals.McHelper;
import com.qouteall.immersive_portals.ModMain;
import com.qouteall.immersive_portals.my_util.LimitedLogger;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.MessageType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Util;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.WeakHashMap;

public class GcMonitor {
private static boolean memoryNotEnough = false;

private static final WeakHashMap<GarbageCollectorMXBean, Long> lastCollectCount =
new WeakHashMap<>();

private static final LimitedLogger limitedLogger = new LimitedLogger(3);

@Environment(EnvType.CLIENT)
public static void initClient() {
ModMain.preGameRenderSignal.connect(GcMonitor::update);
}

public static void initCommon() {
ModMain.postServerTickSignal.connect(() -> {
MinecraftServer server = McHelper.getServer();
if (server != null) {
if (server.isDedicated()) {
update();
}
}
});
}

private static void update() {

for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
long currCount = garbageCollectorMXBean.getCollectionCount();

Long lastCount = lastCollectCount.get(garbageCollectorMXBean);
lastCollectCount.put(garbageCollectorMXBean, currCount);

if (lastCount != null) {
if (lastCount != currCount) {
onGced();
}
}
}
}

private static void onGced() {
long maxMemory = Runtime.getRuntime().maxMemory();
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
long usedMemory = totalMemory - freeMemory;

double usage = ((double) usedMemory) / maxMemory;

if (usage > 0.8) {
memoryNotEnough = true;


Helper.err(
"Memory not enough. Try to Shrink loading distance or allocate more memory." +
" If this happens with low loading distance, it usually indicates memory leak"
);

if (!O_O.isDedicatedServer()) {
informMemoryNotEnoughClient();
}
}
}

@Environment(EnvType.CLIENT)
private static void informMemoryNotEnoughClient() {
limitedLogger.invoke(() -> {
MinecraftClient.getInstance().inGameHud.addChatMessage(
MessageType.SYSTEM,
new TranslatableText("imm_ptl.memory_not_enough"),
Util.NIL_UUID
);
});
}

public static boolean isMemoryNotEnough() {
return memoryNotEnough;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.qouteall.immersive_portals.CGlobal;
import com.qouteall.immersive_portals.ModMain;
import com.qouteall.immersive_portals.ducks.IEMinecraftClient;
import com.qouteall.immersive_portals.miscellaneous.FPSMonitor;
import com.qouteall.immersive_portals.network.CommonNetwork;
import com.qouteall.immersive_portals.network.CommonNetworkClient;
import com.qouteall.immersive_portals.render.FPSMonitor;
import com.qouteall.immersive_portals.render.context_management.WorldRenderInfo;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.qouteall.immersive_portals.OFInterface;
import com.qouteall.immersive_portals.ducks.IEGameRenderer;
import com.qouteall.immersive_portals.ducks.IEWorldRenderer;
import com.qouteall.immersive_portals.miscellaneous.FPSMonitor;
import com.qouteall.immersive_portals.portal.PortalLike;
import com.qouteall.immersive_portals.render.FPSMonitor;
import com.qouteall.immersive_portals.render.MyRenderHelper;
import com.qouteall.immersive_portals.render.QueryManager;
import net.minecraft.client.MinecraftClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"imm_ptl.loop": "Loop",
"imm_ptl.enabled": "Enabled",
"imm_ptl.disabled": "Disabled",
"imm_ptl.loading_datapack_dimensions": "Loading Datapack Dimensions"
"imm_ptl.loading_datapack_dimensions": "Loading Datapack Dimensions",
"imm_ptl.memory_not_enough": "Memory not enough. You may experience GC lag. Try to Shrink render distance or allocate more memory."
}

0 comments on commit cee6f7e

Please sign in to comment.