Does an unused import declaration eat memory, in Java?
Asked Answered
W

5

32

Does an unused import like so - import android.widget.RelativeLayout; eat memory? Just want to know about how much or just is it valuable? Maybe this is stupid question, but I haven't found answer.

Warrant answered 9/8, 2013 at 18:53 Comment(1)
Possible duplicate of Does unused import and objects have an performance impactGrosz
D
50

No they don't take any memory. Imports are just used by compiler to resolve class names at compile time.

Compiler changes each class name to fully qualified name. And removes the import statement. So, the import statement doesn't make it to byte code.

The only issue that can come up with wildcard import is namespace conflict, i.e., when two types with the same name is defined in two different packages, then importing those packages with wildcards will cause name conflict for that type used.


To see how compiler replaces the import statement, you can generate the byte code of your class using javap command. Consider the below code:

import java.util.*;
import java.util.regex.*;

public class Test {
    public static void main(String[] args) {

    }
}

Just compile the above code, and check the byte code using the following command:

javap Test

It gives out following output:

public class Test {
  public Test();
  public static void main(java.lang.String[]);
}

So, you can see that String type is replaced with it's fully qualified name java.lang.String, and there is no import statement in byte code.

Denote answered 9/8, 2013 at 18:54 Comment(2)
This answer makes sense to me, but I have had issues in my Java project that seem to contradict this. I can compile a class, run a program that can obfuscate some imports and their references to make it work with some other obfuscated code, then decompile that class and see some import statements for the old now-obfuscated classes. If those classes are never used in the code any more, how is the decompiler getting these import statements? Shouldn't they have been lost in compilation? (Should I post this as a new question?)Scarify
Never mind. Forget it. I believe the reobfuscator is just doing a half-job. I believe that's why the import statements are still there. The decompiler is probably writing in those import statements just to make it look nicer.Scarify
P
14

No, compiler removes them after compilation. But two issue you may face

  1. code clutter
  2. If you import some classes from jar and later removed jar but not imports, then you may get compile time error
Peccant answered 9/8, 2013 at 18:55 Comment(0)
S
2

Unused imports do not have a impact at runtime (because there are no imports in the byte code). However, unused imports will affect the compiler, but not by much.

In general, importing only what you need will lead to improved maintainability and readability of code.

Smarten answered 9/8, 2013 at 18:55 Comment(0)
C
2

No impact at runtime. It can make the compiling process a very tiny bit (unmeasurable) slower. But as far as having them, it's better to remove them because it makes the files smaller and it makes it easier to read what imports are actually being used.

Charisecharisma answered 9/8, 2013 at 18:56 Comment(1)
+1 for the self documenting aspect. That's always been my main reason for explicit imports of only classes that are used.Slink
D
0

No impact any rumtime-compiler time,

but best approach is to write the clearest and simplest code you can as this improves the maintainability of the code and helps ensure it performs reasonably well even after it is changed.

Documentation: Best Practices for Performance

Dennie answered 2/1, 2017 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.