Java Arguments(JVM), explaining them, and commonly used settings.
Basic beginners in Java: what does 'arguments' mean in Java - Stack Overflow
Difference between arguments and parameters in Java - Stack Overflow
I am learning how to properly use Java parameters and arguments - Code Review Stack Exchange
Hello, I am a longtime player of modded minecraft and in my quest to fit as much stuff as I can into a modpack I ran into JVM arguments. After several days, and a headache later, this is what I have came up with so far. Do keep in mind, I have almost no java programming experience and this is compiled based off personal testing and various posts from people such as CPW Player and other programmers who know what they are doing far more than I do. They deserve the credit for this, I am merely trying to raise awareness for it and try to help some people who have issues running the game like my friends.Sources:
-
https://www.reddit.com/r/feedthebeast/comments/5jhuk9/modded_mc_and_memory_usage_a_history_with_a/
-
http://www.fasterj.com/articles/oraclecollectors1.shtml
-
https://www.reddit.com/r/feedthebeast/comments/beyi1t/the_ultimate_guide_to_improving_performance_in/
-
and several others however the first and second ones are the most relevant.
WARNING: THESE SETTINGS WILL NEED TO BE CHANGED BASED ON YOUR COMPUTER, EXPERIMENT AND FIND WHAT WORKS BEST FOR YOU, THIS IS JUST AN OUTLINE USING COMMONLY USED SETTINGS.
First off, the actual Garbage collector.What this does is free up unused memory periodically while java is running, aka cleans up the garbage.For minecraft, the two biggest ones are;-XX:+UseG1GC and -XX:+UseConcMarkSweepGCAs a note, -XX:+UseConcMarkSweepGC has many settings to it as outlined in the fasterj post.I found that -XX:+UseConcMarkSweepGC -XX:+UseParNewGC tends to work out best for me but this may differ.I am not sure if it is required, but I use -XX:+CMSConcurrentMTEnabled This way it makes sure multi-threading is enabled for the Garbage Collector, may not be required.
In tune with multi-threading, -XX:ParallelGCThreads=12 This command sets the number of parallel threads that the GC will use, this number is directly based on your processor. I have a 6 core intel processor and tend to not be doing anything when playing so I use all 12 of the available threads. Again this may vary so do some research on what processor you have and tweek accordingly.
Next up is this, -Dsun.rmi.dgc.server.gcInterval=2147483646To he honest, I actually do not know what exactly it means however the post by CPW explains it as follows "This tells the RMI layer not to do a full GC every minute."
-XX:+UnlockExperimentalVMOptions Does exactly what it says, it unlocks Experimental Java Virtual Machine Options.
-XX:+DisableExplicitGC As far as I can understand, some mods implement their own Garbage Collection in an effort to reduce memory usage which can sometimes interfere with the normal Garbage Collector, I believe that there is an alternative option but I can not remember it and will update this if I find it.
For the sake of finding a lot of options which I am not sure will work or not, and are most likely redundant or simply outdated, I will be skipping them. I have found that the simpler the arguments are, the more effective they tend to be, so here are the remaining args I know work.
And now for two important arguments for any Garbage Collector changes:
-XX:G1NewSizePercent=20 and -XX:G1ReservePercent=20 They "Tells G1GC to put aside 20% of the heap as "new" space. This is space where new objects will be allocated, in general."As a note, despite CPW saying G1GC they work with ALL Garbage Collectors as far as I can tell.This is especially useful in large modpacks where large amounts of blocks and items are present, again, tweek as necessary.
Much like the previous, -XX:MaxGCPauseMillis=50 is core to most of these arguments, and once again, works with ALL Garbage Collectors as far as I can tell. To again quote CPW "This tells G1GC to try and not stop for more than 50 milliseconds when garbage collecting, if possible. This is a target, and G1GC will ignore you if you put a silly number in like 1 which is unattainable. 50 millis is the time for one server tick "
Finally, there is -XX:G1HeapRegionSize=32M Which, I have no idea how to explain it myself, so here is CPWs Explanation " This tells G1GC to allocate it's garbage collection blocks in units of 32megs. The reason for this is that chunk data is typically just over 8megs in size, and if you leave it default (16 megs), it'll treat all the chunk data as "humungous" and so it'll have to be garbage collected specially as a result. Some mods cause humongous allocations as well, such as journeymap, and this setting helps them too."I have not played around with this one as the default settings make sense to me. Like the ones above it, it should work with ALL Garbage Collectors and not just G1GC.
And there you have it. My args come to look like this by the time all is said and done;-XX:+UseG1GC -XX:ParallelGCThreads=12 -XX:+DisableExplicitGC -Dsun.rmi.dgc.server.gcInterval=2147483646 -XX:+UnlockExperimentalVMOptions -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=40 -XX:G1HeapRegionSize=32M
I am currently playing around with-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSConcurrentMTEnabled -XX:ParallelGCThreads=12 -Dsun.rmi.dgc.server.gcInterval=2147483646 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M
You may have noticed that I did not include -Xmx or -Xms. These were not included because I use the MultiMc launcher and they do not work on other launchers either. For those who are not aware of what these two do, on the default minecraft launcher, they control the heap size. Or, better put, this is where you put the amount of ram to dedicate to minecraft. To quote CPW one last time "Minecraft eats a lot of memory. If you have -Xms set to something smaller, the garbage collector may be convinced to try "harder" to garbage collect to that lower target. This can result in "big lag spikes" because those aggressive collections will be slow and painful."
In the MultiMC launcher, Xms is Minimum memory allocated while Xmx is Maximum memory allocated.
In the twitch launcher, Xms can not be modified and Xmx is the memory slider under minecrafts settings
I am not familiar with other launchers as I have not used them in a long time but I will update when I find out about them.
So that wraps up all I was able to find on JVM arguments. One thing I discovered while creating this compilation is that the simpler the arguments, the more effective they seam to be. So I hope that his has helped all of you out there that are trying to optimize your minecraft just a little bit more.
Edit 1: As outlined in the comments, I recommend reserving at least one thread for your OS, one thread for background apps, and one thread for minecraft itself. So take your total threads and subtract at least 3 threads. Thanks to valarnin for pointing this mistake of mine out.
Edit 2:Testing reducing the thread count actually REDUCED my performance and caused slowdown across my PC. Though this is the case for me, you may get different results so play with the settings a bit.
Edit 3: Testing using -XX:+UseStringDeduplication Will report back with findings. Rough idea of this command is to remove redundant pieces of code that were used in a unoptimal way. I belive that I was using this during my original arguments before I went testing.
I can't understand the line "if (arguments.length > 0) what does "arguments" mean? where did it value came from?
It came from the method declaration:
public static void main(String[] arguments) {
That declares a parameter called arguments. For a normal method call, the caller specifies the argument, and that becomes the initial value of the parameter. For example:
int foo(int x) {
System.out.println(x);
}
...
foo(10);
Here, 10 is the argument to the method, so it's the initial value for the x parameter.
Now a public static void method called main in Java is an entry point - so when you run:
java MyClass x y z
the JVM calls your main method with an array containing the command line arguments - here, it would be an array of length 3, with values "x", "y" and "z".
For more details, see the relevant bits of the Java tutorial:
- Passing information to a method or constructor
- Command-line arguments
arguments are the command line options given to your java program when it runs. They are stored in an array so calling arguments.length gives the number of command line inputs to your program.
They are passed in like this on execution
java program argument1, argument2, argument3
In this case arguments.length would return 3 as there are 3 command line arguments.
Generally a parameter is what appears in the definition of the method. An argument is the instance passed to the method during runtime.
You can see a description here: http://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments
The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call. i.e.
- parameter used in function/method definition.
- arguments used in function/method call.
Please have a look at the below example for better understanding:
package com.stackoverflow.works;
public class ArithmeticOperations {
public static int add(int x, int y) { //x, y are parameters here
return x + y;
}
public static void main(String[] args) {
int x = 10;
int y = 20;
int sum = add(x, y); //x, y are arguments here
System.out.println("SUM IS: " +sum);
}
}
Thank you!