From 23a573f7cbc1d2c6e195f5caa5c078426e490543 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 12 Nov 2016 16:49:40 -0500 Subject: [PATCH] process: add `process.memoryUsage.external` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/9587 Reviewed-By: Ben Noordhuis Reviewed-By: Matteo Collina Reviewed-By: Michaël Zasso Reviewed-By: Johan Bergström Reviewed-By: Colin Ihrig Reviewed-By: Roman Reiss Reviewed-By: Anna Henningsen --- doc/api/process.md | 18 +++++++++++++++--- src/env.h | 1 + src/node.cc | 4 ++++ test/parallel/test-memory-usage.js | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 7279c33a2935a6..1cb946aa2579c7 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -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()); @@ -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][, ...]) diff --git a/src/env.h b/src/env.h index a015988c345e24..2be7aa1824d152 100644 --- a/src/env.h +++ b/src/env.h @@ -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") \ diff --git a/src/node.cc b/src/node.cc index 2935729bbb9f41..de3e5757705a47 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2145,11 +2145,15 @@ void MemoryUsage(const FunctionCallbackInfo& args) { Number::New(env->isolate(), v8_heap_stats.total_heap_size()); Local heap_used = Number::New(env->isolate(), v8_heap_stats.used_heap_size()); + Local external_mem = + Number::New(env->isolate(), + env->isolate()->AdjustAmountOfExternalAllocatedMemory(0)); Local 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); } diff --git a/test/parallel/test-memory-usage.js b/test/parallel/test-memory-usage.js index f704c8812801ba..c5905c537ac613 100644 --- a/test/parallel/test-memory-usage.js +++ b/test/parallel/test-memory-usage.js @@ -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);