Results 1 to 11 of 11
  1. #1
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Unhappy Java not using all free memory.

    Hello,

    So can someone explain why I see this behaviour?

    Java Code:
    Runtime runtime = Runtime.getRuntime();
    System.out.println("free memory: " + runtime.freeMemory());
    byte[] a = new byte[4000000];
    System.out.println("free memory: " + runtime.freeMemory());
    gives
    Java Code:
    free memory: 9854904
    free memory: 5854888
    (9854904-5854888 gives 4000016)

    Java Code:
    Runtime runtime = Runtime.getRuntime();
    System.out.println("free memory: " + runtime.freeMemory());
    int[] a = new int[1000000];
    System.out.println("free memory: " + runtime.freeMemory());
    gives
    Java Code:
    free memory: 9854904
    free memory: 5854888
    (9854904-5854888 gives 4000016 (exact same as before))


    Java Code:
    Runtime runtime = Runtime.getRuntime();
    System.out.println("free memory: " + runtime.freeMemory());
    byte[] a = new byte[4000000];
    int[] b = new int[1000000];
    System.out.println("free memory: " + runtime.freeMemory());
    gives
    Java Code:
    free memory: 9854904
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    	at ***.***.***.***.***.***.mergetest.main(mergetest.java:35)
    What on earth is going on?
    Surely allocating those two arrays should only take up 8000032 bytes, well within the 9854904 java reports as having free...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Surely the Java should hide anything about how memory works in a computer from me?

    Am I being stupid here?

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

    Default

    Quote Originally Posted by abacathoo View Post
    Surely the Java should hide anything about how memory works in a computer from me?

    Am I being stupid here?
    This might be a silly question but is there something else (another Thread maybe?) that eats up the rest of the free memory?

    kind regards,

    Jos

  5. #5
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    I doubt it.
    Java Code:
    Runtime runtime = Runtime.getRuntime();
    System.out.println("free memory: " + runtime.freeMemory());
    int[] a = new int[1000000];
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    System.out.println("free memory: " + runtime.freeMemory());
    gives
    Java Code:
    free memory: 9854904
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    free memory: 5854888
    Which I also find surprising since I would thing the print statement itself would leave some rubbish on the heap that wouldn't get garbage collected yet.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    I've just run the second one (that fails with you), with -Xmx12M:

    free memory: 8192808
    free memory: 3141880

    Which gives 5050928, which is a strange number, considering the individual ones are 4000024.

    All I can say is, you're on the edge of your memory there so the JVM might be doing strange things. With larger memory than I gave there I got the expected 8000048 with the two arrays.

  7. #7
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    So is there a way of knowing exactly how much memory I can use without Java freaking out?
    I could maybe hack something like.
    long availiable_mem = r.freeMemory()-1000000;
    I couldn't find any documentation anywhere about how much of the Java free memory you can run into.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Don't worry about it.
    You either need the memory or you don't, don't micro manage it in Java.

    What are you doing such that you feel you need to do this?

  9. #9
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    An external sort, with the VM limited to 10M
    It's an assignment, I think it's set in java just because the people who set it feel like having fun.
    I want to load the biggest chunk from disk that I can without running into an OutOfMemoryException

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    OK.
    Don't push the boundary, because the JVM does more in the background than you can monitor. This sort of thing might make sense in C, but is pretty silly in Java, even on a mobile.

  11. #11
    abacathoo is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. free memory of bufferedimage problem
    By mr_empty in forum Java 2D
    Replies: 2
    Last Post: 01-17-2010, 06:27 PM
  2. Free JAVA IDE - JCODER
    By samnav in forum Other IDEs
    Replies: 1
    Last Post: 08-02-2009, 04:59 PM
  3. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  4. How Can I get free memory ?
    By sathish_2111 in forum Networking
    Replies: 2
    Last Post: 07-19-2007, 04:29 PM

Tags for this Thread

Posting Permissions

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