High Memory Usage

Hi,

This code when compiled with mojo build uses 19M of memory according to htop (and 2G of virtual memory) on Linux amd64.
What are the reasons for this imho high memory usage?
Anything I can do to bring it down?
Thanks.

from time import sleep

fn main():
  while True:
    print(“hello”)
    sleep(5.0)

The resident 19M is likely due to how the allocator works. Mojo’s runtime does do some allocation up front for various things like wrappers around stdin/stdout, storing which cores are pcores on Intel CPUs, etc. tcmalloc, Mojo’s allocator, is designed to minimize fragmentation and have good multi-threaded throughput, which means that it creates decent sized per-core caches. Most of that memory resident memory usage is likely coming from there.

The 2 GB of virtual memory is lazily handed over by the kernel, since tcmalloc requests a decent chunk of memory up front so it doesn’t need to do that expensive operation again for short-lived allocations. Unless you actually touch that memory, it doesn’t matter except as a bit of book keeping work for the kernel.