Language with lowest memory foot print

I am writing some new code and I am experimenting with PHP and Python. The script will be connecting to a MongoDB instance and running a few core functions such as playing messages, playing MusicOnHold and voicemail. I noticed that every time I invoke an AGI and keep the call up, regardless of the language (PHP or Python) once I load the modules needed for my code, each agi uses 20-30 megs. This is sort of OK till there is a rush of calls. I am wondering if there is any other language that will offer a lower memory foot print such as

Has anyone dealt with any of these and Asterisk agi’s in which would have the lowest memory footprint?


Are you sure that that is 20-30 MB exclusive to the process, and not, mainly, shared, read-only, pages?

If you want a minimum footprint, you should use Fast AGI.

In terms of language, C is going to have the lowest footprint of commonly used languages.

Sounds like an issue with the Asterisk API wrappers you are using.

C (or any compiled language) eliminates dragging an interpreter into memory and parsing the scripting language source (Perl, PHP, Python) and will have the smallest memory footprint.

I write almost all of my AGIs in C.

Just for fun, in one terminal I started a new Python session:

ldo@theon:seaskirt> python3
Python 3.11.4 (main, Jun  7 2023, 10:13:09) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Check its RAM usage from another terminal:

ldo@theon:~> cat /proc/879757/smaps_rollup
00400000-7fffa697c000 ---p 00000000 00:00 0                              [rollup]
Rss:               12016 kB
... (etc) ...

Load my Seaskirt module into the Python session:

>>> import seaskirt

Check its RAM usage again:

ldo@theon:~> cat /proc/879757/smaps_rollup
00400000-7fffa697c000 ---p 00000000 00:00 0                              [rollup]
Rss:               30384 kB
... (etc) ...

So it’s gone from 12MB to 30MB.

So yeah, there would be a certain overhead in creating lots of these processes. FastAGI would be the way to go.

Just for comparison:

The sum of the Rss of one of my (very small) C AGIs on Linux 3.10 (before _rollup) is 416 kB

The sum of the Rss of the largest AGIs (including accessing a MySQL database) is 2,892 kB.

Lower-level languages are always going to be more “efficient” than higher-level ones, aren’t they. Your C code likely looks bloated compared to something written in assembly language, for example.

But how much code are you going to have to add to handle

  • a TLS connection
  • async event handling?

e.g. a simple example, just making a call:

async def main() :
    the_conn = await seaskirt.ManagerAsync \
        username = user,
        password = password,
        ssl_context = ssl_ca,
        want_events = False,
        timeout = timeout,
        debug = debug
    sys.stdout.write("the_conn opened, hello = \"%s\"\n" % the_conn.hello)
    originate_args = \
            "action" : "Originate",
            "parms" :
                    "Channel" : channel,
                    "Context" : context,
                    "Exten" : extension,
                    "Priority" : 1,
                    "Async" : "false",
                    "Timeout" : round(timeout * 1000),
    response = await the_conn.transact(**originate_args)
    sys.stdout.write(repr(response) + "\n")
    await the_conn.transact("Logoff", {})
    await the_conn.close()
#end main

Look at Mr 30MB callin’ Mr 3MB bloated :slight_smile:

In a previous life I coded in MACRO-11 (assembly) on multi-user computers that only had 32KB of per user address space.

I see your PDP-11 and raise you a PDP-8. Or better still, a Motorola 6800D2 kit with 384 bytes of RAM, programmed via a hex keypad. Or a TI-58C calculator with a maximum of 480 program steps or 60 memory registers, but not both at once.

But those who are not into techno-chest-beating might be interesting in seeing where some of the memory goes. I did a more detailed monitoring of the RAM usage of a Python process as it loads various modules needed by Seaskirt, ending with Seaskirt itself. Here are some of the points at which significant (> 1MiB) additional memory was needed:

  • Initial python3 command (as before) — 12.1MiB
  • import ssl — 4.2MiB
  • import http.client — 1.5MiB
  • import asyncio — 2.7MiB
  • import wsproto — 1.1MiB
  • import seaskirt — 6.4MiB

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.