• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding physical memory  RSS feed

 
Vasudhaiv Naresh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i need to develop an application which requires to monitor the physical memory(note its not the JVM but RAM).
Can anybody let me know how i can get it done?
Thanks.
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem requires direct access to the hardware and sad to say if you are using Java, it does not have this capabilities. Another solution is to write it in C/C++ then use JNI to wrap this method and call it in your Java classes.

arnel
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks!

You can learn about JNI here.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use Linux, you can get this information and much more from the proc virtual file system as if you were reading text files.
Other operating systems are not as transparent.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are willing to do a runtime exec, there are also programs which return the information.

For Unix, you can just call the "vmstat" program. For Windows, you can call the "typeperf" program. The "typeperf" program is much more difficult to use, but once you figured out the counter that you need, it is much more easy and flexible. For example, to get free memory...

C:\> typeperf "\memory\available bytes"

Henry
 
Vasudhaiv Naresh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Henry!
thanks for the information...
I'm glad to say that the Unix command "vmstat" is working,
but its Windows counterpart "typeperf" isn't working.
My system doesn't recognize it as an internal command(I'mm using Win2000 Professional). Is there any setting I've to make for the same?

Thanks,
Naresh
 
Arjun K
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JDK1.5 which packages java.management packages and helps in getting the system info.

<code>
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import com.sun.management.*;

public class Info {


public static void main(String[] args) {


OperatingSystemMXBean osMBean =
(OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

RuntimeMXBean runtimeMBean = ManagementFactory.getRuntimeMXBean();


System.out.println("Operating system:\t" + osMBean.getName());
System.out.println("Architecture:\t\t" + osMBean.getArch());
System.out.println("Number of processors:\t" + osMBean.getAvailableProcessors());
System.out.println("Process CPU time:\t" + osMBean.getProcessCpuTime() );
System.out.println("Total physical memory:\t" + osMBean.getTotalPhysicalMemorySize()/1024 +" kB");
System.out.println("Free physical memory:\t" + osMBean.getFreePhysicalMemorySize()/1024 +" kB" );
System.out.println("Comm. virtual memory:\t" + osMBean.getProcessCpuTime()/1024 +" kB" );

System.out.println("Total swap space:\t" + osMBean.getTotalSwapSpaceSize()/1024 +" kB" );
System.out.println("Free swap space:\t" + osMBean.getFreeSwapSpaceSize() /1024 +" kB" );

}

}

</code>

Hope this helps,
Arjun K
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!