Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/be_gclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
static int m_allocated(bvm *vm)
{
size_t count = be_gc_memcount(vm);
#if BE_INTGER_TYPE >= 2
/* bint is 64-bit: can always represent the memory count as int */
be_pushint(vm, (bint)count);
#else
/* bint is 32-bit: fall back to real if count >= 2GB */
if (count < 0x80000000) {
be_pushint(vm, (bint)count);
} else {
be_pushreal(vm, (breal)count);
}
#endif
be_return(vm);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/gc.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import gc

# gc.collect() returns nil
assert(gc.collect() == nil)

# gc.allocated() returns a number (int for normal heap sizes)
var mem = gc.allocated()
assert(type(mem) == 'int' || type(mem) == 'real')
assert(mem > 0)

# allocating objects increases memory usage
var before = gc.allocated()
var l = []
for i : 0..99
l.push(str(i))
end
var after = gc.allocated()
assert(after > before)

# collecting after freeing should reduce or maintain allocation
l = nil
gc.collect()
var post = gc.allocated()
assert(post < after)

# gc.allocated() is consistent across calls (non-decreasing without allocation)
var a1 = gc.allocated()
var a2 = gc.allocated()
assert(a2 >= a1 || a2 == a1)