Windows下获取进程内存占用

#include <process.h>
using namespace std;QString getProcessMemory()
{QProcess p;int PIDNum = getpid();p.start("tasklist /FI \"PID EQ " + QString::number(PIDNum) + " \"");p.waitForFinished();QString result = QString::fromLocal8Bit(p.readAllStandardOutput());int lastKIndex =  result.lastIndexOf(' ');QString tempProcessMem;while(result[lastKIndex-1].toLatin1() != ' '){tempProcessMem += result[lastKIndex-1].toLatin1();--lastKIndex;if(lastKIndex < 0){break;}}reverse(tempProcessMem.begin(), tempProcessMem.end());tempProcessMem.remove(',');return QString::number(tempProcessMem.toDouble() / 1024.0, 'f', 2);
}

mac下获取进程内存占用

​
#include <assert.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <mach/mach.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/mach_port.h>
#include <mach/mach_traps.h>
#include <mach/task_info.h>
#include <mach/thread_info.h>
#include <mach/thread_act.h>
#include <mach/vm_region.h>
#include <mach/vm_map.h>
#include <mach/task.h>
// #include <mach/shared_memory_server.h>
#include <mach/shared_region.h>
#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <QDebug>typedef struct vmtotal vmtotal_t;typedef struct { /* dynamic process information */size_t rss, vsize;double utime, stime;
} RunProcDyn;int runGetDynamicProcInfo(pid_t pid, RunProcDyn *rpd)
{task_t task;kern_return_t error;mach_msg_type_number_t count;thread_array_t thread_table;thread_basic_info_t thi;thread_basic_info_data_t thi_data;unsigned table_size;struct task_basic_info ti;error = task_for_pid(mach_task_self(), pid, &task);if (error != KERN_SUCCESS) {/* fprintf(stderr, "++ Probably you have to set suid or become root.\n"); */rpd->rss = rpd->vsize = 0;rpd->utime = rpd->stime = 0;return 0;}count = TASK_BASIC_INFO_COUNT;error = task_info(task, TASK_BASIC_INFO, (task_info_t)&ti, &count);assert(error == KERN_SUCCESS);{ /* adapted from ps/tasks.c */vm_region_basic_info_data_64_t b_info;vm_address_t address = SHARED_REGION_BASE_PPC;vm_size_t size;mach_port_t object_name;count = VM_REGION_BASIC_INFO_COUNT_64;error = vm_region_64(task, &address, &size, VM_REGION_BASIC_INFO,(vm_region_info_t)&b_info, &count, &object_name);if (error == KERN_SUCCESS) {if (b_info.reserved && size == (SHARED_REGION_NESTING_SIZE_PPC) &&ti.virtual_size > (SHARED_REGION_NESTING_SIZE_PPC + SHARED_REGION_NESTING_MIN_PPC)){ti.virtual_size -= (SHARED_REGION_NESTING_SIZE_PPC + SHARED_REGION_NESTING_MIN_PPC);}}rpd->rss = ti.resident_size;rpd->vsize = ti.virtual_size;}{ /* calculate CPU times, adapted from top/libtop.c */unsigned i;rpd->utime = ti.user_time.seconds + ti.user_time.microseconds * 1e-6;rpd->stime = ti.system_time.seconds + ti.system_time.microseconds * 1e-6;error = task_threads(task, &thread_table, &table_size);assert(error == KERN_SUCCESS);thi = &thi_data;for (i = 0; i != table_size; ++i) {count = THREAD_BASIC_INFO_COUNT;error = thread_info(thread_table[i], THREAD_BASIC_INFO, (thread_info_t)thi, &count);assert(error == KERN_SUCCESS);if ((thi->flags & TH_FLAGS_IDLE) == 0) {rpd->utime += thi->user_time.seconds + thi->user_time.microseconds * 1e-6;rpd->stime += thi->system_time.seconds + thi->system_time.microseconds * 1e-6;}if (task != mach_task_self()) {error = mach_port_deallocate(mach_task_self(), thread_table[i]);assert(error == KERN_SUCCESS);}}error = vm_deallocate(mach_task_self(), (vm_offset_t)thread_table, table_size * sizeof(thread_array_t));assert(error == KERN_SUCCESS);}mach_port_deallocate(mach_task_self(), task);return 0;
}//获取mac进程内存占用QString getProcessMemory()
{int pid = getpid();RunProcDyn rpd;runGetDynamicProcInfo(pid, &rpd);return QString::number(rpd.rss/1024.0/1024.0, 'f', 2);
}​

Windows和Mac下获取(当前)进程内存占用相关推荐

  1. windows进程管理器_面向Windows,Linux,Mac ,安卓, 木马,密码破解内存提取基于KPCR结构的技术研究...

    Windows内存分析原理 内存镜像存储的是二进制数据,内存取证分析的任务就是从这些貌似毫无意义的二进制数据中找到系统的运行信息.这些二进制数据从根本上说,其来源都是操作系统的内存分配.为何分配.如何 ...

  2. 获取内存_如何获取一个进程所占用的内存

    推荐观看: BATJ面试官最喜欢问的:多线程.线程并发面试题详解(volatile+ThreadLocal+Sleep)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili​www.bilibili. ...

  3. Linux和Mac下获取文件CRC/MD5/SHA1/SHA256

    简介: Linux和Mac下获取文件CRC.MD5.hash.SHA1.SHA256值. 注: Linux和Mac下获取方式一致. 语法如下: cksum 文件名 # CRC md5sum 文件名 # ...

  4. Linux、Windows、Mac下Docker的安装与使用

    Linux.Windows.Mac下Docker的安装与使用 一.Linux下的安装 二.Windows与Mac下的安装 三.Docker的使用 一.Linux下的安装 这里以Centos 7为例,首 ...

  5. 如何在Windows和Mac下挂载EFI分区

    使用clover启动Mac系统时,我们经常需要替换 U盘启动中EFI. 使用分区工具DiskGenius无法直接在windows下将EFI文件导入U盘中. 本人在经历很多折磨后,终于实验出最快速最有效 ...

  6. Windows和Mac下NOIP/NOI复赛上机环境搭建

    根据NOIP和NOI复赛要求,上机环境是定制的NOI Linux,该系统集成编译器.调试器和集成开发环境,便于无差异地进行比赛及评分.一般情况下很多人在Windows下使用Dev C++进行编程训练, ...

  7. 获取指定进程所占用内存、指定GPU显存信息、所有GPU型号和驱动

    ######################################################### # 获取指定进程所占用内存 ############################ ...

  8. linux c 得到指定进程内存占用

    Linux系统的文件类型大致可分为三类: 普通文件, 目录文件和伪文件. 伪文件不是用来存储数据的, 因此这些文件不占用磁盘空间, 只是存在于内存中. /proc 让你可以与内核内部数据进行交互, 获 ...

  9. java 超出 xmx_java进程内存占用超过xmx设置的问题

    今天通过top查看系统信息,发现系统启动的一个进程内存占用达900M之多 top - 11:30:26 up 167 days, 40 min,  1 user,  load average: 1.0 ...

最新文章

  1. AI教育公司物灵科技完成战略融资,商汤科技投资
  2. 确认!语音识别大牛Daniel Povey将入职小米,曾遭霍普金斯大学解雇,怒拒Facebook
  3. [IE9] 如何让你的网站在IE9和Win7任务栏上更闪亮
  4. 超图空间数据模型概要1
  5. SQL Server-事务处理(Tansaction)与锁(Lock)
  6. 织梦 mail.class.php,详解织梦模板DEDECMS核心类TypeLink.class.php功能分析
  7. 腾讯云智服确保战“疫”期间服务不间断
  8. Linux学习资料-万用字符与特殊符号
  9. 开发人员安全行为规则_为开发人员改善生活的7条简单规则
  10. ThinkPHP6项目基操(13.实战部分 项目中的自定义异常处理总结 错误页面API错误)
  11. mac下hive-1.2.2-src版本的编译
  12. 事态升级是什么意思_为什么有的人越到关键时刻越容易掉链子?记住不要有“赌徒心理”...
  13. 【CentOS7】设置静态IP地址
  14. [ZOJ 3607] Lazier Salesgirl
  15. pvremore删除物理卷
  16. android 微信分享小程序 图片显示不全
  17. adb与java端口冲突,如何解决adb devices 端口被占用的问题zz
  18. Python爬虫马蜂窝结伴信息
  19. 《仿人机器人原理与实战》一1.2 反射弧与仿人机器人设计
  20. ROM RAM FLASH说明

热门文章

  1. L2-008. 最长对称子串
  2. tnt_esri.dat Arcgis8.1安装license
  3. 51nod1079中国剩余定理
  4. 支持异步通知的globalfifo平台设备驱动程序及其测试代码
  5. flash事件冒泡的理解
  6. 用Eclipse远程调试部署在Tomcat下的Web应用程序
  7. Python BFS 提取二值图联通域
  8. 实例协议分析RFC1483:AAL5和几种常见ADSL接入技术
  9. 从零开始学 Java - Spring MVC 实现跨域资源 CORS 请求
  10. putty的保存功能如何使用