#include <stdio.h>
#include <stdlib.h>
// Memory Usage
#include <sys/types.h>
#include <sys/sysctl.h>
void simple_wait() {
size_t wait_buf_size = 32;
char *wait_buf;
wait_buf = (char *)malloc(wait_buf_size * sizeof(char));
getline(&wait_buf, &wait_buf_size, stdin);
free(wait_buf);
}
void show_memory_usage() {
struct rusage usage;
printf("\n-- Memory Usage -- Begin --\n");
if(0 == getrusage(RUSAGE_SELF, &usage)) {
printf("\tBytes:\t%ld\n", usage.ru_maxrss);
printf("\t= \t%.3f KB\n", usage.ru_maxrss / 1024.0);
printf("\t= \t%.3f MB\n", usage.ru_maxrss / 1024.0 / 1024.0);
} else {
printf("\tREAD ERROR\n");
}
printf("-- End -- Memory Usage --\n");
}
int main(int argc, char *argv[]) {
show_memory_usage();
printf("press enter to continue\n");
simple_wait();
return 0;
}
用法:
$ gcc t.c
$ ./a.out
-- Memory Usage -- Begin --
Bytes: 671744
= 656.000 KB
= 0.641 MB
-- End -- Memory Usage --
press enter to continue
只是在 man page: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getrusage.2.html 的描述:
ru_maxrss the maximum resident set size utilized (in kilobytes).
但看數據總覺得是 bytes 啊 XD 先記錄起來,有空再追蹤
沒有留言:
張貼留言