Skip to content

Commit

Permalink
src: export number_of_native_contexts and number_of_detached_contexts
Browse files Browse the repository at this point in the history
export number_of_native_contexts and number_of_detached_contexts as
part of v8.getHeapStatistics()

PR-URL: #27933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
Yuriy Vasiyarov authored and targos committed May 31, 2019
1 parent 5eccd64 commit 33236b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
15 changes: 14 additions & 1 deletion doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,24 @@ 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
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 currently active. 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 a potential memory leak.

<!-- eslint-skip -->
```js
{
Expand All @@ -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
}
```

Expand Down
8 changes: 6 additions & 2 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ const {
kSpaceSizeIndex,
kSpaceUsedSizeIndex,
kSpaceAvailableSizeIndex,
kPhysicalSpaceSizeIndex
kPhysicalSpaceSizeIndex,
kNumberOfNativeContextsIndex,
kNumberOfDetachedContextsIndex
} = internalBinding('v8');

const kNumberOfHeapSpaces = kHeapSpaces.length;
Expand Down Expand Up @@ -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]
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-v8-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 33236b7

Please sign in to comment.