From WikiChip
Difference between revisions of "resident set size"

(Fixed wrong variable name)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
The '''resident set size''' ('''RSS''') is the amount of space of [[physical memory]] (RAM) held by a [[process]]. The value is typically specified in [[bytes]] or [[pages]]. If the full amount of space required by a process exceeds the RSS, the remaining portion is typically stored in [[swap]].
+
{{title|Resident Set Size (RSS)}}
 +
The '''resident set size''' ('''RSS''') is the amount of space of [[physical memory]] (RAM) held by a [[process]]. The value is typically specified in [[bytes]] or [[pages]]. If the full amount of space required by a process exceeds the RSS, the remaining portion is typically stored in [[swap]]. Collectively, the total amount is the [[virtual set size]].
  
The '''peak resident set size''' ('''Peak RSS''') refers to the peak amount of memory a process has had up to now.
+
The '''peak resident set size''' ('''Peak RSS''' or '''Max RSS''') refers to the peak amount of memory a process has had up to that point.
  
 
== Overview ==
 
== Overview ==
In well-configured operating system, the RSS, which is typically specified in [[pages]], should equal the process' [[working set size|working set]]. Because the exact [[working set size]] often cannot be calculated, a processes usually have more pages than needed for its working set. While most systems today do not employee a working-set model due to its lack of accurate information about the reference pattern of a process, they do keep track of the resident set size. The use of RSS is often used in making decisions on whether the system had enough space to swap the process when it entered the [[ready queue]].
+
In well-configured operating system, the RSS, which is typically specified in [[pages]], should equal the process' [[working set size|working set]]. Because the exact [[working set size]] often cannot be calculated, a process usually has more pages than needed for its working set. While most systems today do not employee a working-set model due to its lack of accurate information about the reference pattern of a process, they do keep track of the resident set size. The use of RSS is often used in making decisions on whether the system had enough space to swap the process when it entered the [[ready queue]].
  
 
== Current RSS ==
 
== Current RSS ==
Line 26: Line 27:
 
     PROCESS_MEMORY_COUNTERS memCounter;
 
     PROCESS_MEMORY_COUNTERS memCounter;
 
     if (GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof memCounter))
 
     if (GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof memCounter))
         return (size_t)info.WorkingSetSize;
+
         return (size_t)memCounter.WorkingSetSize;
 
     return (size_t)0; /* get process mem info failed */
 
     return (size_t)0; /* get process mem info failed */
 
}
 
}
Line 47: Line 48:
 
}
 
}
 
</source>
 
</source>
 +
 +
== Peak RSS ==
 +
 +
=== Win32 ===
 +
The peak resident set size can be obtained using the same method as the current RSS.
 +
 +
<source lang="C">
 +
#include <windows.h>
 +
#include <psapi.h>
 +
size_t peakRSS()
 +
{
 +
    PROCESS_MEMORY_COUNTERS memCounter;
 +
    if (GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof memCounter))
 +
        return (size_t)memCounter.PeakWorkingSetSize;
 +
    return (size_t)0; /* get process mem info failed */
 +
}
 +
</source>
 +
 +
=== OS X ===
 +
Retrieving the peak RSS can be achieved in a very similar way to retrieving the current RSS.
 +
 +
<source lang="C">
 +
#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 */
 +
}
 +
</source>
 +
 +
=== Linux ===
 +
On linux, the peak RSS can be obtained using the [[getrusage|getrusage()]] function.
 +
 +
<source lang="C">
 +
#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 */
 +
}
 +
</source>
 +
 +
== RSS in various languages ==
 +
In addition to C, various other languages provide a convenient way of accessing such information:
 +
 +
{| class="wikitable"
 +
|-
 +
! Language !! Code
 +
|-
 +
| [[Node.js]] || <source lang="javascript">process.memoryUsage().rss</source>
 +
|}
  
 
== See also ==
 
== See also ==
* [[working set size]]
+
* [[Virtual set size]] (VSZ)
 +
* [[Proportional set size]] (PSS)
 +
* [[Working set size]]
 
* [[Virtual size]]
 
* [[Virtual size]]
 +
 +
[[Category:Memory management]]
 +
[[Category:Operating system terminology]]
 +
[[Category:Articles with C code]]

Latest revision as of 10:20, 3 June 2020

The resident set size (RSS) is the amount of space of physical memory (RAM) held by a process. The value is typically specified in bytes or pages. If the full amount of space required by a process exceeds the RSS, the remaining portion is typically stored in swap. Collectively, the total amount is the virtual set size.

The peak resident set size (Peak RSS or Max RSS) refers to the peak amount of memory a process has had up to that point.

Overview[edit]

In well-configured operating system, the RSS, which is typically specified in pages, should equal the process' working set. Because the exact working set size often cannot be calculated, a process usually has more pages than needed for its working set. While most systems today do not employee a working-set model due to its lack of accurate information about the reference pattern of a process, they do keep track of the resident set size. The use of RSS is often used in making decisions on whether the system had enough space to swap the process when it entered the ready queue.

Current RSS[edit]

/proc/self/status[edit]

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[edit]

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[edit]

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)memCounter.WorkingSetSize;
    return (size_t)0; /* get process mem info failed */
}

OS X[edit]

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[edit]

Win32[edit]

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)memCounter.PeakWorkingSetSize;
    return (size_t)0; /* get process mem info failed */
}

OS X[edit]

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[edit]

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 */
}

RSS in various languages[edit]

In addition to C, various other languages provide a convenient way of accessing such information:

Language Code
Node.js
process.memoryUsage().rss

See also[edit]