From 9aaf19f4de1ba1f1bd7278f35d5b3c9e56d860da Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 6 Apr 2020 12:36:49 +0200 Subject: [PATCH] LibJS: Do a garbage collection every N allocations (N=10'000) To prevent the heap from growing infinitely large, we now do a full GC every 10'000 allocations. :^) --- Libraries/LibJS/Heap/Heap.cpp | 8 +++++++- Libraries/LibJS/Heap/Heap.h | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index c75702bd35..3dd3902ca5 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -58,8 +58,14 @@ Heap::~Heap() Cell* Heap::allocate_cell(size_t size) { - if (should_collect_on_every_allocation()) + if (should_collect_on_every_allocation()) { collect_garbage(); + } else if (m_allocations_since_last_gc > m_max_allocations_between_gc) { + m_allocations_since_last_gc = 0; + collect_garbage(); + } else { + ++m_allocations_since_last_gc; + } for (auto& block : m_blocks) { if (size > block->cell_size()) diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 8ed4d55b87..afaacada13 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -78,6 +78,9 @@ private: Cell* cell_from_possible_pointer(FlatPtr); + size_t m_max_allocations_between_gc { 10000 }; + size_t m_allocations_since_last_gc { false }; + bool m_should_collect_on_every_allocation { false }; Interpreter& m_interpreter;