0

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?

0

2 Answers 2

8

In 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

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

as written, it seems like the JVM uses the import statement - I don't think there is any reference to these in the class file generated by the compiler. It is only a compilation thing
Yes, import only has meaning for the compiler. In the class file all class references use the fully qualified name (FQN). But using the FQN all the time in the source file would be extremely verbose and annoying, so the language provides the ability to "import" types. However, all the import really does is allow the compiler to see the simple name of the type and replace it with the FQN specified by said import.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.