2010-01-06 10 views

Répondre

128

Utilisez ce code:

// Get current size of heap in bytes 
long heapSize = Runtime.getRuntime().totalMemory(); 

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException. 
long heapMaxSize = Runtime.getRuntime().maxMemory(); 

// Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created. 
long heapFreeSize = Runtime.getRuntime().freeMemory(); 

Il m'a été utile de le savoir.

+0

Vous devrez recompiler votre application (NetBeans) si vous utilisez cette stratégie. – vkraemer

+0

Selon doc: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#freeMemory(). Les méthodes mentionnées ci-dessus renvoient de la mémoire dans la machine virtuelle Java pas nécessairement dans la mémoire de tas –

+0

Notez que toute la taille de segment que vous spécifiez avec '-J-Xmx 1g' n'est pas nécessairement disponible pour les objets de votre application. La machine virtuelle, en particulier le garbage collector, peut utiliser une partie de cette mémoire. [Voici] (https://plumbr.io/blog/memory-leaks/less-memory-than-xmx) un peu plus sur le sujet. –

8

Vous pouvez utiliser jconsole (standard avec la plupart des JDK) pour vérifier les tailles de segments de n'importe quel processus java.

+0

Cela inclut des graphiques en temps réel d'utilisation de la mémoire, des fils et des classes. très utile. – vkraemer

2

Joindre avec jvisualvm à partir de Sun Java 6 JDK. Les indicateurs de démarrage sont répertoriés.

+0

visualvm ... non j – vkraemer

+2

le binaire est nommé jvisualvm (.exe). Aucune idée pourquoi. –

0

Pour vérifier vos outils d'application java taille de la pile et le temps CPU utilisation JProfiler qui peuvent accéder à votre machine virtuelle Java et afficher tous les tas et la répartition du temps CPU

plug-in disponible pour Eclipse et intellej Idée IDE

Link pour Télécharger JProfiler

http://www.ej-technologies.com/download/jprofiler/files

0
public class CheckHeapSize { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     long heapSize = Runtime.getRuntime().totalMemory(); 

     // Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException. 
     long heapMaxSize = Runtime.getRuntime().maxMemory(); 

     // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created. 
     long heapFreeSize = Runtime.getRuntime().freeMemory(); 

     System.out.println("heapsize"+formatSize(heapSize)); 
     System.out.println("heapmaxsize"+formatSize(heapMaxSize)); 
     System.out.println("heapFreesize"+formatSize(heapFreeSize)); 

    } 
    public static String formatSize(long v) { 
     if (v < 1024) return v + " B"; 
     int z = (63 - Long.numberOfLeadingZeros(v))/10; 
     return String.format("%.1f %sB", (double)v/(1L << (z*10)), " KMGTPE".charAt(z)); 
    } 
} 
+0

Utilisez ce code pour obtenir HeapSize de la mémoire au format lisible par l'utilisateur –

+0

1 kilo-octet (1 Ko) = 1000 octets. 1 kibibyte (1 KiB) = 1024 octets. – bcody

0

préféré quand jvisualvm est surpuissant ou vous avez besoin cli seule: jvmtop

JvmTop 0.8.0 alpha amd64 8 cpus, Linux 2.6.32-27, load avg 0.12 
https://github.com/patric-r/jvmtop 

PID MAIN-CLASS  HPCUR HPMAX NHCUR NHMAX CPU  GC VM USERNAME #T DL 
3370 rapperSimpleApp 165m 455m 109m 176m 0.12% 0.00% S6U37 web  21 
11272 ver.resin.Resin [ERROR: Could not attach to VM] 
27338 WatchdogManager 11m 28m 23m 130m 0.00% 0.00% S6U37 web  31 
19187 m.jvmtop.JvmTop 20m 3544m 13m 130m 0.93% 0.47% S6U37 web  20 
16733 artup.Bootstrap 159m 455m 166m 304m 0.12% 0.00% S6U37 web  46