Skip to content

Commit

Permalink
process: add process.memoryUsage.external
Browse files Browse the repository at this point in the history
PR-URL: #9587
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
indutny authored and MylesBorins committed Jan 24, 2017
1 parent 26ce005 commit 67abbb6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
18 changes: 15 additions & 3 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,16 @@ As with `require.main`, it will be `undefined` if there was no entry script.
added: v0.1.16
-->

Returns an object describing the memory usage of the Node.js process
measured in bytes.
* Returns: {Object}
* `rss` {Integer}
* `heapTotal` {Integer}
* `heapUsed` {Integer}
* `external` {Integer}

The `process.memoryUsage()` method returns an object describing the memory usage
of the Node.js process measured in bytes.

For example, the code:

```js
console.log(process.memoryUsage());
Expand All @@ -776,10 +784,14 @@ This will generate:
```js
{ rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472 }
heapUsed: 650472,
external: 49879
}
```

`heapTotal` and `heapUsed` refer to V8's memory usage.
`external` refers to the memory usage of C++ objects bound to JavaScript
objects managed by V8.


## process.nextTick(callback[, arg][, ...])
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace node {
V(exponent_string, "exponent") \
V(exports_string, "exports") \
V(ext_key_usage_string, "ext_key_usage") \
V(external_string, "external") \
V(external_stream_string, "_externalStream") \
V(family_string, "family") \
V(fatal_exception_string, "_fatalException") \
Expand Down
4 changes: 4 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2145,11 +2145,15 @@ void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
Number::New(env->isolate(), v8_heap_stats.total_heap_size());
Local<Number> heap_used =
Number::New(env->isolate(), v8_heap_stats.used_heap_size());
Local<Number> external_mem =
Number::New(env->isolate(),
env->isolate()->AdjustAmountOfExternalAllocatedMemory(0));

Local<Object> info = Object::New(env->isolate());
info->Set(env->rss_string(), Number::New(env->isolate(), rss));
info->Set(env->heap_total_string(), heap_total);
info->Set(env->heap_used_string(), heap_used);
info->Set(env->external_string(), external_mem);

args.GetReturnValue().Set(info);
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-memory-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ var r = process.memoryUsage();
assert.ok(r.rss > 0);
assert.ok(r.heapTotal > 0);
assert.ok(r.heapUsed > 0);
assert.ok(r.external > 0);

0 comments on commit 67abbb6

Please sign in to comment.