I just wanted to ask whether all modules imported with an import statement in java and python are loaded into memory at runtime, even if they are not used in the program. I asked somebody and they told me in java, only the ones that are actually used are loaded into RAM at runtime and in python, all imported modules are always loaded into RAM at runtime whether you use them or not. Is this true? If yes, does this make python less efficient in memory usage at least in this sense (disregarding other aspects like garbage collection or the workings of the JVM, etc)?
Sorry for the superfluous words. Just want to be clear- The person I asked told me in java, only the ones that are actually used are loaded into RAM at runtime because during compilation the import statements only act as references to the actual packages, and the JVM loads them into memory only if the references are actually used, and in python, all imported modules are always loaded into RAM at runtime whether you use them or not (by loading they mean, that all definitions, functions and objects of that package/module are loaded into memory during runtime whether or not they are used), since they're not just references rather are actual instructions to load them into memory in python. I need guidance on this. Thanks.
Edit- I am aware that most programs only import the things they need but if suppose a program in java and one in python are there are both import 3 similarly sized packages but use only one of them, which one will use more memory solely on that basis?
I just wanted to ask whether all modules imported with an import statement in java and python are loaded into memory at runtime, even if they are not used in the program. I asked somebody and they told me in java, only the ones that are actually used are loaded into RAM at runtime and in python, all imported modules are always loaded into RAM at runtime whether you use them or not. Is this true? If yes, does this make python less efficient in memory usage at least in this sense (disregarding other aspects like garbage collection or the workings of the JVM, etc)?
Sorry for the superfluous words. Just want to be clear- The person I asked told me in java, only the ones that are actually used are loaded into RAM at runtime because during compilation the import statements only act as references to the actual packages, and the JVM loads them into memory only if the references are actually used, and in python, all imported modules are always loaded into RAM at runtime whether you use them or not (by loading they mean, that all definitions, functions and objects of that package/module are loaded into memory during runtime whether or not they are used), since they're not just references rather are actual instructions to load them into memory in python. I need guidance on this. Thanks.
Edit- I am aware that most programs only import the things they need but if suppose a program in java and one in python are there are both import 3 similarly sized packages but use only one of them, which one will use more memory solely on that basis?
Share Improve this question asked Mar 14 at 16:24 Gautam SahniGautam Sahni 11 silver badge1 bronze badge 1 |2 Answers
Reset to default 7In java, the import statements themselves do not load anything into memory. They are just references to classes or packages that the Java compiler and the JVM will use to resolve class names. The actual loading of classes happens during runtime when those classes are needed
- Chapter 5: JVM Specification
In python, when you import a module, the entire module is loaded into memory at runtime. This means that even if you import a module but never actually use it, Python will still load the module into memory, which includes all definitions, functions, and objects in that module.
- Official Python Import Docs
In terms of memory efficiency, this does make Python less efficient in this specific context, because Python will allocate memory for entire modules that might not even be needed.
About your last question Python will still load all 3 packages into memory, whereas Java will only load the package that you actually use.
Java does not load classes from import statements if they are not actually used in the code. Unused import statements in Java do not have a performance impact at runtime because they are not included in the bytecode.
Python on the other hand loads any imported module into memory regardless of whether the module is actually used or not. Also, regardless of how you import a module (import one function, one object, or whole module), the entire module is loaded into memory.
On the plus side, Python will only load the module once even if imported multiple times across multiple modules.
You can check which modules were imported and the order at which they were imported with the sys.modules variable.
Example:
import sys
import pandas as pd # this module is imported but never used
for m in sys.modules:
print(m)
print("number modules:", len(sys.modules))
Even though pandas is not actually used in the example above, the pandas module and all its dependencies will be loaded into memory, The number of modules loaded with pandas will be over 500 so it is a best practice not to import modules that are never used.
pylint will generate a unused-import warning if some code imports a module that is not used.
import
statement in Java just imports the namespace, that is, no module, package, or similar is imported, it is just to allow you to use the single name of classes (e.g.File
) in your code instead of having to use the full qualified name (java.io.File
) – user85421 Commented Mar 14 at 17:08