Add a startup low memory warning

This commit is contained in:
James Seibel
2024-06-15 11:04:43 -05:00
parent edaafb5cf0
commit 2f7e3d43d1
3 changed files with 61 additions and 25 deletions
@@ -71,7 +71,7 @@ public class ClientApi
public static boolean prefLoggerEnabled = false;
public static final ClientApi INSTANCE = new ClientApi();
public static TestRenderer testRenderer = new TestRenderer();
public static final TestRenderer TEST_RENDERER = new TestRenderer();
private static final IMinecraftClientWrapper MC = SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class);
private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
@@ -80,6 +80,7 @@ public class ClientApi
public static final long SPAM_LOGGER_FLUSH_NS = TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS);
private boolean configOverrideReminderPrinted = false;
private boolean lowMemoryWarningPrinted = false;
private final Queue<String> chatMessageQueueForNextFrame = new LinkedBlockingQueue<>();
@@ -475,29 +476,7 @@ public class ClientApi
{
// logging //
// dev build
if (ModInfo.IS_DEV_BUILD && !this.configOverrideReminderPrinted && MC.playerExists())
{
this.configOverrideReminderPrinted = true;
// remind the user that this is a development build
MC.sendChatMessage("Distant Horizons nightly/unstable build, version: [" + ModInfo.VERSION+"].");
MC.sendChatMessage("Issues may occur with this version.");
MC.sendChatMessage("Here be dragons!");
MC.sendChatMessage("");
}
// generic messages
while (!this.chatMessageQueueForNextFrame.isEmpty())
{
String message = this.chatMessageQueueForNextFrame.poll();
if (message == null)
{
// done to prevent potential null pointers
message = "";
}
MC.sendChatMessage(message);
}
this.sendChatMessagesNow();
IProfilerWrapper profiler = MC.getProfiler();
profiler.pop(); // get out of "terrain"
@@ -579,7 +558,7 @@ public class ClientApi
else if (Config.Client.Advanced.Debugging.rendererMode.get() == EDhApiRendererMode.DEBUG)
{
profiler.push("Render Debug");
ClientApi.testRenderer.render();
ClientApi.TEST_RENDERER.render();
profiler.pop();
}
}
@@ -659,6 +638,53 @@ public class ClientApi
}
}
private void sendChatMessagesNow()
{
// dev build
if (ModInfo.IS_DEV_BUILD && !this.configOverrideReminderPrinted && MC.playerExists())
{
this.configOverrideReminderPrinted = true;
// remind the user that this is a development build
MC.sendChatMessage("\u00A72" + "Distant Horizons: nightly/unstable build, version: [" + ModInfo.VERSION+"]." + "\u00A7r");
MC.sendChatMessage("Issues may occur with this version.");
MC.sendChatMessage("Here be dragons!");
MC.sendChatMessage("");
}
// memory
if (!this.lowMemoryWarningPrinted && Config.Client.Advanced.Logging.showLowMemoryWarningOnStartup.get())
{
this.lowMemoryWarningPrinted = true;
// 4 GB
long minimumRecommendedMemoryInBytes = 4L * 1_000_000_000L;
// Java returned 17,171,480,576 for 16 GB so it might be slightly off what you'd expect
long maxMemoryInBytes = Runtime.getRuntime().maxMemory();
if (maxMemoryInBytes < minimumRecommendedMemoryInBytes)
{
MC.sendChatMessage("\u00A76" + "Distant Horizons: Low memory detected." + "\u00A7r");
MC.sendChatMessage("Stuttering or low FPS may occur.");
MC.sendChatMessage("Please increase Minecraft's available memory to 4 gigabytes.");
MC.sendChatMessage("This can be disabled in DH's config under Advanced -> Logging.");
MC.sendChatMessage("");
}
}
// generic messages
while (!this.chatMessageQueueForNextFrame.isEmpty())
{
String message = this.chatMessageQueueForNextFrame.poll();
if (message == null)
{
// done to prevent potential null pointers
message = "";
}
MC.sendChatMessage(message);
}
}
/**
* Queues the given message to appear in chat the next valid frame.
* Useful for queueing up messages that may be triggered before the user has loaded into the world.
@@ -1120,6 +1120,14 @@ public class Config
+ "This can be useful for debugging.")
.build();
public static ConfigEntry<Boolean> showLowMemoryWarningOnStartup = new ConfigEntry.Builder<Boolean>()
.set(true)
.comment(""
+ "If enabled, a chat message will be displayed if Java doesn't have enough \n"
+ "memory allocated to run DH well.")
.build();
}
public static class Debugging
@@ -524,6 +524,8 @@
"File Sub Dimension Events",
"distanthorizons.config.client.advanced.logging.logNetworkEvent":
"Network Events",
"distanthorizons.config.client.advanced.logging.showLowMemoryWarningOnStartup":
"Show Low Memory Warning",