I found out that I can decrease the memory usage of the program by using the vm arg -Xmx32m. This sets the max heap size to 32 megabytes. Of course, the program still uses more than 32MB of RAM, because there's other memory being used too, such as the metaspace, which by default is 1GB. I didn't change that. But the unloading/loading test (doing it hundreds of times in a single session) yielded memory usage of 130MB instead of 190MB like before. This is because the garbage collector will be more likely to collect garbage if the used heap memory is a higher percent of the max heap size.
There's also the issue of reserved vs. actively used heap memory. Some Java GC options prefer to have a higher amount of reserved heap for performance reasons, because GC can be expensive from a CPU perspective.
But all in all, -Xmx32m seems to work fine for reducing memory usage. However, in the future, when there are more features added to the game, a 32MB heap might become insufficient, and then I'd have to use -Xmx64m, -Xmx128m, or even -Xmx256m. But by default, it seems like the max heap will be about 1/4 of your computer's total RAM.
So it's not a memory leak after all, just that the GC doesn't collect garbage unless it thinks there isn't enough free heap space left.
I found out that I can decrease the memory usage of the program by using the vm arg -Xmx32m. This sets the max heap size to 32 megabytes. Of course, the program still uses more than 32MB of RAM, because there's other memory being used too, such as the metaspace, which by default is 1GB. I didn't change that. But the unloading/loading test (doing it hundreds of times in a single session) yielded memory usage of 130MB instead of 190MB like before. This is because the garbage collector will be more likely to collect garbage if the used heap memory is a higher percent of the max heap size.
There's also the issue of reserved vs. actively used heap memory. Some Java GC options prefer to have a higher amount of reserved heap for performance reasons, because GC can be expensive from a CPU perspective.
But all in all, -Xmx32m seems to work fine for reducing memory usage. However, in the future, when there are more features added to the game, a 32MB heap might become insufficient, and then I'd have to use -Xmx64m, -Xmx128m, or even -Xmx256m. But by default, it seems like the max heap will be about 1/4 of your computer's total RAM.
So it's not a memory leak after all, just that the GC doesn't collect garbage unless it thinks there isn't enough free heap space left.