1

Im using java melody to monitor memory usage in production environment. The requirement is memory not should exceed 256MB/512MB .

I have done maximum of code optimized but still the usage is 448MB/512MB but when i executed garbage collector in java melody manually the memory consumption is 109MB/512MB.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
varun
  • 11
  • 5

2 Answers2

0

Why you actually care about heap usage? as long as you set XMS (maximum heap) you are fine. Let java invoke GC when it seems fit. As long as you have free heap it is no point doing GC and freeing heap just for sake of having a lot of free heap.

If you want to limit memory allocated by process XMX is not enough. You should also limit native memory.

What you should care about is

  • Memory leaks, consecutive Full GCs, GC starvation
  • GC KPIs: Latency, throughput, footprint
  • Object creation rate, promotion rate, reclamation rate…
  • GC Pause time statistics: Duration distribution, average, count, average interval, min/max, standard deviation
  • GC Causes statistics: Duration, Percentage, min/max, total
  • GC phases related statistics: Each GC algorithm has several sub-phases. Example for G1: initial-mark, remark, young, full, concurrent mark, mixed

See https://blog.gceasy.io/2017/05/30/improving-your-performance-reports/ https://blog.gceasy.io/2017/05/31/gc-log-analysis-use-cases/ for more technical details. You could also analyze your GC logs using https://blog.gceasy.io/ it will help you understand how your JVM is using memory.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
0

You can invoke the garbage collection using one of these two calls in your code (they're equivalent):

  • System.gc();
  • Runtime.getRuntime().gc();

It's better if you place the call in a Runnable that gets invoked periodically, depending on how fast your memory limit is reached.