Por cortesía de Miguel Garvia ahí va una clase que permite de una forma muy sencilla monitorizar la carga (memoria, CPU,…) de una aplicación Java.
La clase (para copiar y pegar ;)):
| package test;
import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; public class PerformanceMonitor { private int availableProcessors = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors(); private long lastSystemTime = 0; private long lastProcessCpuTime = 0; public PerformanceMonitor() { // Inicializar la estadistica getCpuUsage(); } public synchronized String getStats() { java.lang.management.OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); com.sun.management.OperatingSystemMXBean osDetail = null; if ( os instanceof com.sun.management.OperatingSystemMXBean ) { osDetail = (com.sun.management.OperatingSystemMXBean)os; } StringBuffer stats = new StringBuffer(); stats.append( «operating system architecture: » + osDetail.getArch() + «\n» ); stats.append( «processors available to the Java virtual machine: » + osDetail.getAvailableProcessors() + «\n» ); stats.append( «virtual memory guaranteed to the running process in Mb: » + osDetail.getCommittedVirtualMemorySize() / (1024 * 1024) + «\n» ); stats.append( «amount of free physical memory in Mb: » + osDetail.getFreePhysicalMemorySize() / (1024 * 1024) + «\n» ); stats.append( «amount of free swap space in bytes: » + osDetail.getFreeSwapSpaceSize()/ (1024 * 1024) + «\n» ); stats.append( «operating system name: » + osDetail.getName() + «\n» ); stats.append( «jvm process CPU time in seconds: » + osDetail.getProcessCpuTime() / (1000 * 1000) + «\n» ); stats.append( «system load average for the last minute: » + osDetail.getSystemLoadAverage() + «\n» ); stats.append( «total amount of physical memory in Mb: » + osDetail.getTotalPhysicalMemorySize()/ (1024 * 1024) + «\n» ); stats.append( «total amount of swap space in Mb: » + osDetail.getTotalSwapSpaceSize()/ (1024 * 1024) + «\n» ); stats.append( «operating system version: » + osDetail.getVersion() + «\n» ); stats.append( «Total Heap Memory bytes: » + Runtime.getRuntime().totalMemory() + «\n» ); stats.append( «Free Heap Memory bytes: » + Runtime.getRuntime().freeMemory() + «\n» ); return stats.toString(); } public synchronized long getHeapSpace() { Runtime runtime = Runtime.getRuntime(); return runtime.maxMemory(); } public synchronized long getFreeHeap() { Runtime runtime = Runtime.getRuntime(); return runtime.freeMemory() + runtime.maxMemory() – runtime.totalMemory(); } public synchronized double getCpuUsage() { if ( lastSystemTime == 0 ) { baselineCounters(); return -1.0; } long systemTime = System.nanoTime(); long processCpuTime = 0; if ( ManagementFactory.getOperatingSystemMXBean() instanceof OperatingSystemMXBean ) { processCpuTime = ( (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean() ).getProcessCpuTime(); } double cpuUsage = (double) ( processCpuTime – lastProcessCpuTime ) / ( systemTime – lastSystemTime ); lastSystemTime = systemTime; lastProcessCpuTime = processCpuTime; return cpuUsage / availableProcessors; } private void baselineCounters() { lastSystemTime = System.nanoTime(); if ( ManagementFactory.getOperatingSystemMXBean() instanceof OperatingSystemMXBean ) { lastProcessCpuTime = ( (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean() ).getProcessCpuTime(); } } public static void main( String[] args ) throws Exception { PerformanceMonitor performanceMonitor = new PerformanceMonitor(); List<Calendar> list = new ArrayList<Calendar>(); for ( int i = 0; i < 1000000000; i++ ) { // Petar la lista con 1000 calendars for ( int k = 0; k < 10000; k++ ){ list.add( new GregorianCalendar() ); } // Petar la CPU con ciclos vacios for ( int j = 0; j< 1000000000; j++ ) {} // Mem en Mb System.out.println( «CPU: » + performanceMonitor.getCpuUsage() + «\t\tMem: » + performanceMonitor.getFreeHeap() / (1024d*1024d) + » (» + performanceMonitor.getHeapSpace() / (1024d*1024d) + «)» ); } } } |
Podríamos lanzar la clase en un Hilo de Baja Prioridad para ir monitorizando la aplicación.
La traza que saca:
| operating system architecture: x86
processors available to the Java virtual machine: 2 virtual memory guaranteed to the running process in Mb: 26 amount of free physical memory in Mb: 1398 amount of free swap space in bytes: 2414 operating system name: Windows XP jvm process CPU time in seconds: 31 system load average for the last minute: -1.0 total amount of physical memory in Mb: 2047 total amount of swap space in Mb: 4095 operating system version: 5.1 CPU: 0.53092372493373 Mem: 59.24299621582031 (63.5625) CPU: 0.49657241013084563 Mem: 54.95256805419922 (63.5625) CPU: 0.4942800629113504 Mem: 50.687034606933594 (63.5625) CPU: 0.5014447636314036 Mem: 46.33628845214844 (63.5625) CPU: 0.49340744417460447 Mem: 42.19970703125 (63.5625) CPU: 0.47895371827853395 Mem: 38.14019775390625 (63.5625) CPU: 0.489600379360572 Mem: 34.019569396972656 (63.5625) CPU: 0.5035100827089479 Mem: 29.867881774902344 (63.5625) CPU: 0.4956995526839775 Mem: 25.292449951171875 (63.5625) CPU: 0.5094588912754633 Mem: 21.131568908691406 (63.5625) CPU: 0.4906238625778933 Mem: 17.017196655273438 (63.5625) CPU: 0.5065215722196845 Mem: 12.91436767578125 (63.5625) CPU: 0.4985452957553055 Mem: 8.817756652832031 (63.5625) CPU: 0.4914929960487762 Mem: 3.883514404296875 (63.5625) CPU: 0.4986814075082175 Mem: 0.4375 (63.5625) Exception in thread «main» java.lang.OutOfMemoryError: Java heap space at java.util.Calendar.<init>(Unknown Source) at java.util.GregorianCalendar.<init>(Unknown Source) at java.util.GregorianCalendar.<init>(Unknown Source) at test.PerformanceMonitor.main(PerformanceMonitor.java:92) |

Deja un comentario