From 5b29e703dbcc516c3d72dbd5527fdad8ea1dc375 Mon Sep 17 00:00:00 2001 From: LinuxMint4Ever <> Date: Thu, 17 Oct 2019 02:16:30 +0200 Subject: [PATCH] implemented RAM detection function --- lib/launcher/python/mchandler.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/launcher/python/mchandler.py b/lib/launcher/python/mchandler.py index d2c98ca..97008fe 100644 --- a/lib/launcher/python/mchandler.py +++ b/lib/launcher/python/mchandler.py @@ -1,4 +1,4 @@ -import os +import os, psutil # java -Xmx$RAM$ -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -Xmn128M -Djava.library.path=$NATIVES$ -cp $LIBRARIES$ net.minecraft.launchwrapper.Launch --username $USERNAME$ --version 1.7.10-Forge10.13.4.1614-1.7.10 --gameDir $GAMEDIR$ --assetsDir $ASSETS$ --assetIndex 1.7.10 --uuid $UUID$ --accessToken $ACCESSTOKEN$ --userProperties {} --userType mojang --width $WINDOWWIDTH$ --height $WINDOWHEIGHT$ --tweakClass cpw.mods.fml.common.launcher.FMLTweaker # $RAM$ the amount of RAM with postfix (k, M, G ...) @@ -13,7 +13,7 @@ import os game_properties = { "window width": "1366", "window height": "768", - "ram": autoram(), + "ram": "4G", "username": None, "uuid": None, "access token": None, @@ -27,9 +27,21 @@ command = None full_command = None -def autoram(): - return "4G" - # TODO: detect available RAM +def autoram(minram, maxram, balance): + ram_available = psutil.virtual_memory().available + # not enough memory + if ram_available < minram: + # TODO: raise an exception + return -1 + # plenty of memory + elif ram_available > maxram+balance: + return maxram + # enough memory but we do not want to occupy all + elif float(ram_available)*float(maxram)/float(maxram+balance) > minram: + return int(float(ram_available)*float(maxram)/float(maxram+balance)) + # barely enough memory + elif ram_available > minram or ram_available == minram: + return ram_available def launch(): global command