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?

3
  • 2
    You can't, you're out of memory. Commented 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. Commented Jun 22, 2016 at 10:07
  • Probably duplicates with stackoverflow.com/questions/12096403/… Commented Jun 22, 2016 at 10:27

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

I will try that out.. Thanks for this.
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);

1 Comment

Thanks. But as you rightly said its very heavy so I can't use it. But please do reply if you find anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.