Problem with Runtime.freeMemory () - Java

Hey, I'm testing Runtime.freeMemory () with this code:

 Runtime runtime = Runtime.getRuntime();
 long freeMemory = runtime.freeMemory();

 // This is some arbitary factor that should work <= 1
 double factor = 0.8;

 int size = (int) (factor * freeMemory);
 byte[] testArray = new byte[size];

I am creating an array of size bytes close to the value of freeMemory. For some reason, especially when I limit the program memory to about 8 MB, the code throws an OutOfMemory exception for any coefficient> 0.55. It really doesn't make sense, of course, freeMemory means freeMemory, I expect it to be a little, but not twice as much as it really is for free.

Any suggestions on what's going on? Thanks

(Note that my tests limit memory available to 8 MB or 16 MB using -Xmx8M, etc.)

+3
source share
5

, : http://java.sun.com/docs/hotspot/gc1.4.2/ one - "" , - . -verbose: gc -XX: + PrintGCDetails VM, , . , , .

+2

runtime.gc().

JVM , , .


> 1. 1,25, . " ".


, , "maxMemory()".

public class Mem2 {
    public static void main(String[] args) {


        Runtime runtime = Runtime.getRuntime();
        runtime.gc();

        long freeMemory = runtime.freeMemory();

        // This is some arbitary factor that should work <= 1
        double factor = 1.29;

        int size = (int) (factor * freeMemory);
        System.out.println("      freememory is " + freeMemory);
        System.out.println("            size is " + size);
        System.out.println("the total memory is " + runtime.totalMemory());
        System.out.println("  the max memory is " + runtime.maxMemory());
        byte[] testArray = new byte[size];
    }
}

:

      freememory is 84466864
            size is 108962254
the total memory is 85000192
  the max memory is 129957888

Process finished with exit code 0

, , 20 .

, totalMemory() - , JVM, freeMemory() - , , maxMemory() - .


totalMemory() freememory() .

public class Mem3 {

    public static void main(String[] args) {


        Runtime runtime = Runtime.getRuntime();

        for (int i = 0; true; i++) {
            runtime.gc();
            int size = i * 10000000;
            System.out.println("                 i is " + i);
            System.out.println("              size is " + size);
            System.out.println("b       freememory is " + runtime.freeMemory());
            System.out.println("b the total memory is " + runtime.totalMemory());
            System.out.println("b   the max memory is " + runtime.maxMemory());
            byte[] testArray = new byte[size];
            System.out.println("                array " + testArray.length);
            System.out.println("a       freememory is " + runtime.freeMemory());
            System.out.println("a the total memory is " + runtime.totalMemory());
            System.out.println("a   the max memory is " + runtime.maxMemory());
            System.out.println(" ");
        }

    }
}

, , . , 6 7:

                 i is 6
              size is 60000000
b       freememory is 84300496
b the total memory is 85000192
b   the max memory is 129957888
                array 60000000
a       freememory is 24300472
a the total memory is 85000192
a   the max memory is 129957888

                 i is 7
              size is 70000000
b       freememory is 84300496
b the total memory is 85000192
b   the max memory is 129957888
                array 70000000
a       freememory is 59258168
a the total memory is 129957888
a   the max memory is 129957888

6 , 60M 24M. 7 . ( totalMemory), freeMemory 60M.

0

- , , . Eclipse, , TPTP, , IDE .

0

, . JVM , ""; , , "eden" , GC, . ( JVM), , " " 8 .

, 55% , " ". OOME, , , ... GC.

, JVM , . , Java. ( ), .

0

spong quesiton.

If instead of creating an array in one cartridge, you split its creation into many smaller arrays, you can fill up free memory up to a factor of about 0.98.

     Runtime runtime = Runtime.getRuntime();
     long freeMemory = runtime.freeMemory();

     // This is some arbitary factor that should work <= 1
     double factor = 0.8;

     int size = (int) (factor * freeMemory);


     int slice = 100;
     byte[][] testArrays = new byte[slice][1];
     for (int i = 1; i <= slice; i++) {
            testArrays[i-1] = new byte[size / slice];
            System.out.println("Allocated: " + i * size / slice);
     }

     System.out.println("Created! "+testArrays.length);
0
source

Source: https://habr.com/ru/post/1766117/


All Articles