Skip to content

Resident set size

resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).

RSS는 실제 메모리가 세팅된 크기이고, 스와핑이 되지 않은 물리적 메모리가 해당 task에 쓰이고 있는 양을 의미.

VSZ 크기만큼을 reserve 해놓고 실제로 물리적 메모리가 사용하고 있는 것은 RSS 크기만큼이다.

Current RSS

/proc/self/status

The RSS of the current process can be queried by reading the contents of /proc/self/status. The path /proc/{PID}/status can be used to look up the RSS of a given process by its process ID. For example, one could get the current RSS of Firefox by invoking the following Shell code:

$ cat /proc/`pidof firefox`/status | grep VmRSS

/proc/self/stat

The /proc/self/stat file provides similar info to status except only the values themselves are shown. The exact fields are not always the same across different operating systems, however, on Linux, RSS is the 24th field. The field has %ld scanf() format.

Win32

The current resident set size can be obtained using the GetProcessMemoryInfo() function.

#include <windows.h>
#include <psapi.h>
size_t currentRSS()
{
    PROCESS_MEMORY_COUNTERS memCounter;
    if (GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof memCounter))
        return (size_t)info.WorkingSetSize;
    return (size_t)0; /* get process mem info failed */
}

OS X

The current resident set size can be obtained using the task_info() function.

#include <unistd.h>
#include <sys/resource.h>
#include <mach/mach.h>
size_t currentRSS()
{
    struct mach_task_basic_info info;
    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
    if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS)
        return (size_t)info.resident_size;
    return (size_t)0; /* query failed */
}

Peak RSS

Win32

The peak resident set size can be obtained using the same method as the current RSS.

#include <windows.h>
#include <psapi.h>
size_t peakRSS()
{
    PROCESS_MEMORY_COUNTERS memCounter;
    if (GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof memCounter))
        return (size_t)info.PeakWorkingSetSize;
    return (size_t)0; /* get process mem info failed */
}

OS X

Retrieving the peak RSS can be achieved in a very similar way to retrieving the current RSS.

#include <unistd.h>
#include <sys/resource.h>
#include <mach/mach.h>
size_t peakRSS()
{
    struct mach_task_basic_info info;
    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
    if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS)
        return (size_t)info.resident_size_max;
    return (size_t)0; /* query failed */
}

Linux

On linux, the peak RSS can be obtained using the getrusage() function.

#include <sys/time.h>
#include <sys/resource.h>
size_t peakRSS()
{
    struct rusage rusage;
    if (!getrusage(RUSAGE_SELF, &rusage))
        return (size_t)rusage.ru_maxrss;
    return (size_t)0; /* query failed */
}

See also

Favorite site