문제

I just installed the Unnecessary Code Detector for Eclipse and ran it on my project. I see a lot of so-called "dead code". Although, from an organizational standpoint, it makes sense to remove dead/unnecessary code, it got me thinking:

Does dead code actually hinder a Java app's performance?!?!

To me, if code is truly "dead", it never gets executed, so I don't see how removing it (again, except for organizational/housekeeping/code cleanup purposes) could ever improve performance.

도움이 되었습니까?

해결책 2

It could affect a few things...

  • Size of the application
  • Memory used when application is running
  • Decreased performance on package scanning (if applicable)

다른 팁

I don't think "dead code" will hinder application performance, but it will hinder development performance, which is invariably more expensive.

Where possible the JIT compiler may remove this kind of dead-code - see dead-code elimination. I suppose in theory if the JIT compiler removed huge amounts of dead-code it could impact initial compilation.

However I doubt this would occur in practice, and I'd only recommend dead-code removal to make development easier/faster.

It could affect it a bit, but the JIT compiler should be able to detect and eliminate methods that are never used. And even if it doesn't, the overheads (memory, load time, JIT compilation time, etc) are likely to be small.

A much better reason to eliminate dead methods is to get rid of "old stuff" that makes your codebase harder to read, test, maintain. If this is code that you might conceivably need again, you can always get it back from version control.


What if I ask the user which method do you want to call? , take the input as a String and then invoke that method using reflection?. The JIT can't say which method will be used so it can't remove any methods :).

Good point. So it probably won't eliminate methods in practice. (But it could ... if the classloader knew where to reload the method from ...)

Dead methods increases method area in JVM.

Yes, though the percentage memory increase is probably insignificant. And the consequent performance reduction is probably even less significant.

Also a method that is never called will never be JIT compiled, so you are likely to not incure 50% or more of memory usage for a typical live method.

So too much dead code miight lead to unloading of classes from the method area (heap) which could affect app performance. am I right?.

That is highly unlikely. A class will only be unloaded if nothing references it and its classloader is also unreachable. And if it does happen, the class would not be used again anyway, so it is right to unload it.

It could affect performance of your application.

Edit

One way to look at it is; dead code is going to add some extra memory to your running application. So it will impact application performance as well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top