September 04, 2010, 12:36:30 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: CPU and memory usage statistics in Java application  (Read 1278 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: August 09, 2008, 11:49:39 AM »

For those that want to know how to see the CPU and memory usage statistics in a Java application here's a demo that P. Lawrey wrote  Very nice

Code
GeSHi (java):
  1. import java.lang.management.ManagementFactory;
  2. import java.lang.management.OperatingSystemMXBean;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Modifier;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.GregorianCalendar;
  8. import java.util.List;
  9.  
  10. public class OSBeanTester {
  11.    public static void main(String... args) {
  12.        printUsage();
  13.        System.out.println("Calendars are surprisingly expensive. Creating 100000 Calendars");
  14.        List<Calendar> calendars = new ArrayList<Calendar>();
  15.        for (int i = 0; i < 100000; i++)
  16.            calendars.add(new GregorianCalendar());
  17.        printUsage();
  18.    }
  19.  
  20.    private static void printUsage() {
  21.        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
  22.        for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
  23.            method.setAccessible(true);
  24.            if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
  25.                Object value;
  26.                try {
  27.                    value = method.invoke(operatingSystemMXBean);
  28.                } catch (Exception e) {
  29.                    value = e;
  30.                }
  31.                System.out.println(method.getName() + " = " + value);
  32.            }
  33.        }
  34.    }
  35. }
  36.  
Created by GeSHI 1.0.7.20

Output
getCommittedVirtualMemorySize = 28811264
getFreePhysicalMemorySize = 1207009280
getFreeSwapSpaceSize = 3365294080
getProcessCpuTime = 109375000
getTotalPhysicalMemorySize = 2145497088
getTotalSwapSpaceSize = 4129447936
Calendars are surprisingly expensive. Creating 100000 Calendars
getCommittedVirtualMemorySize = 91783168
getFreePhysicalMemorySize = 1154912256
getFreeSwapSpaceSize = 3302273024
getProcessCpuTime = 671875000
getTotalPhysicalMemorySize = 2145497088
getTotalSwapSpaceSize = 4129447936
« Last Edit: August 09, 2008, 11:55:24 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: August 09, 2008, 11:49:39 AM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.23 seconds with 32 queries.

Google visited last this page September 02, 2010, 05:49:16 AM