3 ms·
It doesn't actually need 840MB of RAM. This is an inherent limitation of the way the JVM works. It will reserve a configured amount of memory upfront and grow t
by imtringued 7y ago
It doesn't actually need 840MB of RAM. This is an inherent limitation of the way the JVM works. It will reserve a configured amount of memory upfront and grow the heap if memory is full until it reaches the configured maximum heap size. One especially annoying thing is that the JVM will not garbage collect until the heap is actually full. This means if you allocate a little bit here and there (like every single Java program in existence) over time you are going to reach the maximum configured heap. Once the JVM has allocated heap space it will never let go of it. Therefore you might as well assume that the minimum amount of memory a JVM process consumes is the maximum that it has been configured for. In this case the maximum was probably set to 840MB. The application itself probably only uses something like 50MB.
Remember those stupid arguments that a competent programmer can write well performing code [0] in any language? Here is the thing... You actually can't. Especially not with the JVM. Let's say you use a super efficient framework that is purely optimized for startup performance and low memory like Micronaut or Quarkus. When you look at your metrics you see that the Java program is consuming 10MB of heap space and pat yourself smugly on the shoulder thinking how well written these frameworks are. Tough luck. Even if you configure the heap size to something absurdly low like 32MB your JVM process will still use 120MB. Why? Because the JVM has a minimum footprint. It has lots of parked GC threads in the background that need 2MB stacks. It has to keep the class files in memory, keep profiling data, the generated code and lots of other things. If I need something that needs to consume as little memory as possible I just stick with C++ or Lua. With those two languages staying under 1 - 2 MB of RAM is trivial.
Also... it is possible your coworkers do not even set a maximum heap and just let it stay on the default values. That's how a tiny little CRUD app as a dashboard ended up allocating 10GB of RAM to itself on a server with 128GB RAM.
[0] well performing = low memory usage in this case
- mrighele 7y ago> Once the JVM has allocated heap space it will never let go of it. While this used to be true in the past, some of the GCs available to the JVM may now uncommit heap memory. This is for sure the case with ZGC [1], G1 [2] and I think also with Shenandoah [3] [1] https://openjdk.java.net/jeps/351 https://openjdk.java.net/jeps/351 [2] https://openjdk.java.net/jeps/346 https://openjdk.java.net/jeps/346 [3] https://shipilev.net/jvm/anatomy-quarks/21-heap-uncommit/ https://shipilev.net/jvm/anatomy-quarks/21-heap-uncommit/
- StreamBright 7y ago> One especially annoying thing is that the JVM will not garbage collect until the heap is actually full. Factually incorrect. Depends on the settings of the GC.
- hedora 7y agoIt is possible to write well-performing Java code. I’ve done it, and know others that have. From looking at multiple projects over many years (as both languages have improved), my rule of thumb is that writing performant Java is about 10x harder than writing memory-safe C++. One particularly insidious, and related problem is that getting the first working Java program is slightly faster than doing it in C++. There are people that have built a career by producing a series of 90% Java solutions, and rapidly moving on. They leave a wake of human suffering behind them in the form of large teams that perform heroic efforts in order to clean up the mess. This certainly wastes many billions of dollars a year.
- hota_mazi 7y ago> my rule of thumb is that writing performant Java is about 10x harder than writing memory-safe C++. This is a bold claim. The JVM is incredibly good at generating fast code, to the point that even naively written Java is guaranteed to run in the ballpark of C/C++. Writing memory safe C++... good luck with that one, especially as soon as you start using external libraries where memory ownership becomes very blurred.