Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: heap size vs heap used

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default heap size vs heap used

    I'm getting a java.lang.outofmemoryerror: java heap space exception.

    I downloaded VisualVM to try and monitor.

    It seems the heap size increases but never decreases, however the heap that's used stays reasonable flat, with the odd spike.

    So the heap size doesn't seem to adjust down, only up, and I eventually get the exception. The system I'm using has 12GB of RAM so, no problem there.

    it's the first time I have used VisualVM, so unsure what to look for


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: heap size vs heap used

    I found these two bits of text, I think i'll try System.gc() at each iteration of the bigger loops for starters

    "In addition to freeing unreferenced objects, a garbage collector may also combat heap fragmentation. Heap fragmentation occurs through the course of normal program execution. New objects are allocated, and unreferenced objects are freed such that free blocks of heap memory are left in between blocks occupied by live objects. Requests to allocate new objects may have to be filled by extending the size of the heap even though there is enough total unused space in the existing heap. This will happen if there is not enough contiguous free heap space available into which the new object will fit. On a virtual memory system, the extra paging required to service an ever growing heap can degrade the performance of the executing program. "

    "Fortunately, very good garbage collection algorithms have been developed, and adequate performance can be achieved for all but the most demanding of applications. Because Java's garbage collector runs in its own thread, it will, in most cases, run transparently alongside the execution of the program. Plus, if a programmer really wants to explicitly request a garbage collection at some point, System.gc() or Runtime.gc() can be invoked, which will fire off a garbage collection at that time. "

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: heap size vs heap used

    Make sure you are not holding onto references to objects that are not needed - if you are they cannot be garbage collected. If you need more memory, increase the max heap size using -Xmx VM argument.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: heap size vs heap used

    would GC lower the heap size or the amount of heap used?

    my program is running atm

    heap size: 3.8GB
    heap max: 8GB
    used heap: 350MB
    Last edited by aueddonline; February 10th, 2012 at 10:42 AM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: heap size vs heap used

    Quote Originally Posted by aueddonline View Post
    would GC lower the heap size or the amount of heap used?
    It depends. If memory can be garbage collected then the garbage collector will do its best to free memory as it sees the need.

    my program is running atm
    You need to be more explicit if you intend to use acronyms - I have no idea what you mean by 'atm' (and GC can mean a lot when taken out of context)

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: heap size vs heap used

    does the 'used heap' only include referenced objects? or is this where non-referenced objects are before a garbage collection?

    Noted.

    atm - at the moment

    Thanks for the information. My program seems to be running better now after I added the System.gc() statements. However this particular class is running rather slow in comparison to the rest of my program so I think I'll start again with it.

Similar Threads

  1. Heap Memory
    By vamsi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 19th, 2011, 12:48 PM
  2. binary heap
    By dabdi in forum Collections and Generics
    Replies: 0
    Last Post: November 16th, 2011, 05:43 PM
  3. Increase heap size; 4 GB wall?
    By BKB in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 13th, 2010, 08:55 PM
  4. About heap data type
    By PaddyWangTheGG in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 17th, 2010, 03:02 PM
  5. Implementing a 5-heap with an array
    By TBBucs in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 12th, 2010, 10:56 PM