如何在 Java 中找到物理内存大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/950754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:32:04  来源:igfitidea点击:

How do I find the physical memory size in Java?

javamemoryinstallermemory-management

提问by Martin McNulty

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.

我正在编写一个安装程序,它将针对将在其上运行的特定硬件调整产品的配置。特别是,我想确定系统中安装了多少物理 RAM,以便我可以估计在产品运行时为产品分配多少内存。

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.

理想情况下,我想以独立于平台的纯 Java 方式执行此操作,因为安装程序需要在多个不同平台上运行,但如果这是不可能的,则首选 Windows 解决方案,因为这是最常见的部署平台。

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.

在这种情况下,可以安全地假设该产品将是盒子上运行的唯一/主要应用程序,因此我不必担心挤掉其他任何人。我不想过度分配,因为根据我们的经验,这会损害性能。

采纳答案by Nate

For windows, you'll need to access WMI - this will get you going: Accessing Windows Management Instrumentation (WMI) from Java.

对于 Windows,您需要访问 WMI - 这将帮助您前进:从 Java 访问 Windows Management Instrumentation (WMI)

You'll want to use this section of WMI: Win32_LogicalMemoryConfiguration.

您将需要使用 WMI 的这一部分:Win32_LogicalMemoryConfiguration。

There may be a pure java way of doing this, but I am unaware of it.

可能有一种纯 Java 方式可以做到这一点,但我不知道。

回答by Horcrux7

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

您可以使用以下 Java 代码查询物理内存:

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

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

但该软件包com.sun.management是可选的,不需要在每个平台上都可用。

回答by mombip

What about local JMX and MBeans? Try this:

本地 JMX 和 MBean 怎么样?试试这个:

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.

您可以通过这种方式访问​​许多有用的信息。

回答by Laurent Simon

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.

如果您的目的是调整 JVM 的内存设置以使用所有可用的物理内存,但不是更多,那么您可以查看 -XX:+AggressiveHeap 参数。

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

有了它,您就无需知道可用内存。JVM 会自动调整它的参数。

回答by Nadir Fouka

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

 }
}

回答by wds

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

在 linux 下,您可以使用sysinfo(2)。我认为这实际上不可能来自纯 Java。

回答by Jerry Tian

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?

在过去的几年里,多次遇到过这个问题。大多数现有的答案都是黑客攻击和平台相关的,所以,今天的 JAVA 没有任何独立的、独立于平台的工具集吗?

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

其实有一个: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.

OSHI 是一个免费的基于 JNA(本机)的 Java 操作系统和硬件信息库。它不需要安装任何额外的本地库,旨在提供跨平台实现来检索系统信息,例如操作系统版本、进程、内存和 CPU 使用率、磁盘和分区、设备、传感器等。

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).

是的,它确实在内部使用了 JNI,就像其他方法一样,但是如果所有的细节都被小心地隐藏在一个干净的 API 后面,而且如果你使用任何像样的现代依赖管理工具(maven),这个包很容易安装,谁会关心它、gradle 等)。

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

对于任何严肃的项目,请使用这个。不要重新发明轮子。