Skip to content

Commit

Permalink
http2: report memory allocated by nghttp2 to V8
Browse files Browse the repository at this point in the history
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: nodejs#28088 (comment)
  • Loading branch information
addaleax committed Jul 11, 2019
1 parent ca0884a commit d26ab70
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(size - previous_size));
*reinterpret_cast<size_t*>(mem) = size;
mem += sizeof(size_t);
} else if (size == 0) {
session->current_nghttp2_memory_ -= previous_size;
session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(previous_size));
}

return mem;
Expand All @@ -571,6 +579,8 @@ class Http2Session::MemoryAllocatorInfo {
size_t* original_ptr = reinterpret_cast<size_t*>(
static_cast<char*>(ptr) - sizeof(size_t));
session->current_nghttp2_memory_ -= *original_ptr;
session->env()->isolate()->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(*original_ptr));
*original_ptr = 0;
}

Expand Down

0 comments on commit d26ab70

Please sign in to comment.