问题
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 physical RAM is installed in the system so I can estimate how much memory to allocate to the product when it runs.
Ideally, I'd like to do this in a platform-independent, pure Java way, as the installer will need to run on several different platforms, but in case this isn't possible, solutions for Windows are preferred as that's the most common deployment platform.
In this case, it's safe to assume that the product will be the only/main application running on the box, so I don't have to worry about squeezing anyone else out. I don't want to over-allocate as this, in our experience, can hurt performance.
回答1:
For windows, you'll need to access WMI - this will get you going: Accessing Windows Management Instrumentation (WMI) from Java.
You'll want to use this section of WMI: Win32_LogicalMemoryConfiguration.
There may be a pure java way of doing this, but I am unaware of it.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
Under linux you can use sysinfo(2). I don't think it's actually possible from pure java.
回答6:
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.
回答7:
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");
}
}
来源:https://stackoverflow.com/questions/950754/how-do-i-find-the-physical-memory-size-in-java