From b2f005125debe74051bf74d773c15efa39bbb949 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 23 Mar 2020 14:11:19 +0100 Subject: [PATCH] LibJS: Always collect all garbage when destroying Heap When the Heap is going down, it's our last chance to run destructors, so add a separate collector mode where we simply skip over the marking phase and go directly to sweeping. This causes everything to get swept and all live cells get destroyed. This way, valgrind reports 0 leaks on exit. :^) --- Libraries/LibJS/Heap/Heap.cpp | 11 +++++++---- Libraries/LibJS/Heap/Heap.h | 7 ++++++- Userland/js.cpp | 2 -- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index 4ad48b5a970..b3853680c49 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -51,6 +51,7 @@ Heap::Heap(Interpreter& interpreter) Heap::~Heap() { + collect_garbage(CollectionType::CollectEverything); } Cell* Heap::allocate_cell(size_t size) @@ -72,11 +73,13 @@ Cell* Heap::allocate_cell(size_t size) return cell; } -void Heap::collect_garbage() +void Heap::collect_garbage(CollectionType collection_type) { - HashTable roots; - gather_roots(roots); - mark_live_cells(roots); + if (collection_type == CollectionType::CollectGarbage) { + HashTable roots; + gather_roots(roots); + mark_live_cells(roots); + } sweep_dead_cells(); } diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 53ec59d2258..8ed4d55b87c 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -53,7 +53,12 @@ public: return static_cast(memory); } - void collect_garbage(); + enum class CollectionType { + CollectGarbage, + CollectEverything, + }; + + void collect_garbage(CollectionType = CollectionType::CollectGarbage); Interpreter& interpreter() { return m_interpreter; } diff --git a/Userland/js.cpp b/Userland/js.cpp index 4c2cdc67f9c..66e83169475 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -85,7 +85,5 @@ int main(int argc, char** argv) if (print_last_result) printf("%s\n", result.to_string().characters()); - dbg() << "Collecting garbage on exit..."; - interpreter.heap().collect_garbage(); return 0; }