[ACCEPTED]-How to get Ram size and size of Hard disk using Java?-java

Accepted answer
Score: 28

Disk size:

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

User name:

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

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

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

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

long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
Score: 7

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 "); 


Score: 5
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");


Score: 3

Have a look at this topic, which goes into detail 1 of how to get OS information such as this.

Score: 0

For Ram capacity: //this step get ram capacity

long 1 ram= ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();

            long sizekb = ram /1000;
    long sizemb = sizekb / 1000;
    long sizegb = sizemb / 1000 ;
    System.out.println("System Ram ="+sizegb+"gb");

More Related questions