Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to avoid out of memory exception programatically during runtime execution

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

Is there way to avoid out of memory exception programatically?, other than using JVM options "-XX:PermSize" and "-XX:MaxPermSize". I am trying to find solution during runtime of JVM.

Regards
Raj
 
Marshal
Posts: 5005
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has a SoftReference that you can use to keep a weak handle on some expensive object that you have. The JVM knows that it can dereference any object referenced by a SoftReference if it needs to. For example:

And to access your MagicBean

Normally you may well create lots of MagicBean's and consume a load of memory each time until there's none left and the JVM bails, but by using this technique we are telling the JVM that it can take back the memory used by anything referenced through a SoftReference if it needs the memory back. This is a really neat trick to help you keep your application running rather than just falling over unexpectedly. To use this method effectively though you must be prepared for the expensiveThingReference.get() method to return null which will happen if the JVM's claimed back the memory.

You'll find further discussion of this technique in Michael T Nygard's book "Release It!"
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRach!
There isn't a way to programatically prevent and OutOfMemoryError at runtime that is reliable. Some methods of the java.lang.Runtime class offer ways to retrieve memory related information at any given instant, but you can't realiably make use of those to prevent an OutOfMemoryError. The best defense is to profile your application to find out how much memory it needs and to find out if there are any memory leaks. Tweaking the heap and garbage collection settings is usually also worth the effort if you have a feel for your application's memory usage.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you, All.

Regards
Raj
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Datla wrote:Is there way to avoid out of memory exception programatically?, other than using JVM options "-XX:PermSize" and "-XX:MaxPermSize".


As Jelle said: No. And tweaking those parameters is treating the symptom, not the cause. It may buy you a bit of time, but it will almost never solve the problem. Indeed, OOME is itself a symptom - usually of badly-written code.

As Tim suggested, there are some things you can do to help; although I'd suggest that SoftReference's are a last resort, as they can get a bit tricky.

Another, indirect, way of tackling memory issues is to make objects immutable (Google it), because truly immutable objects can share internals. For example, If I write:

String last999999 = millionCharacterString.substring(1);

I am not creating a copy of millionCharacterString.

Indeed, if you look at the memory usage before and after that statement, you'll probably find that it only differs by 20-30 bytes.

It does require thought though, so don't just go making everything immutable because you "heard it here".

Winston
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eek. That is actually not a terrific example, as the substring() method used to copy the entire original char array, which would actually result higher than necessary memory usage. I think that wasn't changed until a minor JDK 7 release, but I may be off the mark a bit there.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:Eek. That is actually not a terrific example, as the substring() method used to copy the entire original char array, which would actually result higher than necessary memory usage. I think that wasn't changed until a minor JDK 7 release, but I may be off the mark a bit there.


Well it's in the v6 openjava source - specifically, the 'offset' field, which is what makes all that nice stuff possible.

But I think the general point is important: you can save a lot of memory if you design immutable classes properly.
If BigInteger, for example, had a "copy" constructor (and I wish it did):

public BigInteger(BigInteger other) {

there would be no need to actually copy the entire contents of other.

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Raj Datla wrote:Is there way to avoid out of memory exception programatically?, other than using JVM options "-XX:PermSize" and "-XX:MaxPermSize".


As Jelle said: No. And tweaking those parameters is treating the symptom, not the cause. It may buy you a bit of time, but it will almost never solve the problem. Indeed, OOME is itself a symptom - usually of badly-written code.



I'd soften that a bit to say that it can be a symptom of poor design or poor coding. There are definitely lots of cases where we legitimately need a lot of memory--or at least more than the default value.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic