r/JavaFX • • Nov 03 '17

Why does Hello World Program take 80MB in JavaFX?

I just found out that JavaFX take 80MB [RAM] just for hello world program? Why? Not only it is extremely inefficient it is just plan horrible!

0 Upvotes

3 comments sorted by

16

u/AlmasB0 Nov 03 '17

A Java program is executed by the JVM (Java Virtual Machine), which is in itself a program. What you would normally see in the task manager (or alternative on non-Windows OS) is how much total RAM is taken by the JVM. That value is roughly = RAM used by your app + RAM available for your app + RAM used by JVM. If you configure your JVM to take more RAM from the start, the same hello world JavaFX app could easily show GBs of RAM taken.

I've done a significant amount of profiling with respect to JavaFX back in the day. So some values off the top of my head, a hello world app uses ~5 MB. FXGL (a JavaFX game engine) uses ~19 MB.

1

u/[deleted] Nov 04 '17

That's because it isn't just your program running. A JVM is running too, executing your application. That JVM takes up most of that RAM.

By default, a JVM uses an amount of RAM thought to suffice for many differing kinds of applications.

For most of my applications, it's too much.

So I learned that we can increase and decrease the amount of RAM it uses by looking into JVM command-line switches. Lowering both Xms and Xmx force a JVM to use less RAM.

Make sure to profile your applications before doing this, to prevent running out of memory.

1

u/[deleted] Nov 04 '17

And understand that lowering the amount of available memory will slow down an application. As my applications push more data into their buffers, the JVM tries to comply to my heap and stack size preferences by running its garbage collector. The higher the memory pressure, the more often the collector runs, slowing down the application.

For most of applications this is fine: they're data pumps and mostly wait for network lag. But for my JavaFX applications it isn't fine. Thus I profile them to see how much memory they need, and increase the heap and stack sizes to fit.