Memory usage of current process in C

后端 未结 7 1519
深忆病人
深忆病人 2020-12-01 04:22

I need to get the memory usage of the current process in C. Can someone offer a code sample of how to do this on a Linux platform?

I\'m aware of the cat /proc/

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 04:59

    #include 
    #include 
    
    errno = 0;
    struct rusage* memory = malloc(sizeof(struct rusage));
    getrusage(RUSAGE_SELF, memory);
    if(errno == EFAULT)
        printf("Error: EFAULT\n");
    else if(errno == EINVAL)
        printf("Error: EINVAL\n");
    printf("Usage: %ld\n", memory->ru_ixrss);
    printf("Usage: %ld\n", memory->ru_isrss);
    printf("Usage: %ld\n", memory->ru_idrss);
    printf("Max: %ld\n", memory->ru_maxrss);
    

    I used this code but for some reason I get 0 all the time for all 4 printf()

提交回复
热议问题