Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builtin: expose gc_disable(), gc_enable(), gc_is_enabled(), in additi…
…on to the existing gc_collect() (#21002)
- Loading branch information
Showing
3 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// The goal of this program, is to test V's garbage collector interaction with gc_disable(), gc_enable(), gc_collect() and `-gc none`. | ||
// Note that this program is intended to be run on Linux, where /proc/PID/status exists. | ||
// It should run on other platforms too, but the last columns of the output will be empty. | ||
// Example invocation: `MAX_ITERATIONS=100 BLOCK_SIZE=1_000_000 v run gc.v` . | ||
import os | ||
|
||
const block_size = os.getenv_opt('BLOCK_SIZE') or { '1_000' }.int() | ||
const max_iterations = os.getenv_opt('MAX_ITERATIONS') or { '40' }.int() | ||
|
||
fn do_some_work_and_allocate_memory() u64 { | ||
a := []u8{len: block_size, init: u8(index)} | ||
mut s := u64(0) | ||
for x in a { | ||
s += x | ||
} | ||
return s | ||
} | ||
|
||
fn process_mem_stats(pid int) string { | ||
lines := os.read_lines('/proc/${pid}/status') or { [] } | ||
mut vals := map[string]string{} | ||
for line in lines { | ||
x := line.split(':') | ||
vals[x[0]] = x[1] | ||
} | ||
return 'VmSize: ${vals['VmSize']} | VmRSS: ${vals['VmRSS']}' | ||
} | ||
|
||
fn main() { | ||
println('BLOCK_SIZE: ${block_size:15}, MAX_ITERATIONS: ${max_iterations:5}, gc_is_enabled: ${gc_is_enabled()}') | ||
gc_disable() | ||
mypid := os.getpid() | ||
for c := 0; c < max_iterations; c++ { | ||
if c % 15 == 0 { | ||
gc_enable() | ||
} | ||
gc_collect() | ||
s := do_some_work_and_allocate_memory() | ||
println('gc_is_enabled: ${gc_is_enabled():6}, c: ${c:5}, s: ${s:10}, gc_memory_use: ${gc_memory_use():10}, ${process_mem_stats(mypid):30}') | ||
if c % 15 == 0 { | ||
gc_disable() | ||
} | ||
} | ||
} |