Runtime thread and heap size count

Hey Zig members, I am looking for some direction to get the runtime heap size usage and overall thread count under the hood being executed of a running app, something similar to the Go runtime pkg.

I can extract the number of threads from Monitor app, but I wanted to get this information through code.

Thanks

Are you trying to get this information from within the process or for a different process?

What platform(s) do you need this for?

I don’t know any answer off the top of my head, and I’d like more details, so I don’t waste time looking at the wrong things.

Sure, I will be more specific in my questions going forward.

I am looking to get this information within the process (own heap and associated threads) and targeting Linux alone.

If the spawning of threads is your code, you can track that yourself. Otherwise you can read /proc/[PID]/stat which will have near the end: thread [num]
alternatively, you can count the number of files in /proc/[PID]/tasks/

You can get the current PID with std.os.linux.getpid, there is not a cross-platform API for this.

DebugAllocator when config .enable_memor_limit = true, you can access total_requested_bytes, the default limit is the maximum addressable memory which you will not hit, unless you override the limit.

But that allocator is slow, so you might want a custom allocator that just wraps another allocator and counts the requested memory.

If you cant provide the allocator for some allocations, eg a c library, or want the mapped memory instead of the used memory.
/proc/[PID]/statm which has a bunch of numbers seperated by space which are, in order:

  • total size
  • resident size
  • shared size
  • executable size
  • data/stack size
  • library size
  • dirty pages
4 Likes

Thanks @vulpesx I will work on this idea.