2

I have limited heap memory in JVM and I cannot increase it as it a embedded device. How ever I would like to execute few logging scripts after out of memory error occurs. how do I do that?

rocktopus
  • 64
  • 1
  • 13

2 Answers2

3

There is an java option flag you can set when starting the JVM, that will execute a given script whhen an OOM occurs.

-XX:OnOutOfMemoryError=string is used to specify a command or script to execute when an OutOfMemoryError is first thrown

DXTR66
  • 563
  • 5
  • 17
0

Here is a little code which from you can get the data you will need.

Actually is nearly impossible catching when you JVM will go on "Out Of Memory".

The only way for doing this should be check if "Free Memory" is getting 0 at runtime, but doing this could be very heavy and so you will reach "Out Of Memory" easly.

int mb = 1024*1024;

//Getting the runtime reference from system
Runtime runtime = Runtime.getRuntime();

//Print used memory
System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

//Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);

//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);

//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
RudiDudi
  • 455
  • 1
  • 7
  • 18
  • Thanks. But as you rightly said its very heavy so I can't use it. But please do reply if you find anything else. – rocktopus Jun 22 '16 at 10:29