Skip to content

Commit a40aae8

Browse files
Yuriy VasiyarovTrott
Yuriy Vasiyarov
authored andcommitted
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() PR-URL: nodejs#27933 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent afb8474 commit a40aae8

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

doc/api/v8.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,24 @@ Returns an object with the following properties:
131131
* `malloced_memory` {number}
132132
* `peak_malloced_memory` {number}
133133
* `does_zap_garbage` {number}
134+
* `number_of_native_contexts` {number}
135+
* `number_of_detached_contexts` {number}
136+
134137

135138
`does_zap_garbage` is a 0/1 boolean, which signifies whether the
136139
`--zap_code_space` option is enabled or not. This makes V8 overwrite heap
137140
garbage with a bit pattern. The RSS footprint (resident memory set) gets bigger
138141
because it continuously touches all heap pages and that makes them less likely
139142
to get swapped out by the operating system.
140143

144+
`number_of_native_contexts` The value of native_context is the number of the
145+
top-level contexts currently active. Increase of this number over time indicates
146+
a memory leak.
147+
148+
`number_of_detached_contexts` The value of detached_context is the number
149+
of contexts that were detached and not yet garbage collected. This number
150+
being non-zero indicates a potential memory leak.
151+
141152
<!-- eslint-skip -->
142153
```js
143154
{
@@ -149,7 +160,9 @@ to get swapped out by the operating system.
149160
heap_size_limit: 1535115264,
150161
malloced_memory: 16384,
151162
peak_malloced_memory: 1127496,
152-
does_zap_garbage: 0
163+
does_zap_garbage: 0,
164+
number_of_native_contexts: 1,
165+
number_of_detached_contexts: 0
153166
}
154167
```
155168

lib/v8.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ const {
109109
kSpaceSizeIndex,
110110
kSpaceUsedSizeIndex,
111111
kSpaceAvailableSizeIndex,
112-
kPhysicalSpaceSizeIndex
112+
kPhysicalSpaceSizeIndex,
113+
kNumberOfNativeContextsIndex,
114+
kNumberOfDetachedContextsIndex
113115
} = internalBinding('v8');
114116

115117
const kNumberOfHeapSpaces = kHeapSpaces.length;
@@ -139,7 +141,9 @@ function getHeapStatistics() {
139141
'heap_size_limit': buffer[kHeapSizeLimitIndex],
140142
'malloced_memory': buffer[kMallocedMemoryIndex],
141143
'peak_malloced_memory': buffer[kPeakMallocedMemoryIndex],
142-
'does_zap_garbage': buffer[kDoesZapGarbageIndex]
144+
'does_zap_garbage': buffer[kDoesZapGarbageIndex],
145+
'number_of_native_contexts': buffer[kNumberOfNativeContextsIndex],
146+
'number_of_detached_contexts': buffer[kNumberOfDetachedContextsIndex]
143147
};
144148
}
145149

src/node_v8.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ using v8::Value;
5252
V(5, heap_size_limit, kHeapSizeLimitIndex) \
5353
V(6, malloced_memory, kMallocedMemoryIndex) \
5454
V(7, peak_malloced_memory, kPeakMallocedMemoryIndex) \
55-
V(8, does_zap_garbage, kDoesZapGarbageIndex)
55+
V(8, does_zap_garbage, kDoesZapGarbageIndex) \
56+
V(9, number_of_native_contexts, kNumberOfNativeContextsIndex) \
57+
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)
5658

5759
#define V(a, b, c) +1
5860
static const size_t kHeapStatisticsPropertiesCount =

test/parallel/test-v8-stats.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const keys = [
88
'does_zap_garbage',
99
'heap_size_limit',
1010
'malloced_memory',
11+
'number_of_detached_contexts',
12+
'number_of_native_contexts',
1113
'peak_malloced_memory',
1214
'total_available_size',
1315
'total_heap_size',

0 commit comments

Comments
 (0)