freeing memory

Discussion in 'Java' started by Rajesh.Rapaka, Apr 20, 2005.

  1. Hi,

    I am using internal frame in my program. How can i free the memory it
    used when i close and Internal frame ??

    plz help.

    regards,
    Rajesh Rapaka.
    Rajesh.Rapaka, Apr 20, 2005
    #1
    1. Advertising

  2. "Rajesh.Rapaka" <> schreef in bericht
    news:...
    > Hi,
    >
    > I am using internal frame in my program. How can i free the memory it
    > used when i close and Internal frame ??


    Dispose and set references to null.
    Boudewijn Dijkstra, Apr 20, 2005
    #2
    1. Advertising

  3. Hi Boudewijn,

    I've done that. But no use. I've even tried gc() and freememory()
    funtions. neither helped.

    any other ideas??

    regards
    Rajesh Rapaka.
    Rajesh.Rapaka, Apr 20, 2005
    #3
  4. Rajesh.Rapaka

    Tony Morris Guest

    "Rajesh.Rapaka" <> wrote in message
    news:...
    > Hi Boudewijn,
    >
    > I've done that. But no use. I've even tried gc() and freememory()
    > funtions. neither helped.
    >
    > any other ideas??
    >
    > regards
    > Rajesh Rapaka.
    >


    Helped what?
    Setting references to null does nothing more than set references to null -
    don't be fooled by the ever dominant fallacies that exist out there.
    Do you have a resource leak? Grab yourself a profiler and find it.

    --
    Tony Morris

    JTiger Unit Test Framework for J2SE 1.5
    http://www.jtiger.org/
    Java Q&A (FAQ, Trivia)
    http://qa.jtiger.org/
    http://xdweb.net/~dibblego/
    Tony Morris, Apr 20, 2005
    #4
  5. hi,

    > Helped what?

    Neither of the process helped in freeing the memory. I've set the
    resources to null and garbage collected it. but this wouldnt help me in
    freeing the memory. how can i free the memory?

    regards
    Rajesh Rapaka.
    Rajesh.Rapaka, Apr 20, 2005
    #5
  6. Rajesh.Rapaka

    Tony Morris Guest

    "Rajesh.Rapaka" <> wrote in message
    news:...
    > hi,
    >
    > > Helped what?

    > Neither of the process helped in freeing the memory. I've set the
    > resources to null and garbage collected it. but this wouldnt help me in
    > freeing the memory. how can i free the memory?
    >
    > regards
    > Rajesh Rapaka.
    >


    You can't set a resource to null, nor can you garbage collect it, so it
    seems you have a few concepts to brush up on.
    Grab yourself a profiler and get cracking - usually only takes 20-30 seconds
    to diagnose the issue if it's easily produced.

    --
    Tony Morris

    JTiger Unit Test Framework for J2SE 1.5
    http://www.jtiger.org/
    Java Q&A (FAQ, Trivia)
    http://qa.jtiger.org/
    http://xdweb.net/~dibblego/
    Tony Morris, Apr 20, 2005
    #6
  7. Rajesh.Rapaka

    Guest

    > Neither of the process helped in freeing the memory. I've set the
    > resources to null and garbage collected it. but this wouldnt help me in
    > freeing the memory. how can i free the memory?


    You simply cannot free memory 'by hand'. If you set references to
    null, it just means the the garbage-collector COULD free the memory
    if it wants to.

    And even if you call the garbage-collector yourself, that's just
    a SUGGESTION to the VM to run the garbage-collector.

    In reality, the garbage-collector will only start to free memory
    if the memory on the computer is starting to run out (!).
    , Apr 20, 2005
    #7
  8. Rajesh.Rapaka

    HK Guest

    HK, Apr 20, 2005
    #8
  9. Rajesh.Rapaka

    . Guest

    On Wed, 20 Apr 2005, Rajesh.Rapaka wrote:

    > hi,
    >
    > Neither of the process helped in freeing the memory. I've set the
    > resources to null and garbage collected it. but this wouldnt help me in
    > freeing the memory. how can i free the memory?


    You cannot explicitly free memory in Java. You can set all references to
    an object to null. If nothing is referencing the object that the garbage
    collector SHOULD free the memory. If you want it to free immediately then
    you can call System.gc(); and it might run the garbage collector right
    away. Note that the API docs clear say that calling System.gc() SUGGESTS
    that the JVM expend effort towards recycling ununsed objects.

    Bottom line, you cannot guarantee that the system will reclaim the memory.

    If you are getting OutOfMemoryExceptions and there are objects that you
    believe have no references to them that are not getting freed then maybe
    you have a memory leak. Google on "java memory leak" and you should find
    some articles on it.

    The most common problem is that you have a reference to the object and you
    just don't realize it. For a large system it will tend to be a reference
    to an object that has a reference to an object that has a reference to the
    object you believe should be free. In other words, it is not always that
    easy to spot.

    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
    ., Apr 20, 2005
    #9
  10. Rajesh.Rapaka

    Daniel Dyer Guest

    On Wed, 20 Apr 2005 13:48:48 +0100, <> wrote:

    > In reality, the garbage-collector will only start to free memory
    > if the memory on the computer is starting to run out (!).


    That's not true, it depends on the JVM implementation (the JVM spec. does
    not define how a garbage collector should behave) and its configuration.
    Generally the heap cannot grow beyond a pre-configured maximum (-Xmx on
    Sun VMs) so it will not use all of the computer's memory (unless you have
    configured the heap to be allowed to grow that big).

    Sun's VMs will increase the heap size as required but never reduce it.
    With this behaviour the heap size would rapidly hit the maximum permitted
    if they didn't ever perform garbage collection before the limit is reached.

    System.gc() doesn't guarantee garbage collection will be performed, but it
    usually is performed when this call is made.


    Dan.

    --
    Daniel Dyer
    http://www.footballpredictions.net
    Daniel Dyer, Apr 20, 2005
    #10
  11. Rajesh.Rapaka

    Tim Ward Guest

    ""."" <> wrote in message
    news:p...
    >
    > The most common problem is that you have a reference to the object and you
    > just don't realize it. For a large system it will tend to be a reference
    > to an object that has a reference to an object that has a reference to the
    > object you believe should be free. In other words, it is not always that
    > easy to spot.


    Swing-type listener lists are a good place to start looking :)

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
    Tim Ward, Apr 20, 2005
    #11
  12. Joseph Dionne, Apr 20, 2005
    #12
  13. Daniel Dyer wrote:

    > Sun's VMs will increase the heap size as required but never reduce it.


    I have logs of the number returned by Runtime.totalMemory() decreasing
    over time. But maybe what it reports is not the heap size?
    Thomas Schodt, Apr 20, 2005
    #13
  14. Rajesh.Rapaka

    Mark Johnson Guest

    ""."" <> wrote in message
    news:p...
    > On Wed, 20 Apr 2005, Rajesh.Rapaka wrote:


    > You cannot explicitly free memory in Java. You can set all references to
    > an object to null. If nothing is referencing the object that the garbage
    > collector SHOULD free the memory. If you want it to free immediately then
    > you can call System.gc(); and it might run the garbage collector right
    > away. Note that the API docs clear say that calling System.gc() SUGGESTS
    > that the JVM expend effort towards recycling ununsed objects.
    >
    > Bottom line, you cannot guarantee that the system will reclaim the memory.


    System.gc() will of course 'suggest' to the JVM that it should reclaim
    garbage to the system heap. My past experiences with a variety of
    Java platforms (Windows, Solaris, HP-UX, IRIX to name just a few)
    is that there is a lot of variation within JVM implementations as to
    whether they will release unused heap back to the OS global
    memory pool. In most cases Unix systems will NOT do so...gc'd
    memory goes back in the JVM's free heap but utilities like 'top'
    or 'swapinfo' will show that the process running the JVM has a
    monotone-increasing pattern to its total memory use. Eventually
    the process will hit either some per-process limit in swap, VM
    pages, or something...or if the per-process limits are set really
    high, a global resource will be exhausted. The only way out is to
    exit the JVM and restart...

    Interestingly enough, Windows (at least Windows XP and 2003)
    JVM's I've used seem to release gc'd heap memory back to the
    OS.

    If you can get access to the source code for the JVM libraries,
    look for calls to the POSIX (?) sbrk() routine, which releases
    memory from a process back to the OS. A JVM that does so
    will (eventually) reduce its system memory use.
    Mark Johnson, Apr 20, 2005
    #14
  15. Hi,

    Thank you for all the answers gentlemen. But isn't there a way by which
    I can free the used memory as soon as I close the window, without
    acessing the VM. I believe java should actually be releasing the memory
    it allocated for the diplaying the Internal Frame as soon as it is
    disposed.

    I somehow don't get the idea of expanding the heap or manipulating the
    VM appealing. Because any program written is ment to be installed on
    several machines which have various memory sizes and various virtual
    machines. So I dont get the idea appealing. (plz correct if my attitued
    is wrong).

    regards,
    Rajesh Rapaka.
    Rajesh.Rapaka, Apr 21, 2005
    #15
  16. Rajesh.Rapaka

    digidigo Guest

    Are you asking on how to get Java to release the memory back to the
    System? OR are you asking for Java to release the memory back to the
    Java free heap ?

    How do you know that you are not getting the results that you are
    looking for?

    -confused
    Dave
    digidigo, Apr 21, 2005
    #16
  17. Rajesh.Rapaka

    Betty Guest

    "Rajesh.Rapaka" <> wrote in message
    news:...
    >
    > Hi,
    >
    > Thank you for all the answers gentlemen.

    (and gentleladies) But isn't there a way by which
    > I can free the used memory as soon as I close the window, without
    > acessing the VM. I believe java should actually be releasing the memory
    > it allocated for the diplaying the Internal Frame as soon as it is
    > disposed.
    >
    > I somehow don't get the idea of expanding the heap or manipulating the
    > VM appealing. Because any program written is ment to be installed on
    > several machines which have various memory sizes and various virtual
    > machines. So I dont get the idea appealing. (plz correct if my attitued
    > is wrong).
    >
    > regards,
    > Rajesh Rapaka.
    >
    Betty, Apr 21, 2005
    #17
  18. Rajesh.Rapaka

    Eric Sosman Guest

    Rajesh.Rapaka wrote:
    > Hi,
    >
    > Thank you for all the answers gentlemen. But isn't there a way by which
    > I can free the used memory as soon as I close the window, without
    > acessing the VM. I believe java should actually be releasing the memory
    > it allocated for the diplaying the Internal Frame as soon as it is
    > disposed.
    >
    > I somehow don't get the idea of expanding the heap or manipulating the
    > VM appealing. Because any program written is ment to be installed on
    > several machines which have various memory sizes and various virtual
    > machines. So I dont get the idea appealing. (plz correct if my attitued
    > is wrong).


    You aren't "wrong," but the set of programs that would
    benefit from returning memory to the O/S is smaller than
    one might expect. Consider:

    1: At some point in its execution, the program must
    acquire a large amount of memory (otherwise, it can
    only return a small amount, and this isn't a large
    benefit).

    2: Having acquired a large amount of memory, the program
    must enter a phase where it needs far less (otherwise,
    it could only return a small amount).

    3: After returning the unneeded memory, the program must
    continue to run for a long time (returning a gigabyte
    one millisecond before termination doesn't help much).

    4: There should not be another "mountain" on the other
    side of the memory "valley" (otherwise the program
    would need to request memory from the O/S a second
    time, and re-requesting the memory would certainly
    take longer than simply re-using memory that has
    been hanging around).

    So: The overall picture is that of a program that runs
    in "phases," where the early phases require a lot of memory
    and the later phases (which run for a long time) require very
    little. One can imagine such programs, but they're fairly
    unusual -- and some of the counter-examples might profitably
    be broken into two or more programs anyhow.

    Of course, there are exceptions. One can imagine a long-
    running program that alternates between periods of heavy memory
    use and lengthy periods of light use -- maybe it does very
    little most of the time, but every hour on the hour it wakes
    up and rummages through a twenty-terabyte database before going
    dormant again. The system as a whole might benefit from being
    able to reclaim this program's memory for fifty minutes out of
    each hour. Also, one can take issue with point 4 above: if
    you've got a gigabyte of useless memory pages sitting on the
    swap device, it may well be cheaper to tell the O/S you don't
    want them any more and then ask for new pages than to fault in
    the old ones merely to overwrite their useless contents. Still
    and all, the "obvious" benefits of returning memory to the O/S
    usually turn out to be smaller than is "obviously" the case.

    (By the way, the above is not merely airy theorizing.
    At a PPOE I was coerced into writing a memory manager that
    returned unused pages to the O/S, so our memory-hungry program
    wouldn't look quite so piggy. Result: The program slowed down
    noticeably, and the actual memory usage really didn't decrease
    very much. I'm not making generalizations based on thin air;
    my claims are based on a statistically significant sample of
    size N=1 ;-)

    --
    Eric Sosman, Apr 21, 2005
    #18
    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. Harald Kirsch

    freeing memory

    Harald Kirsch, Apr 22, 2005, in forum: Java
    Replies:
    0
    Views:
    417
    Harald Kirsch
    Apr 22, 2005
  2. Hassan Iqbal
    Replies:
    3
    Views:
    478
    Al Bowers
    Sep 25, 2003
  3. freeing memory....partially

    , Oct 4, 2003, in forum: C Programming
    Replies:
    12
    Views:
    552
  4. binaya

    freeing allocated memory

    binaya, Oct 19, 2003, in forum: C Programming
    Replies:
    11
    Views:
    582
    James Hu
    Oct 19, 2003
  5. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    578
    Jean-Claude Arbaut
    Jun 15, 2005
Loading...

Share This Page