diff --git a/src/main/vm/memory/heap.cpp b/src/main/vm/memory/heap.cpp index 53889b8c4f0b7a74cf8fd7e975e4785e07acd192..83b3f1b89576eea9e8b2d6858b98bfded3647918 100644 --- a/src/main/vm/memory/heap.cpp +++ b/src/main/vm/memory/heap.cpp @@ -117,6 +117,7 @@ void* Space::allocate(size_t size) { char* start = _top; _top += size; _capacity -= size; + _rate = _capacity * 1.0 / _size; //printf("after allocate %lx, _top is %p\n", size, _top); return start; } diff --git a/src/main/vm/memory/heap.hpp b/src/main/vm/memory/heap.hpp index 2e9a32de4031ae12c546ff305a5dc3c304fb0f87..e408742170608a7d2f96220c3e1dba4313863d07 100644 --- a/src/main/vm/memory/heap.hpp +++ b/src/main/vm/memory/heap.hpp @@ -13,6 +13,7 @@ private: char* _end; size_t _size; size_t _capacity; + double _rate; Space(size_t size); ~Space(); @@ -22,6 +23,7 @@ public: void clear(); bool can_alloc(size_t size); bool has_obj(char* obj); + double rate() { return _rate; } }; class Heap { @@ -46,6 +48,7 @@ public: void* allocate(size_t size); void* allocate_meta(size_t size); void copy_live_objects(); + double rate() { return eden->rate(); } void gc(); }; diff --git a/src/main/vm/runtime/interpreter.cpp b/src/main/vm/runtime/interpreter.cpp index 70c38540613f70c237da0a2c26c1e780843a383b..722c6e438acdd6fb33cc469fd4d5b9ba0ef88ccb 100644 --- a/src/main/vm/runtime/interpreter.cpp +++ b/src/main/vm/runtime/interpreter.cpp @@ -238,6 +238,10 @@ void Interpreter::eval_frame() { //printf("%d %d\n", op_code, op_arg); + if (Universe::heap->rate() < 0.05) { + Universe::heap->gc(); + } + switch (op_code) { case ByteCode::POP_TOP: POP();