问题描述:

I use the datastax driver to create NIO connections with Cassandra without do any other thing but just keep connection.

What's more,

when I set the XMX to 1G, the RES(from top -p [java pid]) will occupied about 400M memory,

but when I set the XMX to 512M, the RES will only occupied about 200M memory,

How to understand it? from my opinion, my code do the same thing, why occupied different memory?

Cluster build = Cluster.builder().addContactPoint("10.224.57.163").withAuthProvider(new PlainTextAuthProvider("fujian","pass")).withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("DC1")).build();

Session connect = build.connect("demo");

网友答案:

There are two factors in play

  1. java does not release managed heap memory eagerly. If you give it more room it may use that to run GCs less often (saves CPU cycles)
  2. NIO might be using off-heap memory (DirectByteBuffer) which is not released until the Buffers holding onto the memory are GCed. Thus GCs running less frequently might not only occupy more of the managed heap but also release off-heap memory later and thus incur additional garbage.

You can try -XX:MaxHeapFreeRatio=30 -XX:MinHeapFreeRatio=20 to constrain the actual heap size closer to the actual occupancy, i.e. to release memory sooner.

相关阅读:
Top