Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Ram size and size of Hard disk using Java?

Tags:

java

How to get the Ram size and Hard disk size of the PC using Java? And Is it possible to get the currently logged user name on PC through java?

like image 807
bharath Avatar asked Apr 01 '11 10:04

bharath


3 Answers

Disk size:

long diskSize = new File("/").getTotalSpace();

User name:

String userName = System.getProperty("user.name");

I'm not aware of a reliable way to determine total system memory in Java. On a Unix system you could parse /proc/meminfo. You can of course find the maximum memory available to the JVM:

long maxMemory = Runtime.getRuntime().maxMemory();

Edit: for completeness (thanks Suresh S), here's a way to get total memory with the Oracle JVM only:

long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
like image 194
WhiteFang34 Avatar answered Nov 18 '22 05:11

WhiteFang34


For Ram Size , if you are using java 1.5

java.lang.management package

com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println(mxbean.getTotalPhysicalMemorySize() + " Bytes "); 
like image 7
Dead Programmer Avatar answered Nov 18 '22 06:11

Dead Programmer


import java.lang.management.*;
import java.io.*;

class max
{
    public static void main(String... a)
    {
        long diskSize = new File("/").getTotalSpace();
        String userName = System.getProperty("user.name");
        long maxMemory = Runtime.getRuntime().maxMemory();
        long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
        System.out.println("Size of C:="+diskSize+" Bytes");
        System.out.println("User Name="+userName);

        System.out.println("RAM Size="+memorySize+" Bytes");
   }
}
like image 5
Atin Agarwal Avatar answered Nov 18 '22 05:11

Atin Agarwal