Skip to content

3. Using Vala's Automatic Memory Management

When writing Vala code, including code using a C library, memory management is handled by the Vala compiler. There is usually no need to manually claim and free memory. When writing a binding, however, it is an important part of the process to accurately instruct the Vala compiler how to use the C library's memory management calls. This is a one time job and means anyone then using the binding can take advantage of a binding that is much easier to write code for.

Vala's memory allocation and types are a bit more involved than most languages. In Python, everything is a dynamically-typed object and it is allocated out of the ether then gets garbage collected. In C, memory allocation is largely handled by the user and types are simply descriptions of memory considered at compile time. Vala lives somewhere trying to cover all the bases at once. Importantly, the types in Vala imply something about the memory management.

There are 4 memory management schemes in Vala:

SchemeMemory ManagerHelpers?Copy Cost
ValuesC compilerNoCheap
ParentedC compilerYesExpensive
Singly-OwnedHeap allocatorYesExpensive
Reference-CountedHeap allocatorYesCheap

3.1. Pointers in C (or what all these *'s mean)

3.2. Constants, the Stack and the Heap in C

3.3. The Concept of "Ownership" in Vala

3.4. Binding to C Heap Handlers