This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Performance and the fly likes How to analyze the performance of java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "How to analyze the performance of java program" Watch "How to analyze the performance of java program" New topic
Author

How to analyze the performance of java program

Eshwar Prasad
Ranch Hand

Joined: Mar 21, 2008
Posts: 202

How do I analyze and conclude the performance of simple java program in general like have i used the right datatypes, do the program executes in less time, would i suppose to use any other datatypes for better performance etc. Also, how do I track the execution time of java program.

Please excuse if this is more of basic question.

Kindly let me know your suggestions.
Kees Jan Koster
JavaMonitor Support
Rancher

Joined: Mar 31, 2009
Posts: 251
    
    5
Dear Eshwar,

Use a profiler to see where your application spends its time. Then optimise those methods that are either called a lot, or that take a lot of time to execute.

Most performance is gained from a good design, though. But that is so application-specific that we cannot help you with that.


Java-monitor, JVM monitoring made easy <- right here on Java Ranch
Toby Eggitt
Ranch Hand

Joined: Dec 08, 2004
Posts: 41
On this topic, what profiler tools do people recommend (for use inside Eclipse, specifically)?

I want to look at method timings, object instances, and total memory used by objects of any given class.

Thanks!
Toby
Zigfrid de Loewe
Greenhorn

Joined: Jun 23, 2009
Posts: 1
VisualVM and jhat (both free and installed tohether with the jvm) can give you the number of allocated instances.
For profiling method timings, I would not recommend visualvm or Netbeans or anything JVMTI/JVMPI/introspection based,because these profilers cause huge overheads and results are often bogus. My personal win is the StackProbe profiler - great accuracy, ease of use and low overheads, however it is free only for opensource projects.
Per Halvarsson
Greenhorn

Joined: Aug 14, 2008
Posts: 6
Toby Eggitt wrote:On this topic, what profiler tools do people recommend (for use inside Eclipse, specifically)?
I want to look at method timings, object instances, and total memory used by objects of any given class.
Thanks!
Toby

The Eclipse Memory Analyzer Tool is a very nice for tool analyzing the heap inside Eclipse.

Othervise you could try JRockit Mission Control. It will also give you statistics about the number of object instances on the heap and how much memory they occupy, both live using the Memory Leak Detector or stored in a file for offline analysis using the JRA-tool. The overhead for method profiling is very low, less than 1% or so, because the tool piggybacks on the hot spot detector the JVM uses. You can get the the Eclipse plugins from the update site. The tool can also give you cheap memory allocation profiling.

Both tools are free to use for development and you can jump to the souce code in your Eclipse projects.
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
YourKit profiler is my tool of choice - http://www.yourkit.com/
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I know this is a simplistic answer, but there are times when the easiest way to get the job done is to code a simple millisecond timer using Date methods, or maybe the Calendar interface. Place these at appropriate locations in your code, and you have a quick and easy way during development to measure improvements in algorithm speed, or check suspected bottlenecks, without having to mess around with learning a resource hungry performance evaluator.

p.s. I see this thread is a little old, but I thought I'd throw this in there.
Kees Jan Koster
JavaMonitor Support
Rancher

Joined: Mar 31, 2009
Posts: 251
    
    5
Dear Fred,

I agree and that is what I use mostly. Or rather, I mainly use a mish-mash of the tool-du-jour and print statements that do simple timing. Tools tend to find stuff you had never though of, and print statements serve to track problems that I know are there.
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
Fred Hamilton - I can't agree it's the best way to go, tweaking existing code to add the 'statistics gathering' by hand - I thought that's what the tools were for ? But hey, I might be wrong ;)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Lucas Lech wrote:Fred Hamilton - I can't agree it's the best way to go, tweaking existing code to add the 'statistics gathering' by hand - I thought that's what the tools were for ? But hey, I might be wrong ;)


Fair enough, but notice I said "sometimes" and I was speaking more of the development phase, looking for ways to refine algorithms to improve performance. And it depends how advanced of a programmer you are too. And I wasn't really talking about statistics gathering,unless you want to call putting a simple timer on a method call statistics gathering.

But yeah, once code is nailed down, their ain't much point in my suggestion. And if your performance analysis needs are complex, then I imagine my idea would fall short also.

regards.
Maximus Moothiringus
Ranch Hand

Joined: Jun 07, 2008
Posts: 46

I would like to bring up this topic again.

I understand that it might be easier in my case to put in the print lines as I want to check the performance of my algorithms just like that.

Here's what I would like in short. I couldn't make much out from the above conversation.

How do I calculate the running time of my program.
How do I calculate the memory used by my program.

As of now I am using simple java programs. Might go in to bigger ones as I learn more. If possible, I would like a free software for the same as I am still not earning.

The craze got in me to find out the running times after participating in some online contests. They give some times with milli-second precision. If i could calculate, it would be great.

I saw the profiler in netbeans, but it always says 0 secs.

Max


-- Maximus Moothiringus
Preparing for SCJA!!
Finally figured out that giving <br/> splits the profile biography but not the signature
Kees Jan Koster
JavaMonitor Support
Rancher

Joined: Mar 31, 2009
Posts: 251
    
    5
Dear Max,

Check these demo's: http://www.yourkit.com/docs/

Kees Jan
Maximus Moothiringus
Ranch Hand

Joined: Jun 07, 2008
Posts: 46

Seems to do the work. But seems like I can get only a 15day evaluation.

Will there be a free alternative to do this.

Or atleast some method with which I can find out just how much time my program takes to execute and how much memory it uses.

Max
Kees Jan Koster
JavaMonitor Support
Rancher

Joined: Mar 31, 2009
Posts: 251
    
    5
Dear Max,

There are some, like the netbeans profiler and project kato and the oktech profiler.
Maximus Moothiringus
Ranch Hand

Joined: Jun 07, 2008
Posts: 46

Ok, Will check.

Thank you very much.
Max
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Hi,
you can use jvmstat+visualgc together to see the garbage collection of your application so you can find if there is a memory leak in your application.
Eclipse MAT can only show you the analysis by reading a memory dump file. I have blogged about how to set up jvmstat+visualgc if you are interested here

Regards,
Dinuka


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to analyze the performance of java program
 
Similar Threads
any segestions or codes
costing
costing
String Object
seeking help for build the java metric analyzer