Memory management
V avoids doing unnecessary allocations in the first place by using value types, string buffers, promoting a simple abstraction-free code style.
Most objects (~90-100%) are freed by V’s autofree engine: the compiler inserts necessary free calls automatically during compilation. Remaining small percentage of objects is freed via reference counting.
The developer doesn’t need to change anything in their code. “It just works”, like in Python, Go, or Java, except there’s no heavy GC tracing everything or expensive RC for each object.
For developers willing to have more low level control, autofree can be disabled with -noautofree
.
Note: right now autofree is hidden behind the -autofree flag. It will be enabled by default in V 0.3.
For example:
import strings
fn draw_text(s string, x int, y int) {
// ...
}
fn draw_scene() {
// ...
name1 := 'abc'
name2 := 'def ghi'
draw_text('hello $name1', 10, 10)
draw_text('hello $name2', 100, 10)
draw_text(strings.repeat(`X`, 10000), 10, 50)
// ...
}
The strings don’t escape draw_text
, so they are cleaned up when the function exits.
In fact, the first two calls won’t result in any allocations at all. These two strings are small, V will use a preallocated buffer for them.
struct User {
name string
}
fn test() []int {
number := 7 // stack variable
user := User{} // struct allocated on stack
numbers := [1, 2, 3] // array allocated on heap, will be freed as the function exits
println(number)
println(user)
println(numbers)
numbers2 := [4, 5, 6] // array that's being returned, won't be freed here
return numbers2
}