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.

    For developers willing to have more low level control, autofree can be disabled with .

    For example:

    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.

    1. struct User {
    2. name string
    3. }
    4. number := 7 // stack variable
    5. user := User{} // struct allocated on stack
    6. numbers := [1, 2, 3] // array allocated on heap, will be freed as the function exits
    7. println(numbers)
    8. numbers2 := [4, 5, 6] // array that's being returned, won't be freed here
    9. return numbers2