I am using the smp_allocator. I need a big hashtable in my program. Can I check before creating if the size is not to big? In other words: Can I check the amount of available memory in Zig?
Memory is a resource provided by the operating system, not zig.
The OS might have a way to query how much memory is currently mapped to your program which might change after alloc/free, it almost certainly has a way to query the total available memory of the system.
If you’re asking about stack size, the default is Os dependent, on linux I think it’s 8Mb.
You can override that by setting the stack_size
field on compile artifacts, or --stack
on the cli.
The right thing to do in this instance is probably not to try to ask the allocator/OS how much memory is left, instead, consider reserving all the memory you need up front using APIs like initCapacity
or ensureTotalCapacity
and then use the *AssumeCapacity
or *Bounded
APIs to insert elements. This way, if there’s not enough memory left, your request will fail immediately instead of halfway through populating your data structure.