0

I am making an application where it is likely that the user will get an OutOfMemoryError; now I would like to find a way in which the user can be notified that the program experienced an OutOfMemoryError so he/she can act accordingly. (e.g. restart the program with a larger heap space etc)

How to "catch" the error so I can notify the user?

Samantha Catania
  • 4,516
  • 5
  • 36
  • 61
  • Isn't it too late to do anything, once you get that exception? – muratgu Dec 09 '11 at 22:01
  • @muratgu "It depends." Will you burn yourself if playing with fire? :) -- also, "*Error*" is more appropriate than "*Exception*" for these sort of things. –  Dec 09 '11 at 22:08
  • 1
    possible duplicate of [Catching java.lang.OutOfMemoryError](http://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror) –  Dec 09 '11 at 22:09
  • http://stackoverflow.com/questions/1692230/is-it-possible-to-catch-out-of-memory-exception-in-java , http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error –  Dec 09 '11 at 22:11

1 Answers1

5

One of the first lessons of a good user experience is to not require the user to do more work than what is necessary.

This means that your idea to "notify the user so they can restart" is poor design and should be avoided.

Instead, evaluate your application and determine why it runs out of memory. Are there segments of data that can be dumped into a storage file while they're not used to free up memory? Is there some form of efficiency you could correct with better design patterns, data representation or algorithms?

Don't be reactive to the issue, be preventative. In the event that the OutOfMemoryError is unavoidable the application should have a way to save state before crashing.

Read some articles for further ideas: http://www.javaperformancetuning.com/news/qotm036.shtml

Grambot
  • 4,057
  • 4
  • 26
  • 42
  • The program I'm working on receives an object (from other computer on the network), this object can be very large at times; but it has to go through memory. Do you think it is feasible to set the maximum heap space to say 8gb to make sure everything is covered? – Samantha Catania Dec 09 '11 at 22:10
  • @SamanthaCatania, you can set the heap to 8GB on a 64 bit OS and jvm. You should assess the maximum size of the object and size your heap accordingly. can you not restrict the size via your interface? why is the object huge and can it be taken in parts? or lazy loaded? – aishwarya Dec 09 '11 at 22:16
  • @aishwarya the object is huge 'cause it might contains a movie in it or similar large media files. Can objects be broken down in this kind of situations? – Samantha Catania Dec 09 '11 at 22:35
  • well you could just stream the movie instead of downloading it in one go... may be a simpler way is to use ftp or something to get the files instead. – aishwarya Dec 10 '11 at 06:15