2010-08-26 14 views

Respuesta

140

De acuerdo con la API

totalMemory() 

Devuelve la cantidad total de memoria en la máquina virtual de Java. El valor devuelto por este método puede variar con el tiempo, dependiendo del entorno del host. Tenga en cuenta que la cantidad de memoria requerida para contener un objeto de cualquier tipo dado puede depender de la implementación.

maxMemory() 

Devuelve la cantidad máxima de memoria que la máquina virtual Java intentará utilizar. Si no hay un límite inherente, se devolverá el valor Long.MAX_VALUE.

freeMemory() 

Devuelve la cantidad de memoria libre en la máquina virtual de Java. Llamar al método gc puede aumentar el valor devuelto por freeMemory.

En referencia a su pregunta, maxMemory() devuelve el valor -Xmx.

Usted puede preguntarse por qué hay una totalMemory() y una MaxMemory(). La respuesta es que la JVM asigna memoria de forma perezosa. Digamos que se inicia el proceso de Java como por ejemplo:

java -Xms64m -Xmx1024m Foo 

Su proceso se inicia con 64 MB de memoria, y siempre y cuando se necesita más (hasta 1024m), se asignará memoria. totalMemory() corresponde a la cantidad de memoria actualmente disponible para la JVM para Foo. Si la JVM necesita más memoria, la asignará perezosamente hasta a la memoria máxima. Si ejecuta con -Xms1024m -Xmx1024m, el valor que obtenga de totalMemory() y maxMemory() será igual.

Además, si se desea calcular con precisión la cantidad de utiliza la memoria, lo hace con el siguiente cálculo:

final long usedMem = totalMemory() - freeMemory(); 
+0

El ' -El valor de Xmx parece afectar directamente el valor inicial de 'maxMemory()', sin embargo, tengo visto el aumento 'máxMemory()' reportado por una pequeña cantidad, quizás ~ 1%, mientras el programa se está ejecutando. – H2ONaCl

+2

¿En qué se diferencia esto de 'Debug.getNativeHeapFreeSize()'? –

+0

@ H2ONaCl sí, podría cambiar ligeramente, porque 'UseAdaptiveSizePolicy' de JVM está habilitado de forma predeterminada. Y por cierto: 'maxMemory()' = 'Xmx' - tamaño de un único espacio de superviviente. ¿Por qué? Porque al mismo tiempo, solo se puede usar un espacio de sobreviviente. –

5

Runtime#totalMemory - la memoria que la JVM ha asignado hasta el momento. Esto no es necesariamente lo que está en uso o el máximo.

Runtime#maxMemory - la cantidad máxima de memoria que la JVM ha sido configurada para usar. Una vez que su proceso alcance esta cantidad, la JVM no asignará más y, en cambio, GC mucho más frecuentemente.

Runtime#freeMemory - No estoy seguro de si esto se mide desde el máximo o la parte del total que no se utiliza. Supongo que es una medida de la porción del total que no se usa.

4

El tamaño del almacenamiento dinámico de JVM puede ser cultivable y contraíble por el mecanismo de recolección de basura. Pero no se puede asignar a un tamaño de memoria máximo: Runtime.maxMemory. Este es el significado de la memoria máxima. La memoria total significa el tamaño de almacenamiento dinámico asignado. Y memoria libre significa el tamaño disponible en la memoria total.

ejemplo) java -Xms20M -Xmn10M -Xmx50M ~~~. Esto significa que jvm debe asignar el montón 20M al inicio (ms). En este caso, la memoria total es 20M. la memoria libre es de 20M de tamaño utilizado. Si se necesita más almacenamiento dinámico, JVM asigna más pero no puede superar los 50M (mx). En el caso del máximo, la memoria total es de 50M, y el tamaño libre es el tamaño de 50M utilizado. En cuanto al tamaño mínimo (mn), si el montón no se usa mucho, jvm puede reducir el tamaño del montón a 10M.

Este mecanismo es para la eficiencia de la memoria. Si el programa Java pequeño se ejecuta en una gran memoria de montón de tamaño fijo, tanta memoria puede ser un desperdicio.

9

Para entenderlo mejor, ejecute este programa siguiente (en jdk1.7.x):

$ java -Xms1025k -Xmx1025k -XshowSettings:vm MemoryTest 

Esto imprimirá opciones de JVM y la utiliza, libre, total y memoria máxima disponible en jvm.

public class MemoryTest {  
    public static void main(String args[]) { 
       System.out.println("Used Memory : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) + " bytes"); 
       System.out.println("Free Memory : " + Runtime.getRuntime().freeMemory() + " bytes"); 
       System.out.println("Total Memory : " + Runtime.getRuntime().totalMemory() + " bytes"); 
       System.out.println("Max Memory : " + Runtime.getRuntime().maxMemory() + " bytes");    
     } 
} 
171

Los nombres y los valores son confusos. Si está buscando total free memory, tendrá que calcular este valor usted mismo. No es lo que obtienes de freeMemory();.

Ver la siguiente guía:

total de memoria designada, este será igual a la configurada -Xmx valor:

Runtime.getRuntime() MaxMemory();.

actual liberar memoria asignada, es el espacio asignado actual listos para los nuevos objetos. . Precaución este no es el total de memoria libre disponible :

Runtime.getRuntime() FreeMemory();

memoria total asignada, es el espacio total asignado reservado para el proceso de java:

Runtime.getRuntime().memoria total();

memoria Utilizado, tienen que ser calculados:..

usedMemory = Runtime.getRuntime() totalMemory() - Runtime.getRuntime() FreeMemory();

memoria libre total, tienen que ser calculados:.

FreeMemory = Runtime.getRuntime() MaxMemory() - usedMemory;

Una imagen puede ayudar a aclarar:

java runtime memory

+1

¿Es esto diferente de 'Debug.getMemoryInfo()'? –

+0

Nota: ** La memoria usada ** puede ** contener objetos a los que ya no hace referencia ** que serán barridos por el siguiente GC. –

+0

@cheneym respuesta increíble! Has inspirado http://stackoverflow.com/a/42567450/253468 – TWiStErRob

2

versión codificada de todas las otras respuestas (en el momento de la escritura):

import java.io.*; 

/** 
* This class is based on <a href="http://stackoverflow.com/users/2478930/cheneym">cheneym</a>'s 
* <a href="http://stackoverflow.com/a/18375641/253468">awesome interpretation</a> 
* of the Java {@link Runtime}'s memory query methods, which reflects intuitive thinking. 
* Also includes comments and observations from others on the same question, and my own experience. 
* <p> 
* <img src="https://i.stack.imgur.com/GjuwM.png" alt="Runtime's memory interpretation"> 
* <p> 
* <b>JVM memory management crash course</b>: 
* Java virtual machine process' heap size is bounded by the maximum memory allowed. 
* The startup and maximum size can be configured by JVM arguments. 
* JVMs don't allocate the maximum memory on startup as the program running may never require that. 
* This is to be a good player and not waste system resources unnecessarily. 
* Instead they allocate some memory and then grow when new allocations require it. 
* The garbage collector will be run at times to clean up unused objects to prevent this growing. 
* Many parameters of this management such as when to grow/shrink or which GC to use 
* can be tuned via advanced configuration parameters on JVM startup. 
* 
* @see <a href="http://stackoverflow.com/a/42567450/253468"> 
*  What are Runtime.getRuntime().totalMemory() and freeMemory()?</a> 
* @see <a href="http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf"> 
*  Memory Management in the Sun Java HotSpot™ Virtual Machine</a> 
* @see <a href="http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html"> 
*  Full VM options reference for Windows</a> 
* @see <a href="http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html"> 
*  Full VM options reference for Linux, Mac OS X and Solaris</a> 
* @see <a href="http://www.oracle.com/technetwork/articles/java/vmoptions-jsp-140102.html"> 
*  Java HotSpot VM Options quick reference</a> 
*/ 
public class SystemMemory { 

    // can be white-box mocked for testing 
    private final Runtime runtime = Runtime.getRuntime(); 

    /** 
    * <b>Total allocated memory</b>: space currently reserved for the JVM heap within the process. 
    * <p> 
    * <i>Caution</i>: this is not the total memory, the JVM may grow the heap for new allocations. 
    */ 
    public long getAllocatedTotal() { 
     return runtime.totalMemory(); 
    } 

    /** 
    * <b>Current allocated free memory</b>: space immediately ready for new objects. 
    * <p> 
    * <i>Caution</i>: this is not the total free available memory, 
    * the JVM may grow the heap for new allocations. 
    */ 
    public long getAllocatedFree() { 
     return runtime.freeMemory(); 
    } 

    /** 
    * <b>Used memory</b>: 
    * Java heap currently used by instantiated objects. 
    * <p> 
    * <i>Caution</i>: May include no longer referenced objects, soft references, etc. 
    * that will be swept away by the next garbage collection. 
    */ 
    public long getUsed() { 
     return getAllocatedTotal() - getAllocatedFree(); 
    } 

    /** 
    * <b>Maximum allocation</b>: the process' allocated memory will not grow any further. 
    * <p> 
    * <i>Caution</i>: This may change over time, do not cache it! 
    * There are some JVMs/garbage collectors that can shrink the allocated process memory. 
    * <p> 
    * <i>Caution</i>: If this is true, the JVM will likely run GC more often. 
    */ 
    public boolean isAtMaximumAllocation() { 
     return getAllocatedTotal() == getTotal(); 
     // = return getUnallocated() == 0; 
    } 

    /** 
    * <b>Unallocated memory</b>: amount of space the process' heap can grow. 
    */ 
    public long getUnallocated() { 
     return getTotal() - getAllocatedTotal(); 
    } 

    /** 
    * <b>Total designated memory</b>: this will equal the configured {@code -Xmx} value. 
    * <p> 
    * <i>Caution</i>: You can never allocate more memory than this, unless you use native code. 
    */ 
    public long getTotal() { 
     return runtime.maxMemory(); 
    } 

    /** 
    * <b>Total free memory</b>: memory available for new Objects, 
    * even at the cost of growing the allocated memory of the process. 
    */ 
    public long getFree() { 
     return getTotal() - getUsed(); 
     // = return getAllocatedFree() + getUnallocated(); 
    } 

    /** 
    * <b>Unbounded memory</b>: there is no inherent limit on free memory. 
    */ 
    public boolean isBounded() { 
     return getTotal() != Long.MAX_VALUE; 
    } 

    /** 
    * Dump of the current state for debugging or understanding the memory divisions. 
    * <p> 
    * <i>Caution</i>: Numbers may not match up exactly as state may change during the call. 
    */ 
    public String getCurrentStats() { 
     StringWriter backing = new StringWriter(); 
     PrintWriter out = new PrintWriter(backing, false); 
     out.printf("Total: allocated %,d (%.1f%%) out of possible %,d; %s, %s %,d%n", 
       getAllocatedTotal(), 
       (float)getAllocatedTotal()/(float)getTotal() * 100, 
       getTotal(), 
       isBounded()? "bounded" : "unbounded", 
       isAtMaximumAllocation()? "maxed out" : "can grow", 
       getUnallocated() 
     ); 
     out.printf("Used: %,d; %.1f%% of total (%,d); %.1f%% of allocated (%,d)%n", 
       getUsed(), 
       (float)getUsed()/(float)getTotal() * 100, 
       getTotal(), 
       (float)getUsed()/(float)getAllocatedTotal() * 100, 
       getAllocatedTotal() 
     ); 
     out.printf("Free: %,d (%.1f%%) out of %,d total; %,d (%.1f%%) out of %,d allocated%n", 
       getFree(), 
       (float)getFree()/(float)getTotal() * 100, 
       getTotal(), 
       getAllocatedFree(), 
       (float)getAllocatedFree()/(float)getAllocatedTotal() * 100, 
       getAllocatedTotal() 
     ); 
     out.flush(); 
     return backing.toString(); 
    } 

    public static void main(String... args) { 
     SystemMemory memory = new SystemMemory(); 
     System.out.println(memory.getCurrentStats()); 
    } 
} 
Cuestiones relacionadas