esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I get the current memory usage of a Process in windows with Java?
You will have to use JNI for this. Using the following code, we will create a DLL that provides the method getProcessWorkingSet which will return the current working set size, and the method getProcessVMSize which will return the current pagefile usage. The working set size is the memory size as shown in the Windows Task Manager. The pagefile usage corresponds with the VM Size in the Windows Task Manager.

From MSDN:
Working set size: The working set is the amount of physical memory assigned to a process. If the working set is too small, the process incurs a high number of page faults as it repeatedly accesses the disk drive to locate data not currently in memory. If the working set is too large, fewer page faults occur, but the process retains memory that it no longer needs, and which might be required by other processes. A steady increase in the size of the working set can mean that the process is not releasing memory appropriately.

MemUsage.java:

This code sample is only viewable to esus.com members
Login or become a member!


Compile it and run javah on it to create the memusage.h header file:

 
This code sample is only viewable to esus.com members
Login or become a member!


It generates the C header file memusage.h:

This code sample is only viewable to esus.com members
Login or become a member!


The implementation of these functions were put in memusage.c:

This code sample is only viewable to esus.com members
Login or become a member!


I used cygwin to develop a Windows DLL from this in the following way:

 
This code sample is only viewable to esus.com members
Login or become a member!


This DLL now allowed me to get the current memory usage and page file usage on Windows. Here's an example:

Main.java:

This code sample is only viewable to esus.com members
Login or become a member!


Sample run:

 
This code sample is only viewable to esus.com members
Login or become a member!


For those who don't have cygwin installed or just want to start using this library right away, here it is:

MemUsage.dll


Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.