Skip to content

Commit 62f3682

Browse files
addaleaxtargos
authored andcommitted
http2: report memory allocated by nghttp2 to V8
This helps the JS engine have a better understanding of the memory situation in HTTP/2-heavy applications, and avoids situations that behave like memory leaks due to previous underestimation of memory usage which is tied to JS objects. Refs: #28088 (comment) PR-URL: #28645 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c7cb70c commit 62f3682

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/node_http2.cc

+12-1
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,20 @@ class Http2Session::MemoryAllocatorInfo {
557557

558558
if (mem != nullptr) {
559559
// Adjust the memory info counter.
560-
session->current_nghttp2_memory_ += size - previous_size;
560+
// TODO(addaleax): Avoid the double bookkeeping we do with
561+
// current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory
562+
// and provide versions of our memory allocation utilities that take an
563+
// Environment*/Isolate* parameter and call the V8 method transparently.
564+
const int64_t new_size = size - previous_size;
565+
session->current_nghttp2_memory_ += new_size;
566+
session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
567+
new_size);
561568
*reinterpret_cast<size_t*>(mem) = size;
562569
mem += sizeof(size_t);
563570
} else if (size == 0) {
564571
session->current_nghttp2_memory_ -= previous_size;
572+
session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
573+
-static_cast<int64_t>(previous_size));
565574
}
566575

567576
return mem;
@@ -571,6 +580,8 @@ class Http2Session::MemoryAllocatorInfo {
571580
size_t* original_ptr = reinterpret_cast<size_t*>(
572581
static_cast<char*>(ptr) - sizeof(size_t));
573582
session->current_nghttp2_memory_ -= *original_ptr;
583+
session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
584+
-static_cast<int64_t>(*original_ptr));
574585
*original_ptr = 0;
575586
}
576587

0 commit comments

Comments
 (0)