From earlier questions on SO like this and this, it seems that the simple import
keyword does not do lazy loading of modules. However, when I run a simple test to check the memory usage before and after the import
statement - it seems that some modules are indeed loaded lazily.
Example:
>>> import django
>>> from django.http import HttpResponse
Memory Usage (the first free
command is run after the first import and the second free
command is run after the second import above):
$ free -m
total used free shared buff/cache available
Mem: 3935 1773 399 0 1762 1889
$ free -m
total used free shared buff/cache available
Mem: 3935 1787 386 0 1762 1875
Note the increase in used memory from 1773MB to 1787MB.
I am trying to understand when does a module actually get loaded in memory? And why does an import django
command not load all of django modules in memory?