简体   繁体   中英

Java G1 Garbage collector takes a lot of memory

I have project with large database. To parse it I use java with G1 garbage collector. When program runs for a long period of time java begins to consume a lot of memory. But when I check java heap the size is much smaller. For example:

  • Java take 20 Gb of RAM
  • "jmap -histo" - shows me that heap is about 5 Gb of RAM

Question: what is taking the rest of my RAM? Is this overhead of G1?

Edit: Here is stats

我的Java进程的RAM统计信息:分配〜50gb,消耗〜20gb

jmap信息:堆大小〜4gb

java procces: allocated ~50gb, consumed ~20gb
jmap info: heap size ~4gb

I understood the problem. As @Holger mentioned the ram is allocated to java process but not fully filled with heap. But the reason why G1 allocates so many ram:

G1 suffers if it needs to allocate a lot of humongous regions. They will be created each time an object size > 50% of the region size. They will waste space as nothing else will be created in the region. Thus if its size is 51%, you will waste 49% of the region. Worse, if a region is 2MB and your object is 2.1MB, it will waste 1.9MB in the second region. If you allocate large objects, adjust your XX:G1HeapRegionSize.

The RAM consumption will be because of the huge database size and the size of the result set .

Try the below: Optimizing Garbage Collection:

  • be wary of String Concatenation operator (+) use concat() instead

  • if using spring, try setFetchSize(number of rows to be fetched at a time) using setFetchSize will, however, increase your time for execution, but it is memory efficient

  • remove all unnecessary statement

  • Use Asynchronous Execution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM