西安专业做网站的的公司,建设工程造价信息网站,兰州网站设计公司排名,企业网站有哪些内容在程序编程过程中#xff0c;为了防止出现内存泄漏情况出现#xff0c;需要持续关注内存程序内存占用情况。如下方式实现获取当前进程内存使用情况#xff1a;
linux#xff1a;
void my_top(string path, bool flag)
{if(flag){FILE* read_top fopen(/proc/self/…在程序编程过程中为了防止出现内存泄漏情况出现需要持续关注内存程序内存占用情况。如下方式实现获取当前进程内存使用情况
linux
void my_top(string path, bool flag)
{if(flag){FILE* read_top fopen(/proc/self/status, r);char line_rss[128];unsigned long long Pid, VmSize, VmRSS;while (fgets(line_rss, 128, read_top) ! NULL){if (strncmp(line_rss, Pid:, 4) 0){string str(line_rss 4);Pid strtoull(str.c_str(), NULL, 19);}if (strncmp(line_rss, VmSize:, 7) 0){string str(line_rss 7);VmSize strtoull(str.c_str(), NULL, 19);}if (strncmp(line_rss, VmRSS:, 6) 0){string str(line_rss 6);VmRSS strtoull(str.c_str(), NULL, 19);}if(Pid 0 || VmSize 0 || VmRSS 0){fclose(read_top);break;}}fclose(read_top);ofstream writer_top(path, ios::app);writer_top Pid VmSize VmRSS endl;writer_top.close();}else{ofstream writer_top(path, ios::app);writer_top 0 0 0 endl;writer_top.close();}
}其他资源
VmPeak: 表示进程所占用最大虚拟内存大小
VmSize: 表示进程当前虚拟内存大小
VmLck: 表示被锁定的内存大小
VmHWM: 表示进程所占用物理内存的峰值
VmRSS: 表示进程当前占用物理内存的大小(与procrank中的RSS)
VmData: 表示进程数据段的大小
VmStk: 表示进程堆栈段的大小
VmExe: 表示进程代码的大小
VmLib: 表示进程所使用共享库的大小
VmPTE: 表示进程页表项的大小windows
#include windows.h
#include psapi.h
#include stdio.hint main()
{PROCESS_MEMORY_COUNTERS pmc;if (GetProcessMemoryInfo(GetCurrentProcess(), pmc, sizeof(pmc))){printf(当前进程占用内存大小为%d KB\n, pmc.WorkingSetSize / 1024);}return 0;
}