Search code examples
nuitka

Nuitka to reduce RAM usage by imported python code


I have some services written in python. The services is using a lot of RAM due to imports (every import seems to be loading the whole imported library to RAM, even though I'm only using a fraction of it. Python is interpreted, so it doesn't know in advance what parts of the library I'll use, and just brings everything).

Nuitka compiles the python code. So I was hoping that if I used it my program would use less RAM.

In practice, this is what I got:

program 1 - takes ~10MB in python and ~12MB after compilation with Nuitka:

while True:
    time.sleep(1)
    print("Hello")

program 2 - taks ~28MB in python, and ~41MB after compilation with Nuitka:

from flask import Flask

while True:
    time.sleep(1)
    print("Hello")

It seems that - despite the compilation - Nuitka is loading all the imported code - even that which I won't use. Is this true?


Solution

  • I posted this question on Nuitka's GitHub page, and got this answer:

    Yes, Nuitka is very compatible with Python and optimizing for performance, not for RAM usage. The C code is a lot larger than bytecode. Your best choice is Python there.

    https://github.com/Nuitka/Nuitka/issues/2618#issuecomment-1865938992

    So it seems that Nuitka won't give me any advantage as far as RAM is concerned.