NBDemoProfiler55

NetBeans Profiler 5.5 Demo (10 Minutes)

Description

Goal is to introduce the Profiler and show some of the features that distinguish it from the competition.

Product Versions Supported

Tested on 5.5, but should also work with 5.0. Requires JDK 5, update 4 or higher. Has not been tested with JDK 6, but should work.

Points to Hit

  1. Started out as a research project at Sun Labs: Is there a better way to profile Java applications, in particular, large applications? Once the concepts were proven, the technology was moved into NetBeans.
  2. Traditional Java profiling tools provide hard-to-tune filters that make it difficult to easily specify exact portions of your application. End result: too much profiling overhead, and too much information to wade through.
  3. The NetBeans Profiler solves these problems by allowing you to easily specify which parts of your program should be profiled. Everything else runs at full speed.

Demo Prep

  1. Make sure the NetBeans IDE is running with 384mb of heap. To set the value, edit the -Xmx value in the etc/netbeans.conf file of your IDE installation directory.
  2. Install the Profiler.
  3. Open up the attached project, which contains the source code for a brain-dead little application that calculates prime numbers. It uses a SwingWorker to do the actual work. It is not as efficient as it could be, and it has a memory leak.
  4. Profile the attached project at least once, so that the Profiler can add the Ant targets that it needs to the build.xml. Since that is a one-time operation, this will avoid having that dialog pop up during your demo.
  5. Make sure PrimeNumbers is the Main Project.

Gotchas

Profiler 5.5 Beta2 has a bug with its "Live Results" window. The IZ is 70301; it is fixed as of the Sep 11 daily build. This demo script has been designed to avoid the bug, but the workaround is not always successful, so if you happen to see an ArrayIndexOutOfBounds or NullPointerException, just close it and keep going ("Well, ya' know this is a beta.") smile You could also run NetBeans with the following -J-DErrorManager.minimum=66000. This will supress all error messages :-). Or use a daily build that has the fix.

Demo:

CPU Profiling - The Painful Way

  1. Select Profile > Profile Main Project
  2. Click the large Analyze Performance button
  3. Select the Entire Application option
  4. Click the Run button
  5. The application's window opens in the upper-left of the screen - make sure the IDE is not covering it up.
  6. Suggested Comment: "I have just started profiling this entire application. Let's see what information is available so far...."
  7. Click the Live Results icon over in the Profiler Control Panel. The Live Results window opens.
  8. In the application, type in 1000000 (that is a 1 followed by 6 zeroes or if you prefer "1 million dollars") and then click the Calculate Prime Numbers button.
  9. The Profiling Results window updates.
  10. Suggested Comment: "This is great and everything, but look at all the data I have to wade through...."
  11. Scroll the Live Results window
  12. Suggested Comment: "There is a better way to do this, and it imposes less profiling overhead!"
  13. IMPORTANT: Close the Live Results window
  14. And then proceed to the next demo....

CPU Profiling - The Right Way

  1. At this point, you have an option. The goal is to set a profiling root method. There are multiple ways to do this. You can use the Projects window: expand the PrimeNumbers project until the construct() method in the PrimeCalculator.java file is visible, then Right-Click it and choose Tools > Add As Profiling Root Method OR you can just open PrimeCalculator.java and then with the cursor anywhere in the body of the construct() method, choose (from the IDE's main menu - No context menu option? Lame. Is there an RFE for this?): Tools > Add As Profiling Root Method.
  2. Regardless of how you do the previous step, in the Select Settings Configuration dialog, click on the first entry in the list: Preset: Part of Application. Then click OK. The Profiler allows multiple configurations, by choosing Preset: Part of Application you are using the default configuration.
  3. Suggested comment: "One of the nice features of the NetBeans Profiler is its tight integration with the rest of the IDE. I have just selected a root method, which is where I want the Profiler to begin profiling. Without stopping the application, let's change the profiling settings...."
  4. Select Profile > Modify Profiling.
  5. Select the Part of Application option.
  6. Suggested Comment: "The Profiler knows which method I want to profile, because I just selected it from within the project."
  7. Optional: Click the Select button and show the folks that the correct method is selected (construct()).
  8. Click the OK button. There will be a pause while the instrumentation is redone....
  9. Suggested Comment: "I've changed the settings so that the Profiler is only going to do profiling of one method and all the methods that it calls. So in other words, the Profiler figures out the call graph and just profiles those methods - the rest of the application runs at full speed. And I don't have to define any filters!"
  10. Click the Calculate Prime Numbers button on the PrimeNumbers application
  11. Suggested Comment: "The performance difference is not that noticeable on this test, but it would be if I used a larger number and we ran several tests. But we don't have that much time...."
  12. After the prime number is reported, go back to the IDE.
  13. Click the Take Snapshot button in the Profiler Control Panel
  14. Suggested comment: "Ahh... this is more like it. No need to wade through large amounts of unnecessary data. I can easily see the method that I am interested in and how much time it spent in its own code ("Self Time") and how much time it spent in the methods that it called. Looks like it is spending a lot of time in this persist() method - that deserves more investigation."
  15. Show how you can Go To Source. A big benefit of having a profiler integrated with the IDE.
  16. Expand the entry for the persist() method.
  17. Continue to the next demo.

Tracking Down a Memory Leak

Note: Steps 2 through 11 are optional

  1. Suggested comment: "The NetBeans Profiler can also help you track down memory leaks. Let's take a look at what is happening on the heap."
  2. Select Profile > View > Telemetry Overview (or click the equivalent icon in the Profiler Control Panel).
  3. The Telemetry Overview opens in the Output pane. Maximize it so it will be easier to see the drop in heap from the garbage collection you're about to perform (the purple graph will get smaller and the surviving generations will increment by one).
  4. Suggested comment: "The Profiler can force a garbage collection."
  5. Choose Profile > Run GC
  6. Suggested comment: "Let's see what is happening on the heap as this application runs"
  7. In the application, click the Calculate Prime Numbers button three times (with appropriate pauses between each, it takes a second or two for each run)
  8. Suggested comment: "This application looks like it might have a memory leak. Let's run the garbage collector again."
  9. Select Profile > Run GC
  10. The heap doesn't get much smaller (the purple graph only drops a bit)
  11. Suggested comment: "Let's change the profiling settings again to do memory profiling and see what that shows us."
  12. Select Profile > Modify Profiling
  13. Click on the big Analyze Memory Usage button
  14. Select both Record Object Creation only and Record Stack Trace for Allocations
  15. IMPORTANT: Make sure the Track Every value is set to 1, not the default value of 10.
  16. Click the OK button
  17. After the instrumentation is changed, click the Calculate Prime Numbers button on the application
  18. After the prime number is calculated, go back to the IDE and click the Live Results button in the Profiler Control Panel
  19. Switch back to the application and leave it on top of the upper-left corner of the IDE
  20. Click the Calculate Prime Numbers button three more times
  21. The entry for int[[ | ]] should be getting pretty large by now (if you have been using 1000000)
  22. Suggested comment: "This looks interesting - this application sure is holding onto lots of int arrays. Let's take a look."
  23. Right-click the entry for int[[ | ]] and choose Take Snapshot and Show Allocation Stack Traces
  24. Suggested comment: "Our old friend construct() seems to be the culprit. Let's go look at the source."
  25. Right-click the entry for construct() and choose Go To Source
  26. Optional: Point out that on the third line of the method, an int[[ | ]] is being allocated, it turns out this is array is later added to a hash map and then never removed, leading to the memory leak.

Demo Cleanup

None.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo