Minimizing Lag on a Plugin Server

### Optimizing a Plugin Server

There are many myths on what does and does not help when it comes to reducing server lag. People often will add the plugin "ClearLagg" or "Mob Stacker" plugins to their server thinking that will reduce their lag. That is ***not*** the step you should be taking to reduce lag. In reality, most of what you need to do involves editing the server software files (I.e. server.properties, bukkit.yml, paper's relevant files, and optionally purpur.yml).

**The guide below covers an in-depth guide of the things you should be doing to improve performance and stability on your server.**

https://github.com/YouHaveTrouble/minecraft-optimization

### Java Startup Flags

When your server starts it does so by running a Java command with some Java flags to specify how much RAM to allocate to your server (by `-Xms2G -Xmx2G` for instance). It is recommended to use Aikar's Java Startup flags for the most stable performance. You may read more about it on the official web page here.

If you're using a Minecraft Host create a ticket with them and request they change your server's Java Startup Flags to the below information.

```
java -Xms10G -Xmx10G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar server.jar nogui

```
This example is using 10 GB of RAM. The amount of RAM will need to be adjusted to whatever you are paying your Minecraft host for.

#### IMPORTANT – Don’t use ALL of your memory PTERODACTYL USERS!

As the official page warns for MC hosts using the Pterodactyl panel:

```
When setting the Xms and Xmx values (RAM values), if your host says you have 8000M memory (8 GB), DO NOT USE 8000M! Minecraft (and Java) needs additional memory on top of that Xmx parameter. It is recommended to reduce your Xmx/Xms by about 1000-1500M to avoid running out of memory or “OOMKiller” hitting your server. This also leaves room for the Operating System to use memory too.
Have 8000M memory? Use 6500M for safety. But you may also ask your host if they will cover this overhead for you and give you 9500M instead. Some hosts will! Just ask.
```

### RAM Recommendations

Plugin servers should have 2 - 10 Gigabytes (GB) of RAM for normal use. If you're using Aikar's Java Startup flags it is recommended to use 4 - 12 GB of RAM.

Having **more than** 12 GB of RAM on your server **will not** increase performance in most cases due to how Java's Garbage Collection feature works.

### Reviewing Timings

Timings is a feature on plugin servers that will allow your server to automatically log what is causing lag then it will allow you to review the data. If you've already optimized your server and are still experiencing problems then this can be a great way to narrow down what specifically is causing lag on your server.

The video below is only 7 minutes long and describes how to use timings very well. **However,** bear in mind they mention using "ClearLagg" and "Mob Stacker" plugins to optimize your server. These are a MYTH and in almost all cases should not be used / better solutions exist (see the guide on optimizing your server above).

YouTube Explanation: https://youtu.be/Sb0c4N8kJvI