Increased memory usage in Python 3.11 when importing redis

Hi,
Python script is taking more memory in python3.11.11 compared to previous version 3.10.16 especially while using packages like redis, pyroute2 etc.
What could be the change for this behavior in python3.11? Does any optimizations are possible here?

1.Running Python Version: 3.10.16 (main, Feb 20 2025, 10:49:09) [GCC 8.4.0]
Filename: testmem.py

Line # Mem usage Increment Occurrences Line Contents

 3     21.2 MiB     21.2 MiB           1   @profile
 4                                         def do_stuff():
 5     21.2 MiB      0.0 MiB           1       import sys
 6     21.2 MiB      0.0 MiB           1       python_version = sys.version
 7     21.2 MiB      0.0 MiB           1       print(f"Running Python Version: {python_version}")
 8     25.8 MiB      4.6 MiB           1       import redis
 9     35.6 MiB      9.8 MiB           1       import pyroute2
  1. python3 -m memory_profiler testmem.py
    Running Python Version: 3.11.11 (main, Mar 12 2025, 15:15:10) [GCC 8.4.0]
    Filename: testmem.py

Line # Mem usage Increment Occurrences Line Contents

 3     23.8 MiB     23.8 MiB           1   @profile
 4                                         def do_stuff():
 5     23.8 MiB      0.0 MiB           1       import sys
 6     23.8 MiB      0.0 MiB           1       python_version = sys.version
 7     23.8 MiB      0.0 MiB           1       print(f"Running Python Version: {python_version}")
 8     29.1 MiB      5.4 MiB           1       import redis
 9     40.0 MiB     10.9 MiB           1       import pyroute2

Redis version is 4.5.5
pyroute2 version is 0.7.5
OS:Linux controller 5.15.178

I see similar results.

I used this script on Fedora to test all versions from 3.10 to 3.14

#!/bin/bash
for PY in python3.10 python3.11 python3.12 python3.13 python3.14
do
    echo "Info: Testing $PY..."
    rm -rf test-$PY
    $PY -m venv create test-$PY
    test-$PY/bin/python3 -m pip install memory-profiler pyroute2
    test-$PY/bin/python3 -m memory_profiler testmem.py |& tee profile-${PY}.txt
done

Running testmem.py

@profile
def test():
    import sys
    print('Version', sys.version)
    import pyroute2
    print(pyroute2)

test()

The summary of results are below.
@profile shows the startup memory size.
import pyroute2 shows the cost of importing pyroute2.

grep -e '@profile' -e 'import pyroute2' profile-python3.1*.txt
profile-python3.10.txt:     1   17.934 MiB   17.934 MiB           1   @profile
profile-python3.10.txt:     5   28.301 MiB   10.367 MiB           1       import pyroute2

profile-python3.11.txt:     1   20.238 MiB   20.238 MiB           1   @profile
profile-python3.11.txt:     5   31.969 MiB   11.730 MiB           1       import pyroute2

profile-python3.12.txt:     1   20.852 MiB   20.852 MiB           1   @profile
profile-python3.12.txt:     5   32.344 MiB   11.492 MiB           1       import pyroute2

profile-python3.13.txt:     1   22.270 MiB   22.270 MiB           1   @profile
profile-python3.13.txt:     5   33.762 MiB   11.492 MiB           1       import pyroute2

profile-python3.14.txt:     1   22.086 MiB   22.086 MiB           1   @profile
profile-python3.14.txt:     5   34.895 MiB   12.809 MiB           1       import pyroute2

I do not have knowledge about why the memory sizes changed.

I’m curious, what is this OS? Do you have a URL for the distro?