OutOfMemoryError

Discussion in 'Java' started by Dlugi, Jul 3, 2007.

  1. Dlugi

    Dlugi Guest

    Hi,

    Is possible to set memory limit which reached throws
    java.lang.OutOfMemoryError exception.


    TIA
    Dlugi, Jul 3, 2007
    #1
    1. Advertising

  2. Dlugi

    Dlugi Guest

    > Is possible to set memory limit which reached throws
    > java.lang.OutOfMemoryError exception.


    I don't express myself exactly, I would like:
    for example when 80% of max memory is used JVM throws exception and then
    I have 20% of memory available to run algorithm which release memory
    Dlugi, Jul 3, 2007
    #2
    1. Advertising

  3. Dlugi

    Twisted Guest

    On Jul 3, 4:49 pm, Dlugi <> wrote:
    > > Is possible to set memory limit which reached throws
    > > java.lang.OutOfMemoryError exception.

    >
    > I don't express myself exactly, I would like:
    > for example when 80% of max memory is used JVM throws exception and then
    > I have 20% of memory available to run algorithm which release memory


    You can use -Xmx256M and the like on the command line to give the JVM
    more memory (e.g. 256 megs) but not to "reserve" some for an algorithm
    to "release memory".

    What you can do is make it easier for the gc to get rid of stuff you
    don't absolutely need. Cached stuff can be held with SoftReference and
    the like; objects you won't be using anymore can be nulled; when
    objects are put in a collection or used as keys that won't be needed
    in there anymore once they aren't being actively referenced anywhere
    else, you can use WeakReference and WeakHashMap; and so forth.

    The JVM will always try to collect garbage and will always toss softly-
    reachable and less reachable objects before it resorts to throwing
    OOME.
    Twisted, Jul 3, 2007
    #3
  4. Dlugi

    Oliver Wong Guest

    "Dlugi" <> wrote in message
    news:f6ecnr$8tu$...
    >> Is possible to set memory limit which reached throws
    >> java.lang.OutOfMemoryError exception.

    >
    > I don't express myself exactly, I would like:
    > for example when 80% of max memory is used JVM throws exception and then
    > I have 20% of memory available to run algorithm which release memory


    AFAIK, this is not possible. What Eclipse does is at the beginning of
    the program, it allocates a large chunk of memory (perhaps using a big
    array), perhaps around 20MB. Then, it tries to catch the OOME, and upon
    doing so, releases that chunk of memory (i.e. it releases the array), then
    runs it's memory recovering algorithm, and presumably re-allocates the
    array.

    If you want to emulate the having the JVM throw the exception at 20%,
    you could create a thread which continuously monitors memory usage and
    throws it's own OOME when it detects memory dropping below a certain
    point, but it seems to me that the exception would be thrown in the
    "wrong" thread, and your other threads might not pause, waiting for your
    memory-releasing algorithm to complete, unless you specifically code them
    to check some sort of flag or other signal to know when they should pause,
    and when they should proceed. In other words, it sounds like your
    OOME-recovery code will be spread out all over your project, instead of
    encapsulated at one point.

    - Oliver
    Oliver Wong, Jul 3, 2007
    #4
  5. Dlugi

    Dlugi Guest

  6. Dlugi

    Tom Hawtin Guest

    Dlugi wrote:
    >> Is possible to set memory limit which reached throws
    >> java.lang.OutOfMemoryError exception.

    >
    > I don't express myself exactly, I would like:
    > for example when 80% of max memory is used JVM throws exception and then
    > I have 20% of memory available to run algorithm which release memory


    Other than free a useless piece of memory or use soft references
    mentioned in other posts. Sun's 1.6 (and possibly other versions) has a
    pair of relevant options.

    -XX:GCHeapFreeLimit sets the percentage of heap that needs to be free in
    order to prevent an OOME. It defaults to 2, so you should set say
    -XX:GCHeapFreeLimit=21. It only does the check after a full GC, and you
    release algorithm better not cause a full GC.

    Also -XX:GCTimeLimit sets maximum percentage CPU usage for GC before
    OOME. Defaults to 98.

    (Disclaimer: I've not actually tried these options.)

    Tom Hawtin
    Tom Hawtin, Jul 3, 2007
    #6
  7. Dlugi

    Oliver Wong Guest

    "Dlugi" <> wrote in message
    news:f6eha7$a8l$...
    > it is possible
    >
    > http://www.javaspecialists.co.za/archive/Issue092.html


    This website is essentially repeating my second suggestion: having a
    thread monitor memory usage. The website's solution has all the same
    problem as my solution:

    <quote>
    your other threads might not pause, waiting for your
    memory-releasing algorithm to complete, unless you specifically code them
    to check some sort of flag or other signal to know when they should pause,
    and when they should proceed. In other words, it sounds like your
    OOME-recovery code will be spread out all over your project, instead of
    encapsulated at one point.
    </quote>

    The article does give an example issue more explicitly: If the other
    threads aren't paused, then there may be thread gobbling up memory faster
    than you can free it, and you'll end up getting an OOME anyway.

    - Oliver
    Oliver Wong, Jul 4, 2007
    #7
  8. Dlugi

    Dlugi Guest

    > -XX:GCHeapFreeLimit sets the percentage of heap that needs to be free in
    > order to prevent an OOME. It defaults to 2, so you should set say
    > -XX:GCHeapFreeLimit=21. It only does the check after a full GC, and you
    > release algorithm better not cause a full GC.

    Thank you, but ...
    Unfortunately, this doesn't work :/
    Dlugi, Jul 4, 2007
    #8
  9. Dlugi

    Dlugi Guest

    > This website is essentially repeating my second suggestion: having a
    > thread monitor memory usage. The website's solution has all the same
    > problem as my solution:
    >
    > <quote>
    > your other threads might not pause, waiting for your
    > memory-releasing algorithm to complete, unless you specifically code them
    > to check some sort of flag or other signal to know when they should pause,
    > and when they should proceed. In other words, it sounds like your
    > OOME-recovery code will be spread out all over your project, instead of
    > encapsulated at one point.
    > </quote>
    >
    > The article does give an example issue more explicitly: If the other
    > threads aren't paused, then there may be thread gobbling up memory faster
    > than you can free it, and you'll end up getting an OOME anyway.
    >
    > - Oliver
    >
    >

    Yes, you're right, this link doesn't solve my problem :/
    I wonder is it possible to do it in java
    Dlugi, Jul 4, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2ZQ==?=

    Serialize session state & OutOfMemoryError?

    =?Utf-8?B?RGF2ZQ==?=, Sep 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,521
    =?Utf-8?B?RGF2ZQ==?=
    Sep 8, 2005
  2. Robert Olofsson
    Replies:
    3
    Views:
    2,967
    Sunitha Kumar
    Jul 3, 2003
  3. Ian Suttle
    Replies:
    2
    Views:
    2,292
    Wolfram Rittmeyer
    Aug 27, 2003
  4. Dean
    Replies:
    1
    Views:
    646
    Robert Olofsson
    Sep 8, 2003
  5. Xiaopeng Xiong

    java.lang.OutOfMemoryError problem

    Xiaopeng Xiong, Nov 14, 2003, in forum: Java
    Replies:
    3
    Views:
    429
    Xiaopeng Xiong
    Nov 15, 2003
Loading...

Share This Page