3 ms·
Java using a large amount of ram is a myth. Let me explain the difference between JVM and other GC'ed languages that makes this appear to be the case. So almos
by slackingoff2017 9y ago
Java using a large amount of ram is a myth. Let me explain the difference between JVM and other GC'ed languages that makes this appear to be the case.
So almost all "web" languages use garbage collection. This reason Java appears to use more ram is, paradoxically because it has a better garbage collector.
The best time to do garbage collection is never. Even if you do it concurrently it uses CPU time. Java is very good about avoiding collecting garbage until the heap is huge. Other languages (by default) use less ram because they run garbage collection a lot more often.
So why do they run GC so much more other than Java? Because their garbage collectors are slower and not as concurrent. Almost all garbage collectors pause execution and even the ones that don't still affect performance. You don't want pauses more than some tens of milliseconds really ever.
Garbage collection time depends on how much garbage you have, so languages with inferior collectors need to keep the heap small to avoid long collection times. Since Java's GC is so fast the heap can be allowed to build to many gigabytes before garbage collection times are an issue.
Turning down the heap size will make Java GC more often and behave more like other languages. Compared to dynamic languages Java itself is very memory efficient. It's just the longer GC interval that makes this appear otherwise.