@Mod(modid = "MemoryCleaner", version = "1.0", name = "Memory Cleaner")
public class MemoryCleaner
{
public static final String MODID = "MemoryCleaner";
public static final String NAME = "Memory Cleaner";
public static final String VERSION = "1.0";
public static long delay;
public static boolean consoleDebug;
public static boolean modOn;
public static Logger log;
public static Configuration configs;
public Thread main;
public MemoryCleaner() {
this.main = new Thread(new Runnable() {
public Runtime runtime = Runtime.getRuntime();
@Override
public void run() {
if (!MemoryCleaner.modOn) {
return;
}
Label_0006_Outer:
while (true) {
while (true) {
try {
while (true) {
if (MemoryCleaner.consoleDebug) {
MemoryCleaner.log.info("_________________________________");
MemoryCleaner.log.info("Used: " + this.runtime.freeMemory() / 1048576L + "mb of " + this.runtime.maxMemory() / 1048576L + "mb| Used: " + (this.runtime.maxMemory() - this.runtime.freeMemory()) / 1048576L + "mb");
this.runtime.gc();
MemoryCleaner.log.info("Cleaned...");
MemoryCleaner.log.info("Used: " + this.runtime.freeMemory() / 1048576L + "mb of " + this.runtime.maxMemory() / 1048576L + "mb| Used: " + (this.runtime.maxMemory() - this.runtime.freeMemory()) / 1048576L + "mb");
}
else {
this.runtime.gc();
}
Thread.sleep(MemoryCleaner.delay * 1000L);
}
}
catch (InterruptedException e) {
e.printStackTrace();
continue Label_0006_Outer;
}
continue;
}
}
}
});
}
@Mod.EventHandler
public void preInit(final FMLPreInitializationEvent event) {
MemoryCleaner.log = event.getModLog();
(MemoryCleaner.configs = new Configuration(event.getSuggestedConfigurationFile())).load();
Property prop = MemoryCleaner.configs.get("general", "delay", 180);
prop.comment = "Delay to clear the memory in seconds (set to 0 to turn off the mod)";
MemoryCleaner.delay = prop.getInt((int)MemoryCleaner.delay);
prop = MemoryCleaner.configs.get("general", "debug", false);
prop.comment = "Output in console the memory when clear";
MemoryCleaner.consoleDebug = prop.getBoolean(MemoryCleaner.consoleDebug);
MemoryCleaner.configs.save();
if (MemoryCleaner.delay == 0L) {
MemoryCleaner.modOn = false;
}
final MemoryCleaner instance = new MemoryCleaner();
FMLCommonHandler.instance().bus().register((Object)instance);
MinecraftForge.EVENT_BUS.register((Object)instance);
}
@Mod.EventHandler
public void onServerStartedEvent(final FMLServerStartedEvent event) {
this.main.start();
}
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onClientConnectedToServer(final FMLNetworkEvent.ClientConnectedToServerEvent event) {
if (!event.isLocal) {
this.main.start();
}
}
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onClientDisconnectionFromServer(final FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
this.main.interrupt();
}
@Mod.EventHandler
public void onServerStoppedEvent(final FMLServerStoppedEvent event) {
this.main.interrupt();
}
static {
MemoryCleaner.delay = 180L;
MemoryCleaner.consoleDebug = false;
MemoryCleaner.modOn = true;
}
}