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?
-
2You can't, you're out of memory.Elliott Frisch– Elliott Frisch2016-06-22 10:03:41 +00:00Commented Jun 22, 2016 at 10:03
-
@ElliottFrisch ..Thanks for quick response. but out of memory is in JVM right? can't I detect it before hand? Is there any technique? There must be some thing we can do.rocktopus– rocktopus2016-06-22 10:07:51 +00:00Commented Jun 22, 2016 at 10:07
-
Probably duplicates with stackoverflow.com/questions/12096403/…waltersu– waltersu2016-06-22 10:27:23 +00:00Commented Jun 22, 2016 at 10:27
Add a comment
|
2 Answers
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
1 Comment
rocktopus
I will try that out.. Thanks for this.
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);
1 Comment
rocktopus
Thanks. But as you rightly said its very heavy so I can't use it. But please do reply if you find anything else.