Proper way to set Direct / Heap memories for distributed jar file?


Up until recently, I had thought I understood how to set the Direct and Heap memory for my application in the SDK, and I’ve never had trouble on the 5-6 devices I’ve been able to test with so far.

But I recently became aware that there are 3 devices (owned by 3 different friends that have just started helping me test my game) that refuse to load my game without an OutOfMemoryError, unless the game is launched from within the SDK, or from the command line with the memory params there

java -XX:MaxDirectMemorySize=1950m -Xms50m -Xmx550m -jar The_Afflicted_Forests.jar

Am I incorrect to have assumed that the JVM parameters in the project properties are always passed along to the distributed jar on other devices? And if so, then what would be the proper way to set the memory parameters so I can fix this error for these specific devices?


What’s weird to me is that for Java 8 I started finding that the max directory memory settings seem to have no affect on direct memory as reported by the JVM. (There is an mbean interface for checking direct memory.)

So I’m curious what you discover in general.

As to this, if it’s the .jar being run directly then there will be no parameters. It will be running with default memory settings.

1 Like

I’ve actually been using your interface to track direct memory for a while and it has always worked consistently to track the current Directory memory being used with the getDirectMemoryUsage() method.

I just checked the method to get the max direct memory value to see if it reports what I set in the JVM argument, and it appears getDirectMemoryTotalCapacity() always returns the same exact value as the method for current memory usage getDirectMemoryUsage().

As for tracking my current memory usage, the interface has been very helpful - I notice that whenever I reach a current DM usage value near the max direct memory parameter set by the JVM arguments, then the game’s framerate will drop for a while until the app inevitably crashes from an OutOfMemory exception (which will be triggered by any random piece code that loads a new asset that tips the memory usage past the max).

I’ve also noticed that all of my devices have a limit where they crash on startup if the JVM argument for max direct memory usage is too high (as well as max heap usage), with an “Unable to allocate memory for JVM” error, but usually its much lower than the amount of total ram for the device. And I think I remember reading something about direct memory and/or heap memory inherently increasing as you increase the other - something like more direct memory usage needed for overhead as you increase the heap memory argument. I think I noticed this first hand at one point when I needed to increase my heap usage JVM param by 100mb to support a new feature that caused OOM crashes with many NPCS, and it solved that heap OOM error, but caused a new direct memory OOM error on the loading screen, which was fixed by increasing the direct memory JVM param slightly.

I guess the quickest solution then would be for me to run the jar from a batch file with the memory params included there.

That is what I normally do. There are also solutions for packaging a exe alongside your jar that will run it with the correct memory settings, and will use the correct version of java. This it normally how I distribute my applications because users seem to have a hard time understanding how to install java.

you mean you distribute jvm along with game right?

No, I have an exe that checks the current java installed on the machine. If an acceptable jre is found it will use it, if not, it will download a copy of java just for that application, into the apps install directory.

1 Like

are you maybe using launch4j?

Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executables. The executable can be configured to search for a certain JRE version or use a bundled one, and it’s possible to set runtime options, like the initial/max heap size. The wrapper also provides better user experience through an application icon, a native pre-JRE splash screen, and a Java download page in case the appropriate JRE cannot be found.

have you seen GitHub - libgdx/packr: Packages your JAR, assets and a JVM for distribution on Windows, Linux and Mac OS X ?
it package JVM along with app for Windows, Linux and Mac OS X.

Packages your JAR, assets and a JVM for distribution on Windows, Linux and Mac OS X, adding a
native executable file to make it appear like a native app. Packr is most suitable for GUI applications, such as games made with libGDX.

Oh, I have not see packr before, that looks nice. I just wrote my own little program in C# for my apps.

1 Like

can you try packr ?

we want use it in future, so please write callback opinion if you will use it :slight_smile: (if you dont, then dont reply)

1 Like

I used Packr for Spoxel. My experience with it was mixed but it did the job for the most part. Packr distributes the jre with the build (although it does attempt some minor pruning). I had it setup in steam so the jre was a separate depot so it wouldn’t re-download with every update. You also pretty much need to use some sort of fat jar system with packr which means you pretty much have to update the entire game and can’t just update say your asset jar. I had issues with OSX so i ended up setting the steam launcher to execute the jar directly. I was getting some sort of crash I think? At any rate, I don’t think they are going to develop packr beyond Java 8. I think they are planning on just letting graalvm handle a native build post java 8.


thanks for opinion, we will look at graalvm in future :slight_smile:

JME had issues when run on above than Java 8 as i remember, but if not then would use higher java version.

Nothing is easy as it might look :slight_smile:

Sorry for off topic post. First and last here.

Yeah, no kidding. I am coming to the conclusion its just to much a pain in the ass to try and manage this stuff.

To me, it looks like it is better to just release requirements for the game, provide the links and let the user handle it.

Things have become to fragmented and trying to handle this stuff for every user, covering every possible scenario, is just not worth the trouble.


yes, some requirements, but do not require too much :slight_smile:

providing JVM along with game IMO is good, but other requirements could be done on user side.

Lets just look at cod MW game, they require Windows 10, while Windows 7 users need add launcher params(for dx11)/etc to run it. and they dont care about this users, just just support Windows 10 + dx12, users complain about that game run much slower on Win 7, but blizzard dont care much about it.

i think every developer should try make user to run game without problem,
but conclusion is that “dont force yourself to make game work everywhere without any user requirement”

1 Like

I appreciate all of the input. For now I have a simple batch file that passes the JVM params I was missing, but I am going to be looking into one of the ways mentioned above that would allow me to install java on the users machine as well.

In regards to Steam, they unfortunately marked my game build as invalid because I did not include an install script for java - I was planning on just telling the users that they need java and leave it up to them to download it (at least for the time being), but it looks like Steam requires that developers make sure the players don’t have to download anything extra on their own, even something as simple as java.

1 Like

how do it look like exactly? steam have own java-installer kind of? well it could save work, but anyway it would be best to provide own VM that you are sure will work as you want.

Anyway thanks for info.

invalid because I did not include an install script for java

so is this possible that you can mark in steam that it just is embeded? or pass empty script?

i belive you can contact them always and ask for sure.

The message they sent me has been deleted unfortunately since it’s been over 60 days.

But after I submitted my app for review by Steam, they essentially just told me that it did not run on one of their devices because the system did not have java, and that I needed to embed it myself, or use one of their install scripts. So if I had known better and embedded it myself from the beginning then they would have not received a launch error, and would have most likely approved my game.

I haven’t looked into it much yet, but they have this documentation for steam install scripts and it looks like it could be used to install pretty much anything that the game requires to run Creating and using InstallScripts (Steamworks Documentation)

1 Like

oh, thats a very usefull link.

Just one question, do they create private steam page, so you can test it first, or its like “guess it will work” and they try it?

i belive you had own steam private page, so you could send code and configuration, check yourself, and later they just try your private page before publish? (sorry if its in link you provided, will read it all later anyway)

Yes, as soon as I paid the Steam Direct fee to partner with Steam, I was immediately allowed to upload my game and I could also give out beta keys to anyone for testing, but the Store page is only available in a private developer mode until the Application passes review and can be publicly listed for sale.
So prior to release, the game is hidden from the steam store and will only shows up in a user’s library if they enter a valid beta key for the game.

So Steam has been a very nice and quick way for me to test new builds of my game among my friends who are sometimes too lazy to download and unzip a packaged jar from a download link I send them :laughing:

When I want to update the game on steam, I just drag and drop the built jar and everything in my “dist” folder into a folder titled “content” and then run a Steam utility called “SteamPipe” to upload it, then I verify that build on the Steamworks website and players will be prompted for the update next time they launch Steam.


It would be super super cool if someone added a steam wiki page with the tips you guys learn along the way.


I probably dont know enough yet to make a full wiki page myself, but I could start another thread where we can continue discussing tips and tricks for steam as well as best practices and common issues for getting a JME game working on steam, and hopefully gather enough info to make a full wiki page soon enough.

In regards to my initial issue with direct memory not being set on certain devices, I’m still having trouble:

I made a batch file with the command to run my game using the exact same JVM memory paramus I used when running it from the command prompt successfully (pasted in my original post), however the game still has an out of memory error when its run from the batch file.

This device still seems to be ignoring the jvm params for memory in a batch file, and only works when its ran from the SDK or manually from the command prompt.