Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit d23ac0e

Browse files
bnoordhuistjfontaine
authored andcommitted
src: add v8.getHeapStatistics() function
Add a one-to-one binding to v8::GetHeapStatistics(). Returns info on the current state of the JS heap, like total size and amount used.
1 parent 010222d commit d23ac0e

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

lib/tracing.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ v8.on('removeListener', function(name) {
4242
binding.stopGarbageCollectionTracking();
4343
}
4444
});
45+
46+
47+
v8.getHeapStatistics = binding.getHeapStatistics;

src/node_v8.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using v8::GCCallbackFlags;
3535
using v8::GCType;
3636
using v8::Handle;
3737
using v8::HandleScope;
38+
using v8::HeapStatistics;
3839
using v8::Isolate;
3940
using v8::Local;
4041
using v8::Null;
@@ -178,6 +179,28 @@ void StartGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
178179
}
179180

180181

182+
void GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
183+
Isolate* isolate = args.GetIsolate();
184+
HandleScope handle_scope(isolate);
185+
Environment* env = Environment::GetCurrent(isolate);
186+
HeapStatistics s;
187+
isolate->GetHeapStatistics(&s);
188+
Local<Object> info = Object::New();
189+
// TODO(trevnorris): Setting many object properties in C++ is a significant
190+
// performance hit. Redo this to pass the results to JS and create/set the
191+
// properties there.
192+
#define V(name) \
193+
info->Set(env->name ## _string(), Uint32::NewFromUnsigned(s.name(), isolate))
194+
V(total_heap_size);
195+
V(total_heap_size_executable);
196+
V(total_physical_size);
197+
V(used_heap_size);
198+
V(heap_size_limit);
199+
#undef V
200+
args.GetReturnValue().Set(info);
201+
}
202+
203+
181204
void StopGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
182205
HandleScope handle_scope(args.GetIsolate());
183206
Environment::GetCurrent(args.GetIsolate())->StopGarbageCollectionTracking();
@@ -193,6 +216,7 @@ void InitializeV8Bindings(Handle<Object> target,
193216
NODE_SET_METHOD(target,
194217
"stopGarbageCollectionTracking",
195218
StopGarbageCollectionTracking);
219+
NODE_SET_METHOD(target, "getHeapStatistics", GetHeapStatistics);
196220
}
197221

198222
} // namespace node

test/simple/test-v8-stats.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var v8 = require('tracing').v8;
25+
26+
var s = v8.getHeapStatistics();
27+
var keys = [
28+
'heap_size_limit',
29+
'total_heap_size',
30+
'total_heap_size_executable',
31+
'total_physical_size',
32+
'used_heap_size'];
33+
assert.deepEqual(Object.keys(s).sort(), keys);
34+
keys.forEach(function(key) {
35+
assert.equal(typeof s[key], 'number');
36+
});

0 commit comments

Comments
 (0)