From d26ab70856dba0e7fb0e65c8bb8db034de51d1f0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 11 Jul 2019 23:46:37 +0200 Subject: [PATCH 1/2] 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: https://github.com/nodejs/node/issues/28088#issuecomment-509965105 --- src/node_http2.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/node_http2.cc b/src/node_http2.cc index 9047171e114606..80a78f1ade3115 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -557,11 +557,19 @@ class Http2Session::MemoryAllocatorInfo { if (mem != nullptr) { // Adjust the memory info counter. + // TODO(addaleax): Avoid the double bookkeeping we do with + // current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory + // and provide versions of our memory allocation utilities that take an + // Environment*/Isolate* parameter and call the V8 method transparently. session->current_nghttp2_memory_ += size - previous_size; + session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( + static_cast(size - previous_size)); *reinterpret_cast(mem) = size; mem += sizeof(size_t); } else if (size == 0) { session->current_nghttp2_memory_ -= previous_size; + session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( + -static_cast(previous_size)); } return mem; @@ -571,6 +579,8 @@ class Http2Session::MemoryAllocatorInfo { size_t* original_ptr = reinterpret_cast( static_cast(ptr) - sizeof(size_t)); session->current_nghttp2_memory_ -= *original_ptr; + session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( + -static_cast(*original_ptr)); *original_ptr = 0; } From d34c8610aad1d6ed6f9812a787f3e776d959ef31 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 14 Jul 2019 23:21:35 +0200 Subject: [PATCH 2/2] fixup! http2: report memory allocated by nghttp2 to V8 --- src/node_http2.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/node_http2.cc b/src/node_http2.cc index 80a78f1ade3115..ab1c73777941a5 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -561,9 +561,10 @@ class Http2Session::MemoryAllocatorInfo { // current_nghttp2_memory_ + AdjustAmountOfExternalAllocatedMemory // and provide versions of our memory allocation utilities that take an // Environment*/Isolate* parameter and call the V8 method transparently. - session->current_nghttp2_memory_ += size - previous_size; + const int64_t new_size = size - previous_size; + session->current_nghttp2_memory_ += new_size; session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory( - static_cast(size - previous_size)); + new_size); *reinterpret_cast(mem) = size; mem += sizeof(size_t); } else if (size == 0) {