Skip to content

Commit 3982919

Browse files
Adrien MaretMylesBorins
Adrien Maret
authored andcommittedAug 31, 2021
process: add direct access to rss without iterating pages
Accessing the rss value through memoryUsage() can be expensive because this method will also generate memory usage statistics by iterating on each page. This commit intend to offer a more direct access to rss value. Refs: #33384 PR-URL: #34291 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 577d228 commit 3982919

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed
 

‎doc/api/process.md

+22
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,28 @@ Will generate:
16101610
When using [`Worker`][] threads, `rss` will be a value that is valid for the
16111611
entire process, while the other fields will only refer to the current thread.
16121612

1613+
The `process.memoryUsage()` method iterate over each page to gather
1614+
informations about memory usage which can be slow depending on the
1615+
program memory allocations.
1616+
1617+
## `process.memoryUsage.rss()`
1618+
1619+
* Returns: {integer}
1620+
1621+
The `process.memoryUsage.rss()` method returns an integer representing the
1622+
Resident Set Size (RSS) in bytes.
1623+
1624+
The Resident Set Size, is the amount of space occupied in the main
1625+
memory device (that is a subset of the total allocated memory) for the
1626+
process, including all C++ and JavaScript objects and code.
1627+
1628+
This is the same value as the one returned by `process.memoryUsage()`.
1629+
1630+
```js
1631+
console.log(process.memoryUsage.rss());
1632+
// 35655680
1633+
```
1634+
16131635
## `process.nextTick(callback[, ...args])`
16141636
<!-- YAML
16151637
added: v0.1.26

‎lib/internal/process/per_thread.js

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function wrapProcessMethods(binding) {
4646
hrtimeBigInt: _hrtimeBigInt,
4747
cpuUsage: _cpuUsage,
4848
memoryUsage: _memoryUsage,
49+
rss,
4950
resourceUsage: _resourceUsage
5051
} = binding;
5152

@@ -160,6 +161,8 @@ function wrapProcessMethods(binding) {
160161
};
161162
}
162163

164+
memoryUsage.rss = rss;
165+
163166
function exit(code) {
164167
if (code || code === 0)
165168
process.exitCode = code;

‎src/node_process_methods.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,20 @@ static void Kill(const FunctionCallbackInfo<Value>& args) {
193193
args.GetReturnValue().Set(err);
194194
}
195195

196-
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
196+
static void Rss(const FunctionCallbackInfo<Value>& args) {
197197
Environment* env = Environment::GetCurrent(args);
198198

199199
size_t rss;
200200
int err = uv_resident_set_memory(&rss);
201201
if (err)
202202
return env->ThrowUVException(err, "uv_resident_set_memory");
203203

204+
args.GetReturnValue().Set(static_cast<double>(rss));
205+
}
206+
207+
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
208+
Environment* env = Environment::GetCurrent(args);
209+
204210
Isolate* isolate = env->isolate();
205211
// V8 memory usage
206212
HeapStatistics v8_heap_stats;
@@ -213,6 +219,11 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
213219
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
214220
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
215221

222+
size_t rss;
223+
int err = uv_resident_set_memory(&rss);
224+
if (err)
225+
return env->ThrowUVException(err, "uv_resident_set_memory");
226+
216227
fields[0] = rss;
217228
fields[1] = v8_heap_stats.total_heap_size();
218229
fields[2] = v8_heap_stats.used_heap_size();
@@ -450,6 +461,7 @@ static void InitializeProcessMethods(Local<Object> target,
450461
env->SetMethod(target, "umask", Umask);
451462
env->SetMethod(target, "_rawDebug", RawDebug);
452463
env->SetMethod(target, "memoryUsage", MemoryUsage);
464+
env->SetMethod(target, "rss", Rss);
453465
env->SetMethod(target, "cpuUsage", CPUUsage);
454466
env->SetMethod(target, "hrtime", Hrtime);
455467
env->SetMethod(target, "hrtimeBigInt", HrtimeBigInt);

‎test/parallel/test-memory-usage.js

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ if (r.arrayBuffers > 0) {
4444
assert.strictEqual(after.arrayBuffers - r.arrayBuffers, size,
4545
`${after.arrayBuffers} - ${r.arrayBuffers} === ${size}`);
4646
}
47+
48+
assert(process.memoryUsage.rss() > 0);

0 commit comments

Comments
 (0)
Please sign in to comment.