how to get the memory usage of one process(Linux)

本文展示了一个使用C++编写的简单程序,通过调用getrusage()函数实时监控进程的内存使用情况。程序每三秒分配一次20MB的内存,并打印当前总内存占用及实际使用的内存大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <sys/resource.h>

#include <unistd.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main() {

int i = 0;

int totalmem = 0;

struct rusage r_usage;

while (++i <= 10) {

void *m = malloc(20*1024*1024);

memset(m,0,20*1024*1024);

totalmem +=20*1024*1024;

printf("Memory malloc[20MB, %dKB], totalmem=%dB, %dKB, %dMB\n", 20*1024, totalmem, totalmem/1024, totalmem/1024/1024);

getrusage(RUSAGE_SELF,&r_usage);

printf("Memory usage = %ldKB, %ldMB\n",r_usage.ru_maxrss, r_usage.ru_maxrss/1024);

sleep (3);

}

printf("\nAllocated memory, sleeping ten seconds after which we will check again...\n\n");

sleep (10);

getrusage(RUSAGE_SELF,&r_usage);

printf("Memory usage = %ldKB, %ldMB\n",r_usage.ru_maxrss, r_usage.ru_maxrss/1024);

return 0;

}

 

 

g++ test.c -o test

 

./test

Memory malloc[20MB, 20480KB], totalmem=20971520B, 20480KB, 20MB

Memory usage = 21284KB, 20MB

Memory malloc[20MB, 20480KB], totalmem=41943040B, 40960KB, 40MB

Memory usage = 42176KB, 41MB

Memory malloc[20MB, 20480KB], totalmem=62914560B, 61440KB, 60MB

Memory usage = 62504KB, 61MB

Memory malloc[20MB, 20480KB], totalmem=83886080B, 81920KB, 80MB

Memory usage = 83096KB, 81MB

Memory malloc[20MB, 20480KB], totalmem=104857600B, 102400KB, 100MB

Memory usage = 103688KB, 101MB

Memory malloc[20MB, 20480KB], totalmem=125829120B, 122880KB, 120MB

Memory usage = 124016KB, 121MB

Memory malloc[20MB, 20480KB], totalmem=146800640B, 143360KB, 140MB

Memory usage = 144608KB, 141MB

Memory malloc[20MB, 20480KB], totalmem=167772160B, 163840KB, 160MB

Memory usage = 164936KB, 161MB

Memory malloc[20MB, 20480KB], totalmem=188743680B, 184320KB, 180MB

Memory usage = 185528KB, 181MB

Memory malloc[20MB, 20480KB], totalmem=209715200B, 204800KB, 200MB

Memory usage = 206120KB, 201MB

 

Allocated memory, sleeping ten seconds after which we will check again...

 

Memory usage = 206120KB, 201MB

 

 

http://appcrawler.com/wordpress/2013/05/13/simple-example-of-tracking-memory-using-getrusage/

https://stackoverflow.com/questions/1558402/memory-usage-of-current-process-in-c

Write a program to simulate a process of translation from a logical address to physical address. Assumptions 1. Assume the file la.txt includes the sequence of generated addresses from CPU. 2. Use a part of memory as backing store that store data for a process. 3. The backing store size is 128 bytes 4. The size of process p is 128 bytes. 5. The contents of p is included in a file pdata.bin which is a binary file. 6. Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7. The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. 8. At beginning, no page table is available for process p. Requirements Write a program to 1. Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2. Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3. Read logical addresses one by one from la.txt. 4. For each logical address, a) if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b) if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Refer to Figure 1 for the relationships and how physical memory, backing store, and CPU are simulated. Figure 1 How physical memory, backing store and CPU are simulated in this program assignment Hints: a) use a memory block pointed by a pointer or use an array as a simulation of backing store b) use functions fread or mmap for the binary file read. Search through the Internet for the usage of these functions. c) Use an array to simulate the memory. d) Use bit operators &, |, <<, and >> to get the bits in a logic address or form a physical address e) Use char for the type of data in the process, use unsigned char (8 bits) for the type of address. Coding & Submission 1. Using pure C to finish this program. 2. Put all the codes in one .c file named PA3_#####.c, replace “#####” as the last 5 digits of your student ID. 3. Put pdata.txt and la.txt in the same folder as PA3_#####.c, which the need .txt file can be open directly by filename instead of absolute path. 4. Submit only the .c file mentioned above.使用C语言完成
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coloriy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值