Results 1 to 13 of 13
  1. #1
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default runing Java program increases continuing memory usage

    Hi
    I have a Java application which i call in a loop and put the same Sring randomally
    to a method and the application write this to system out.
    Wenn I call this program in a loop 200000 times I see that the memory increases in JVM which is not usual I think. Is there a way to give memory free each time when a loop round (1,2,3,4....) is ended??
    regards
    Last edited by henry123; 02-24-2011 at 03:15 PM.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Not. Java can't manage memory. You just are tuning garbage collector or you can use other OOP, which can manage memory for example C/C++.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Petr View Post
    Not. Java can't manage memory. You just are tuning garbage collector
    can I tune and call GC in running loop?how?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by henry123 View Post
    can I tune and call GC in running loop?how?
    There is no need to fiddle with the garbage collector; as long as there is more available memory the gc doesn't do anything, i.e. a new object can be allocated from the available memory. When the jvm runs out of memory the garbage collector fires up and frees (all) unused memory. What you're seeing is normal behaviour. You can set the maximum available heap memory with the -Xmx flag. Better stay away from the garbage collector, it doesn't need any help.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Yep.
    The JVM will often use up memory up to xMx. You've offered it xMx memory so it (not unreasonably) thinks it may as well use it. Then it does a gc.

    If you follow it on a graph it's like a heart beat.

  6. #6
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What you're seeing is normal behaviour.
    Jos
    you say this is a normal behavior, but I have several JAVA applications runing as a batch program (in a loop doing something whole time) and their meory size stay during the execution almost the same. Ok, some of them increase a smal portion memory use, but other increase nothing!. How do we explain that? Apart from that I did observe my applicattion mentioned in my first posting, it increases after one hour almost double memory size :D . Is not that risky, that somewhen a stack overflow ocurs?
    Regards

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The question is, why do you need to free the memory?
    You've givent he JVM a certain amount of memory to work with, so what's wrong with it using that?

    If, as time goes by, the amount available after a garbage collection is shrinking then you might have a problem, but otherwise it's really nothing to worry about.

  8. #8
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    If, as time goes by, the amount available after a garbage collection is shrinking then you might have a problem, but otherwise it's really nothing to worry about.
    Ok, I understand that it is a normal behaviour in JAVA(in other programing languages maybe is not normal) this is now not more my question.
    But the question ist now, how can I understand it, ths same Program does the same test on the same data (conditions are 100% the same and this is not multi threading application) nevertheless increases the memory size continuesly!
    Consider that in a round in a loop, the same program begins and ends and I expect tha the GC begins to clear the heap, each time a loop round ends, where is the GC in this case?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The gc happens whenever the GC decides it needs to. This has nothing to do with your loop at all. It is, by and large, out of your hands.

  10. #10
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    I try to be more clear, let change the question as follow, if the same application (meaning same code with the same input and output data) is called in a loop 1000 times and creats each time f.i 100 objects which are the same objects in each loop, there will exist 1000 x 100 Objects at the end of the loop still in heap? If so it means the old objects in heap (created by this application in previous rounds ) will not be rewritten, but a new version of them are created in the heap?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    No.
    They could be collected sooner.
    Java Code:
    for (int i = 0; i < 1000; i++) {
        Object o = new Object();
    }
    There's nothing to say that gc won't collect some of these objects before the loop has finished.
    If it doesn't then yes, there will be 1000 objects which will all be eligible for garbage collection should it be required.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by henry123 View Post
    I try to be more clear, let change the question as follow, if the same application (meaning same code with the same input and output data) is called in a loop 1000 times and creats each time f.i 100 objects which are the same objects in each loop, there will exist 1000 x 100 Objects at the end of the loop still in heap? If so it means the old objects in heap (created by this application in previous rounds ) will not be rewritten, but a new version of them are created in the heap?
    In the old days the garbage collector was simply a 'mark and sweep' algorithm: as long as more memory was available nothing was done; as soon as a single memory allocation failed the garbage collector marked all reachable chunks of memory, moved them over in memory and released all other memory (there was only garbage in it). This caused your program to stop temporarily while the garbage collector was doing its evil deeds.

    Nowadays the garbage collector tries to avoid this sudden temporay freeze of your program by running in a separate thread and doing a bit of clean up behind the scenes, i.e. some objects might be collected and others are left as garbage. This makes the behaviour of the garbage collector less predictable (to those that are/were used to the simple mark and sweep algorithm) but it most certainly does its job). Only after a full garbage collection not enough memory can be allocated an OutOfMemoryError is thrown. Trying to 'help' the garbage collector is just hindering it most of the time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    @Tals. JosAH
    thanks, now the idea ist more and how the GC handlels data
    Thanks, regards

Similar Threads

  1. Memory increases until window is minimized
    By java_dude in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2011, 01:53 AM
  2. Replies: 4
    Last Post: 11-12-2010, 04:25 AM
  3. Memory usage
    By Moon_E in forum New To Java
    Replies: 20
    Last Post: 07-09-2010, 10:53 PM
  4. run java program in 100% usage
    By mr_empty in forum Advanced Java
    Replies: 5
    Last Post: 01-22-2010, 10:35 PM
  5. JVM memory usage
    By lardum in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 03:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •