I imported some packages in a python program. But a lot of them only be called at the beginning of the program process. Are those packages saved in memory until the whole program finishes running? Can I delete them after they been called to release memory?
-
have you tried `del`? – Igor Rivin Jun 30 '20 at 03:48
-
1as @IgorRivin said, use something like `del np` – Derek Eden Jun 30 '20 at 03:53
-
https://stackoverflow.com/questions/32234156/how-to-unimport-a-python-module-which-is-already-imported – Pramote Kuacharoen Jun 30 '20 at 03:53
-
2@DerekEden, `del np` isn't going to save any memory. That only gets rid of a name - the module is still being referred to by `sys.modules`, and possibly also by references in other modules' namespaces. – jasonharper Jun 30 '20 at 04:10
-
1Seeing the memory management of CPython you should expect that even if you purge all references to the module, no memory is handed back to the OS. – Klaus D. Jun 30 '20 at 04:26
-
Using something like `import gc; gc.collect()` might help with garbage collection, although I'm unsure if it will work with imported packages. [gc — Garbage Collector interface](https://docs.python.org/3/library/gc.html) – bug_spray Jun 30 '20 at 05:13
1 Answers
I don't think it is possible - but even if it is, the memory gained by erasing a loaded module is usually minimal.
Running del module
in the code of some file will not remove it from memory: all loaded modules are available from the global sys.modules
dictionary. If you delete the module from sys.modules, you can gain some space, but I think Python will still hold references to at least the classes that were defined in the module, (even if you have no instances of, or other references to those classes in your code.)
all in all, most modules would get you a few kilobytes back. If you plan to "unload" modules that have a multi-megabyte impact in memory (think of Pandas/matplotlib/sqlalchemy), you will be even less likely to be successful as the complexity of these code bases mean they have a lot of internal cross references, and you'd need to delete everything they import internally from sys.modules.
If the setup is based on a multi-megabyte framework like these, and you are running on a very limited system (raspberry-class mini PC, with a few MB, for example) you could try to perform the initialisation in a subprocess, and kill that process when you are done - that would give you some guarantees the memory is freed up. But limited hardware apart you should simply not bother about it.

- 99,910
- 10
- 151
- 209