首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OSHI库计算进程(作业)占用的内存

使用OSHI库计算进程(作业)占用的内存
EN

Stack Overflow用户
提问于 2019-04-30 01:23:27
回答 2查看 527关注 0票数 0

在我的应用程序中,我从另一个应用程序获得进程占用的估计内存。但我希望获得处理器运行所需的确切内存。

当我在网上搜索如何获得进程所需的正确内存时,我发现oshi lib可以做到这一点。但是我没有找到实现解决方案的方法。有谁能帮帮我吗?

OSHI库:https://github.com/oshi/oshi

仅供参考:我们使用OSHI lib来获取systemInfo、硬件、操作系统、centralProcessor和全局内存。下面是代码片段。

代码语言:javascript
复制
    oshi.SystemInfo systemInfo = new oshi.SystemInfo();
    this.hal = systemInfo.getHardware();
    this.os = systemInfo.getOperatingSystem();
    this.centralProcessor = this.hal.getProcessor();
    this.globalMemory = this.hal.getMemory();
EN

回答 2

Stack Overflow用户

发布于 2019-04-30 01:29:23

也许可以从OSProcess类中检索内存使用情况:

代码语言:javascript
复制
OSProcess process =  new SystemInfo().getHardware().getOperatingSystem().getProcess(myPid);
process.getVirtualSize();
process.getResidentSetSize();
票数 1
EN

Stack Overflow用户

发布于 2020-09-24 20:27:58

代码语言:javascript
复制
public static void memoryUtilizationPerProcess(int pid) {
        /**
         * Resident Size : how much memory is allocated to that process and is in RAM
         */
        OSProcess process;
        SystemInfo si = new SystemInfo();
        OperatingSystem os = si.getOperatingSystem();
        process = os.getProcess(pid);
        oshi.hardware.GlobalMemory globalMemory = si.getHardware().getMemory();
        long usedRamProcess = process.getResidentSetSize();
        long totalRam = globalMemory.getTotal();
        double res1 = (double) ((usedRamProcess*100)/totalRam);
        System.out.println("\nMemory Usage :");
        System.out.println("Memory(Ram Used/Total Mem)="+res1+"%");
        System.out.println("Resident Size: "+humanReadableByteCountBin(usedRamProcess));
        System.out.println("Total Size: "+humanReadableByteCountBin(totalRam));

    }

 public static String humanReadableByteCountBin(long bytes) {
        long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
        if (absB < 1024) {
            return bytes + " B";
        }
        long value = absB;
        CharacterIterator ci = new StringCharacterIterator("KMGTPE");
        for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
            value >>= 10;
            ci.next();
        }
        value *= Long.signum(bytes);
        return String.format("%.1f %ciB", value / 1024.0, ci.current());
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55908151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档