Skip to content

Commit 92af503

Browse files
Adrien Maretdanielleadams
Adrien Maret
authored andcommitted
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d3dc124 commit 92af503

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

doc/api/process.md

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

1592+
The `process.memoryUsage()` method iterate over each page to gather
1593+
informations about memory usage which can be slow depending on the
1594+
program memory allocations.
1595+
1596+
## `process.memoryUsage.rss()`
1597+
1598+
* Returns: {integer}
1599+
1600+
The `process.memoryUsage.rss()` method returns an integer representing the
1601+
Resident Set Size (RSS) in bytes.
1602+
1603+
The Resident Set Size, is the amount of space occupied in the main
1604+
memory device (that is a subset of the total allocated memory) for the
1605+
process, including all C++ and JavaScript objects and code.
1606+
1607+
This is the same value as the one returned by `process.memoryUsage()`.
1608+
1609+
```js
1610+
console.log(process.memoryUsage.rss());
1611+
// 35655680
1612+
```
1613+
15921614
## `process.nextTick(callback[, ...args])`
15931615
<!-- YAML
15941616
added: v0.1.26

lib/internal/process/per_thread.js

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function wrapProcessMethods(binding) {
9393
const {
9494
cpuUsage: _cpuUsage,
9595
memoryUsage: _memoryUsage,
96+
rss,
9697
resourceUsage: _resourceUsage
9798
} = binding;
9899

@@ -168,6 +169,8 @@ function wrapProcessMethods(binding) {
168169
};
169170
}
170171

172+
memoryUsage.rss = rss;
173+
171174
function exit(code) {
172175
if (code || code === 0)
173176
process.exitCode = code;

src/node_process_methods.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,20 @@ static void Kill(const FunctionCallbackInfo<Value>& args) {
172172
args.GetReturnValue().Set(err);
173173
}
174174

175-
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
175+
static void Rss(const FunctionCallbackInfo<Value>& args) {
176176
Environment* env = Environment::GetCurrent(args);
177177

178178
size_t rss;
179179
int err = uv_resident_set_memory(&rss);
180180
if (err)
181181
return env->ThrowUVException(err, "uv_resident_set_memory");
182182

183+
args.GetReturnValue().Set(static_cast<double>(rss));
184+
}
185+
186+
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
187+
Environment* env = Environment::GetCurrent(args);
188+
183189
Isolate* isolate = env->isolate();
184190
// V8 memory usage
185191
HeapStatistics v8_heap_stats;
@@ -192,6 +198,11 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
192198
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
193199
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
194200

201+
size_t rss;
202+
int err = uv_resident_set_memory(&rss);
203+
if (err)
204+
return env->ThrowUVException(err, "uv_resident_set_memory");
205+
195206
fields[0] = rss;
196207
fields[1] = v8_heap_stats.total_heap_size();
197208
fields[2] = v8_heap_stats.used_heap_size();
@@ -542,6 +553,7 @@ static void InitializeProcessMethods(Local<Object> target,
542553
env->SetMethod(target, "umask", Umask);
543554
env->SetMethod(target, "_rawDebug", RawDebug);
544555
env->SetMethod(target, "memoryUsage", MemoryUsage);
556+
env->SetMethod(target, "rss", Rss);
545557
env->SetMethod(target, "cpuUsage", CPUUsage);
546558
env->SetMethod(target, "resourceUsage", ResourceUsage);
547559

@@ -568,6 +580,7 @@ void RegisterProcessMethodsExternalReferences(
568580
registry->Register(Umask);
569581
registry->Register(RawDebug);
570582
registry->Register(MemoryUsage);
583+
registry->Register(Rss);
571584
registry->Register(CPUUsage);
572585
registry->Register(ResourceUsage);
573586

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)