Results 1 to 4 of 4
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Difference between totalMemory and maxMemory in JVM

    Why, when using the getruntine() memory finctions, something like as -
    Java Code:
    System.out.println("" + Runtime.getRuntime().freeMemory());
    System.out.println("" + Runtime.getRuntime().totalMemory());
    System.out.println("" + Runtime.getRuntime().maxMemory());
    I got -
    7,825,536
    8,126,464
    129,761,280
    What maxMemory means - and how it differs from totalMemory() -- why it is so small even to comparing
    overal PC Operative memory - 512 mb.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Difference between totalMemory and maxMemory in JVM

    Did you set a value for -Xmx and -Xms at start of your program? Tinker with those values and see what happens.

    And this: java - What is the exact meaning of Runtime.getRuntime().totalMemory() and freeMemory()? - Stack Overflow
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    6

    Default Re: Difference between totalMemory and maxMemory in JVM

    If we set xmx by IDE or by a system of maxMemory, why just about 7-8% is used of it for total memory.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Difference between totalMemory and maxMemory in JVM

    Be brave and make a little test program. I saved you the trouble and made you one. Run this, with different -Xmx flags as VM parameter and compare the output.
    Java Code:
    package forum;
    
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class Memory {
    
        private static NumberFormat numberInstance = NumberFormat.getNumberInstance();
    
        public static void main(String[] args) {
            List<Integer> list = new ArrayList<>();
    
            Random random = new Random();
    
            for (int i = 0; i < 100_000_000L; i++) {
                if (i % 1_000_000 == 0) {
                    printMemoryStats(list.size());
                }
                list.add(random.nextInt());
            }
    
        }
    
        private static void printMemoryStats( int size) {
            System.out.printf("Size: %s, Free: %s; Total: %s, Max: %s\n", numberInstance.format(size),
                    numberInstance.format(Runtime.getRuntime().freeMemory()),
                    numberInstance.format(Runtime.getRuntime().totalMemory()),
                    numberInstance.format(Runtime.getRuntime().maxMemory()));
        }
    }
    If you study the output below, you see that (simplified):
    Free: is what is left of the heap. This number fluctuates, because the Garbace Collector is frantially trying to remove unused objects to free memory for the program.
    Total: is what is used of the heap
    Max: the maximum size of the heap

    -Xmx64:
    Size: 0, Free: 62,473,728; Total: 64,487,424, Max: 64,487,424
    Size: 1,000,000, Free: 37,449,616; Total: 64,487,424, Max: 64,487,424
    Size: 2,000,000, Free: 20,451,808; Total: 64,487,424, Max: 64,487,424
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    -Xmx512m:
    Size: 0, Free: 252,056,472; Total: 257,425,408, Max: 477,626,368
    Size: 1,000,000, Free: 221,302,544; Total: 257,425,408, Max: 477,626,368
    Size: 2,000,000, Free: 204,113,584; Total: 257,425,408, Max: 477,626,368
    Size: 3,000,000, Free: 172,358,992; Total: 257,425,408, Max: 477,626,368
    Size: 4,000,000, Free: 155,734,696; Total: 257,425,408, Max: 477,626,368
    Size: 5,000,000, Free: 201,613,808; Total: 324,534,272, Max: 477,626,368
    Size: 6,000,000, Free: 186,177,776; Total: 324,534,272, Max: 477,626,368
    Size: 7,000,000, Free: 131,248,656; Total: 324,534,272, Max: 477,626,368
    Size: 8,000,000, Free: 115,812,624; Total: 324,534,272, Max: 477,626,368
    Size: 9,000,000, Free: 318,232,168; Total: 503,316,480, Max: 503,316,480
    Size: 10,000,000, Free: 247,180,944; Total: 503,316,480, Max: 503,316,480
    Size: 11,000,000, Free: 230,168,136; Total: 503,316,480, Max: 503,316,480
    Size: 12,000,000, Free: 214,497,528; Total: 503,316,480, Max: 503,316,480
    Size: 13,000,000, Free: 198,826,920; Total: 503,316,480, Max: 503,316,480
    Size: 14,000,000, Free: 160,051,000; Total: 525,860,864, Max: 525,860,864
    Size: 15,000,000, Free: 144,760,080; Total: 525,860,864, Max: 525,860,864
    Size: 16,000,000, Free: 129,469,160; Total: 525,860,864, Max: 525,860,864
    Size: 17,000,000, Free: 114,178,240; Total: 525,860,864, Max: 525,860,864
    Size: 18,000,000, Free: 98,592,560; Total: 525,860,864, Max: 525,860,864
    Size: 19,000,000, Free: 135,542,304; Total: 525,860,864, Max: 525,860,864
    Size: 20,000,000, Free: 120,116,544; Total: 525,860,864, Max: 525,860,864
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space


    -Xmx2g:
    Size: 0, Free: 252,056,472; Total: 257,425,408, Max: 1,908,932,608
    Size: 1,000,000, Free: 221,302,544; Total: 257,425,408, Max: 1,908,932,608
    Size: 2,000,000, Free: 204,129,984; Total: 257,425,408, Max: 1,908,932,608
    Size: 3,000,000, Free: 172,375,392; Total: 257,425,408, Max: 1,908,932,608
    Size: 4,000,000, Free: 155,751,096; Total: 257,425,408, Max: 1,908,932,608
    Size: 5,000,000, Free: 201,621,920; Total: 324,534,272, Max: 1,908,932,608
    Size: 6,000,000, Free: 186,185,792; Total: 324,534,272, Max: 1,908,932,608
    Size: 7,000,000, Free: 131,256,560; Total: 324,534,272, Max: 1,908,932,608
    Size: 8,000,000, Free: 115,820,432; Total: 324,534,272, Max: 1,908,932,608
    Size: 9,000,000, Free: 377,493,344; Total: 562,561,024, Max: 1,908,932,608
    ...
    Size: 61,000,000, Free: 619,292,536; Total: 1,877,999,616, Max: 1,908,932,608
    Size: 62,000,000, Free: 602,771,928; Total: 1,877,999,616, Max: 1,908,932,608
    Size: 63,000,000, Free: 586,251,320; Total: 1,877,999,616, Max: 1,908,932,608
    Size: 64,000,000, Free: 569,730,712; Total: 1,877,999,616, Max: 1,908,932,608
    Size: 65,000,000, Free: 556,229,824; Total: 1,877,999,616, Max: 1,908,932,608
    Size: 66,000,000, Free: 567,920,544; Total: 1,908,932,608, Max: 1,908,932,608
    Size: 67,000,000, Free: 553,657,272; Total: 1,908,932,608, Max: 1,908,932,608
    Size: 68,000,000, Free: 539,394,000; Total: 1,908,932,608, Max: 1,908,932,608
    Size: 69,000,000, Free: 520,376,304; Total: 1,908,932,608, Max: 1,908,932,608
    Size: 70,000,000, Free: 506,113,032; Total: 1,908,932,608, Max: 1,908,932,608
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    An interesting obesrvation when you change -Xms also: (Hint: the interesting part is in Total)
    -Xmx2g -Xms2g:
    Size: 0, Free: 2,015,362,840; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 1,000,000, Free: 1,993,867,000; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 2,000,000, Free: 1,950,683,840; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 3,000,000, Free: 1,923,526,832; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 4,000,000, Free: 1,902,030,992; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 5,000,000, Free: 1,866,669,456; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 6,000,000, Free: 1,845,173,616; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 7,000,000, Free: 1,797,505,280; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 8,000,000, Free: 1,776,009,440; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 9,000,000, Free: 1,765,261,520; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 10,000,000, Free: 1,688,385,064; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 11,000,000, Free: 1,677,637,144; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 12,000,000, Free: 1,666,889,224; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 13,000,000, Free: 1,645,393,384; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 14,000,000, Free: 1,551,574,544; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 15,000,000, Free: 1,530,078,704; Total: 2,058,354,688, Max: 2,058,354,688
    ...
    Size: 42,000,000, Free: 1,072,055,800; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 43,000,000, Free: 1,051,490,056; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 44,000,000, Free: 1,041,207,184; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 45,000,000, Free: 1,020,641,440; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 46,000,000, Free: 1,000,075,696; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 47,000,000, Free: 709,428,528; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 48,000,000, Free: 688,862,784; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 49,000,000, Free: 678,579,912; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 50,000,000, Free: 658,014,168; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 51,000,000, Free: 647,731,296; Total: 2,058,354,688, Max: 2,058,354,688
    Size: 52,000,000, Free: 623,672,456; Total: 2,058,354,688, Max: 2,058,354,688
    ...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Difference between JRE and JDK
    By allaudin in forum New To Java
    Replies: 1
    Last Post: 05-28-2013, 12:19 PM
  2. what is the difference
    By ron87 in forum New To Java
    Replies: 5
    Last Post: 01-04-2011, 04:31 PM
  3. what the difference?!!
    By Engineer in forum New To Java
    Replies: 14
    Last Post: 08-29-2010, 04:57 PM
  4. difference
    By nishant in forum New To Java
    Replies: 2
    Last Post: 09-17-2008, 06:04 PM
  5. Difference between ASP and JSP
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:15 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
  •