Reputation: 1452
I want to optimize my Java source code, but I am not sure what parts of my code may be optimized. Is there any tool or any technique through which I can optimize my Java source code?
I know the basic fundamentals of code optimization, but I have a large number of lines and very complex source code (Java classes).
Can I optimize my code with the help of a Java profiler?
Thanks in Advance!
Upvotes: 4
Views: 5069
Reputation: 49351
Their are several tools for static code analyzes (to do code style / code convention / best practises / bug "optimization" of your code).
I would recommend using Sonar. It covers them all and is easy to setup - even on a local machine (unzip and start). It is best used with maven projects but also possible for NON maven projects.
Upvotes: 0
Reputation: 533750
What do you mean by optimising your code? DO you want to refactor your code to simplify it. In that case I suggest you use your IDE to help refactor the code.
If you want to minimise its memory / CPU consumption, I would suggest you use a profiler like VisualVM or YourKit to idenify where resources are being consumed.
A code analysis tool can also help pick up the obvious performance issues. I have code analysis on as I type in my IDE which helps me pick up those issues as I write it.
Upvotes: 3
Reputation: 114817
Performance optimization - yes, a profiler may help. At least it can show you those areas in your application that take an unexpected amount of CPU time.
But before starting to apply changes - take care not to do some microoptimization. Look at improving algorithms first. Sometimes we use nested for loops while a task can be done with a single one (linear time). Double check if you use the correct collection types. Then have a look if you accidentally create more objetcs than needed (object creation in loops is a typical reason for performance problems).
Upvotes: 3