From 84b141496a236ba3e66c401ff918a85006faa604 Mon Sep 17 00:00:00 2001 From: Yuriy Vasiyarov Date: Tue, 28 May 2019 13:41:12 +0300 Subject: [PATCH 1/3] src: export number_of_native_contexts and number_of_detached_contexts export number_of_native_contexts and number_of_detached_contexts as part of v8.getHeapStatistics() --- doc/api/v8.md | 15 ++++++++++++++- lib/v8.js | 8 ++++++-- src/node_v8.cc | 4 +++- test/parallel/test-v8-stats.js | 2 ++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index 9949720cf68b78..401914295a48e1 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -131,6 +131,9 @@ Returns an object with the following properties: * `malloced_memory` {number} * `peak_malloced_memory` {number} * `does_zap_garbage` {number} +* `number_of_native_contexts` {number} +* `number_of_detached_contexts` {number} + `does_zap_garbage` is a 0/1 boolean, which signifies whether the `--zap_code_space` option is enabled or not. This makes V8 overwrite heap @@ -138,6 +141,14 @@ garbage with a bit pattern. The RSS footprint (resident memory set) gets bigger because it continuously touches all heap pages and that makes them less likely to get swapped out by the operating system. +`number_of_native_contexts` The value of native_context is the number of the +top-level contexts created. Increase of this number over time indicates +a memory leak. + +`number_of_detached_contexts` The value of detached_context is the number +of contexts that were detached and not yet garbage collected. This number +being non-zero indicates potential memory leak. + ```js { @@ -149,7 +160,9 @@ to get swapped out by the operating system. heap_size_limit: 1535115264, malloced_memory: 16384, peak_malloced_memory: 1127496, - does_zap_garbage: 0 + does_zap_garbage: 0, + number_of_native_contexts: 1, + number_of_detached_contexts: 0 } ``` diff --git a/lib/v8.js b/lib/v8.js index 2bede41291a947..cbb8229c7dd914 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -109,7 +109,9 @@ const { kSpaceSizeIndex, kSpaceUsedSizeIndex, kSpaceAvailableSizeIndex, - kPhysicalSpaceSizeIndex + kPhysicalSpaceSizeIndex, + kNumberOfNativeContextsIndex, + kNumberOfDetachedContextsIndex } = internalBinding('v8'); const kNumberOfHeapSpaces = kHeapSpaces.length; @@ -139,7 +141,9 @@ function getHeapStatistics() { 'heap_size_limit': buffer[kHeapSizeLimitIndex], 'malloced_memory': buffer[kMallocedMemoryIndex], 'peak_malloced_memory': buffer[kPeakMallocedMemoryIndex], - 'does_zap_garbage': buffer[kDoesZapGarbageIndex] + 'does_zap_garbage': buffer[kDoesZapGarbageIndex], + 'number_of_native_contexts': buffer[kNumberOfNativeContextsIndex], + 'number_of_detached_contexts': buffer[kNumberOfDetachedContextsIndex] }; } diff --git a/src/node_v8.cc b/src/node_v8.cc index 5adc53b84d87c4..1227ebec5362e5 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -52,7 +52,9 @@ using v8::Value; V(5, heap_size_limit, kHeapSizeLimitIndex) \ V(6, malloced_memory, kMallocedMemoryIndex) \ V(7, peak_malloced_memory, kPeakMallocedMemoryIndex) \ - V(8, does_zap_garbage, kDoesZapGarbageIndex) + V(8, does_zap_garbage, kDoesZapGarbageIndex) \ + V(9, number_of_native_contexts, kNumberOfNativeContextsIndex) \ + V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) #define V(a, b, c) +1 static const size_t kHeapStatisticsPropertiesCount = diff --git a/test/parallel/test-v8-stats.js b/test/parallel/test-v8-stats.js index 83b515d3fd56ec..3e2eadef1711ae 100644 --- a/test/parallel/test-v8-stats.js +++ b/test/parallel/test-v8-stats.js @@ -8,6 +8,8 @@ const keys = [ 'does_zap_garbage', 'heap_size_limit', 'malloced_memory', + 'number_of_detached_contexts', + 'number_of_native_contexts', 'peak_malloced_memory', 'total_available_size', 'total_heap_size', From 5cd33022a9972a65d32a2f3bbe70188c67d32d49 Mon Sep 17 00:00:00 2001 From: Yuriy Vasiyarov Date: Tue, 28 May 2019 14:23:27 +0300 Subject: [PATCH 2/3] Update doc/api/v8.md Co-Authored-By: Anna Henningsen --- doc/api/v8.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index 401914295a48e1..9374fabb08a06b 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -142,7 +142,7 @@ because it continuously touches all heap pages and that makes them less likely to get swapped out by the operating system. `number_of_native_contexts` The value of native_context is the number of the -top-level contexts created. Increase of this number over time indicates +top-level contexts currently active. Increase of this number over time indicates a memory leak. `number_of_detached_contexts` The value of detached_context is the number From 86a2c21a7f7598d307618b924d4a467e96095655 Mon Sep 17 00:00:00 2001 From: Yuriy Vasiyarov Date: Wed, 29 May 2019 15:16:18 +0300 Subject: [PATCH 3/3] Update doc/api/v8.md Co-Authored-By: Anna Henningsen --- doc/api/v8.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index 9374fabb08a06b..ab5950dda62c6a 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -147,7 +147,7 @@ a memory leak. `number_of_detached_contexts` The value of detached_context is the number of contexts that were detached and not yet garbage collected. This number -being non-zero indicates potential memory leak. +being non-zero indicates a potential memory leak. ```js