fix coredump when fopen failed

This commit is contained in:
nroskill
2022-04-12 20:43:29 +08:00
committed by LINxiansheng
parent 69facd6237
commit d4c2794b60

View File

@ -26,13 +26,12 @@
int64_t get_virtual_memory_used() int64_t get_virtual_memory_used()
{ {
constexpr int BUFFER_SIZE = 128;
char filename[BUFFER_SIZE];
int64_t page_cnt = 0; int64_t page_cnt = 0;
snprintf(filename, BUFFER_SIZE, "/proc/%d/statm", getpid()); FILE* statm = fopen("/proc/self/statm", "r");
FILE* statm = fopen(filename, "r"); if (OB_NOT_NULL(statm)) {
fscanf(statm, "%ld", &page_cnt); fscanf(statm, "%ld", &page_cnt);
fclose(statm); fclose(statm);
}
return page_cnt * sysconf(_SC_PAGESIZE); return page_cnt * sysconf(_SC_PAGESIZE);
} }