How do I find the physical memory size in Java?

前端 未结 7 843
心在旅途
心在旅途 2021-01-07 22:02

I\'m writing an installer that will tune the configuration of the product for the particular hardware on which it will be run. In particular, I want to determine how much p

相关标签:
7条回答
  • 2021-01-07 22:30

    If your intent is to tune memory settings for the JVM to use all the available physical memory, but not more, then you can take a look at the -XX:+AggressiveHeap parameter.

    With it, you don't need to know the available memory. The JVM will scale it's parameters automatically.

    0 讨论(0)
  • 2021-01-07 22:32

    What about local JMX and MBeans? Try this:

    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
    System.out.println("Total memory: "+ attribute.toString() +" B");
    

    You can access many usefull info this way.

    0 讨论(0)
  • 2021-01-07 22:43
    import java.lang.management.ManagementFactory;
    
    import javax.management.AttributeNotFoundException;
    import javax.management.InstanceNotFoundException;
    import javax.management.MBeanException;
    import javax.management.MBeanServer;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.ReflectionException;
    
    public class Main {
      public static void main(String[] args) throws InstanceNotFoundException, AttributeNotFoundException, MalformedObjectNameException, ReflectionException, MBeanException  {
      /* Total number of processors or cores available to the JVM */
    
    
          MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
          Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
          Object attribute2 = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize");
          System.out.println("Total memory: "+ Long.parseLong(attribute.toString()) / 1024  +"MB");
          System.out.println("Free  memory: "+ Long.parseLong(attribute2.toString()) / 1024  +"MB");    
    
     }
    }
    
    0 讨论(0)
  • 2021-01-07 22:46

    Have came across this question several times for the past few years. Most of the existing answers are hacking and platform dependent, so, there isn't any self-contained, platform independent tool-set out there for JAVA today?

    Actually, there is one: https://github.com/oshi/oshi

    OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It doesn't require the installation of any additional native libraries and aims to provide a cross-platform implementation to retrieve system information, such as OS version, processes, memory & CPU usage, disks & partitions, devices, sensors, etc.

    Yes, it did use JNI internally, as other methods do, but who cares about it if all the details are carefully hidden behind a clean API and the package are quite easy to install if you are using any decent, modern dependency management tools(maven, gradle, etc).

    For any serious project, use this one. Don't reinvent the wheel.

    0 讨论(0)
  • 2021-01-07 22:47

    Under linux you can use sysinfo(2). I don't think it's actually possible from pure java.

    0 讨论(0)
  • 2021-01-07 22:48

    You can use the following Java code to query the physical memory:

    com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)
         java.lang.management.ManagementFactory.getOperatingSystemMXBean();
    long physicalMemorySize = os.getTotalPhysicalMemorySize();
    

    But the package com.sun.management is optional and need not be available on every platform.

    0 讨论(0)
提交回复
热议问题