2

I am a bit new to profiling applications for improving performance. I have selected YourKit as my profiler. There is no doubt that YourKit provides very interesting statistics. Where I am getting stuck is what to do with these statistics.

For instance, Consider a method that operates on a JAXB POJO. The method iterates through the POJO to access a tag/element that is deeply nested inside the XML. This requires 4 layers of for loops to get to the element/tag as shown below :

   List<Bundle> bundles = null;
   List<Item> items = null;
   for(Info info : data) { 
      bundles = info.getBundles();
      for(Bundle bundle : bundles) {
         items = bundle.getItems();
         //.. more loops like this till we get to the required element
       }          
    }

YourKit tells me that the above code is a 'hot-spot' and 80 objects are getting garbage collected for each call to the method containing this code. The above code is just an example and not the only part where I am getting stuck. Most of the times I have no clue about what to do with the information given by the profiler. What can I possibly do to reduce the number of temporary objects in the above code? Are there any well defined principles for imporoving the performance of an application? What statistics to look for when profiling an application and what implications does each kind of statistics have?

Edit : The main objective for profiling the application is to increase the throughput and response time. The current throughput is only 10 percent of the required throughput!

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82

3 Answers3

1

Focus on the statistics relevant to your performance goal. You are interested in minimal response time, so look at how much each method contributes to response time, and focus on those that contribute much (for single threaded processing, that's simply elapsed time during method call, summed over all invocation of that method). I am not sure what YourKit defines as hot spots (check the docs), but it's probably the methods with highest cummulative elapsed time, so hot spots are a good thing to look at. In constrast, object allocation has no direct impact on response time, and is irrelavant in your case (unless you have identified that the garbage collector contributes a significant proportion of cpu time, which it usually doesn't).

meriton
  • 68,356
  • 14
  • 108
  • 175
1

I absolutely agree with the given answers.

I would like to add that considering your concrete example, you actually can make an improvement by using xpath api to access the specific location in the XML.

In situations where you don't need to actually iterate the entire DOM, this should be your first choice since it is declarative and hence more expressive and less error prone.

It would often give you superior performance as well (For very complex queries it may not be the case, but you seem to have a simple scenario).

Vitaliy
  • 8,044
  • 7
  • 38
  • 66
  • We don't directly access the XML. JAXB unmarshals the XML received by our Apache CXF Web services into POJOs. We receive the POJO as a parameter to our web-service endpoints. In short, can xpath be plugged into JAXB such that the generated POJOs contain methods to use XPATH? – Chetan Kinger Sep 13 '12 at 19:18
  • Also note that traversing through the entire DOM is necessary in many cases where the attributes of each tag traversed are required by the application before picking up the information from the leaf tag – Chetan Kinger Sep 13 '12 at 19:28
  • I am not aware of a technique that would allow you to plug xPath into JAXB. In fact, it would contradict the very purpose of the technology. I am sorry, but I overlooked the fact that you are dealing with a JAXB pojo and do not have direct access to the Xml. However, as others have stated, the creation and collection of 80 objects is nothing to the JVM. As to your general question, it may be that you are looking at the wrong place. How did you conclude that this particular piece of code is the one taking the most amount of time? – Vitaliy Sep 13 '12 at 19:47
  • YourKit has something called a Hot Spot which gives you the methods that had a large number of temporary objects created and garbage collected. This particular method showed up in the hot spots. YourKit also gives hot spots for CPU profiling. The same method showed up in the CPU profiling hot spots as well. Although it executed for only 20 ms! – Chetan Kinger Sep 14 '12 at 05:31
0

A way to improve the loop would be to change your schema and essentially flatten the model, of course this depends on whether you can change the schema. This way the generated Java will not require 4 layers of looping. Of course at the end of the day you need to ask yourself is the code really a problem - 80 objects are getting GCed so? Is your application running slow? Are you experiencing memory issues? Remember premature optimization is the root of all evil!

Profiling and optimization is a complex beast and depends on may things (Java version, 32 vs 64 bit os, etc...). Furthermore the optimization might not always require code changes, for example you could resolve problems by changing your GC policy on the JVM - for example there are GC policies that are more effective in situations where your code is creating many small objects that need to be GCed frequently. If you had specifics maybe it would be easier to help you however your question seems too broad. In fact there are many books written on topic which might be worth a read.

Rorick
  • 8,857
  • 3
  • 32
  • 37
ramsinb
  • 1,985
  • 12
  • 17