Skip to content

Commit 3382125

Browse files
authored
add function to get total live bytes (#33094)
1 parent 305b103 commit 3382125

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

base/util.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ end
6060
# total time spend in garbage collection, in nanoseconds
6161
gc_time_ns() = ccall(:jl_gc_total_hrtime, UInt64, ())
6262

63+
"""
64+
Base.gc_live_bytes()
65+
66+
Return the total size (in bytes) of objects currently in memory.
67+
This is computed as the total size of live objects after
68+
the last garbage collection, plus the number of bytes allocated
69+
since then.
70+
"""
71+
function gc_live_bytes()
72+
num = gc_num()
73+
Int(ccall(:jl_gc_live_bytes, Int64, ())) + num.allocd + num.deferred_alloc
74+
end
75+
6376
# print elapsed time, return expression value
6477
const _mem_units = ["byte", "KiB", "MiB", "GiB", "TiB", "PiB"]
6578
const _cnt_units = ["", " k", " M", " G", " T", " P"]

src/gc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,11 @@ void jl_gc_sync_total_bytes(void)
26552655
jl_gc_get_total_bytes(&last_gc_total_bytes);
26562656
}
26572657

2658+
JL_DLLEXPORT int64_t jl_gc_live_bytes(void)
2659+
{
2660+
return live_bytes;
2661+
}
2662+
26582663
static void jl_gc_premark(jl_ptls_t ptls2)
26592664
{
26602665
arraylist_t *remset = ptls2->heap.remset;

0 commit comments

Comments
 (0)