Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 allocating too much memory

I am running Eclipse 4.3 with a Java 7 JRE. When I upgrade to a Java 8 JRE, Eclipse suddenly consumes more memory. If I launch Eclipse with JRE 7 directly into my workspace, it allocates 600 MB of RAM according to Task Manager. When I use JRE 8, this value is 750 MB.

Even worse, If I run a large Java program which typically allocates about 10 GB of RAM with JRE 7, switching to JRE 8 causes it to allocate 12 GB of RAM.

Does anybody know what causes this extra RAM allocation? I have tested tweaking different options but with zero success:

-XX:ReservedCodeCacheSize= -XX:MaxMetaspaceSize= -XX:MetaspaceSize= ... 
like image 767
RegedUser00x Avatar asked Sep 08 '15 15:09

RegedUser00x


People also ask

Why is Java taking up so much memory?

Java is also a very high-level Object-Oriented programming language (OOP) which means that while the application code itself is much easier to maintain, the objects that are instantiated will use that much more memory.

Why is my JVM using more memory than XMX?

But in 99% of the cases it is completely normal behaviour by the JVM. What you have specified via the -Xmx switches is limiting the memory consumed by your application heap. But besides the memory consumed by your application, the JVM itself also needs some elbow room.

Is Java 8 have limited storage?

No storage. Streams don't have storage for values; they carry values from a source (which could be a data structure, a generating function, an I/O channel, etc) through a pipeline of computational steps.

How much memory should I allocate to Java?

This resource memory used by the JVM is often called overhead. The recommended minimum starting memory point for 64-bit Maximo 7.5 JVMs systems is 3584 MB. Therefore we recommended that physical memory availability for each JVM be 4096 MB;0.5 GB is for JVM allocation and 512 MB is for overhead.


1 Answers

In the way question is asked

why Java 8 allocating too much memory on my machine

I don't think anyone will be able to answer, however there are several guidelines which might help. Given that you are measuring memory via task manager you are interested in total RSS used. So

  • Step 1: Compare JVM defaults between version you are running. You can get them using java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version command for both jdks. Using text sort on top of output you'll be able to get a nice diff using any compare tool. Things like changing default GC collector and thread stack size will affect final RSS a lot.
  • Step 2: Measure allocation for each memory pool. In general total memory used by java could be calculated using heap + metasize + code cache + native + (thread_stack_size * maximum_number_of_threads)
    • Heap memory easy measurable (and could be easy compared!!) by mature tools (Eclipse Memory Analyzer, VisualVM, etc.). If you memory increase in heap area -you are very lucky. In additional visual vm allow you to install plugin which will show values of all memory pools accessible via jmx.
    • metasize (aka permgen in jdk<8) should more/less equal and could be found with jmap tool. No need to play with flag, you can get number and just compare whenever it's increased or not.
    • Code cache: in addition to reserved code cache there you can set initial code cache (and that will affect how much RSS is used).
    • native: it's a bit of black sheep. If all other memory pools are equal (your memory bump is 2GB) memory lost must be somewhere in native area. The only tool I'm aware of is jcmd and it has extensive documentation in oracle docs.

Non technical - whilst tweaking various options in order to reduce memory can help, the chances of getting right values are close to finding needle in haystack. I would really recommend to have a about how RSS is used in java. This knowledge will be handy for quite a few years!

Please let me know whenever you'd like more concrete references or better explanation. And... good luck with your quest ;-)

like image 189
Petro Semeniuk Avatar answered Sep 17 '22 14:09

Petro Semeniuk