Você está na página 1de 5

DEBUGGER & PROFILER IN NETBEANS 6.

0
HANDS-ON DEMO

Using the debugger:

Debugging is the process of examining your application for (runtime) errors. The process of
debugging is accomplished by setting breakpoints and watches in your code and running it in the
debugger. This enables you to execute your code line by line and have a closer look at the state of
your application (values of variables, call order, etc.) in order to discover any problems.

STEP 1: Write a simple application

• Create a new Java Application


• Under the main method, write the following code:

int sum = 0;

for (int i=0; i<100000; i++) {

sum = add(sum, square(i));

System.out.println("Sum is " + sum);

• Under the Main class, add the following methods:

private static int square(int x) {

return x*x;

private static int add(int a, int b) {

return a+b;

}
STEP 2: Set a breakpoint at the main method

A breakpoint is a flag in the source code that tells the debugger where to stop execution of the
program. When your program stops on a breakpoint, you can perform actions like examining the
value of variables and single-stepping through your program.

To set a breakpoint, Click in the left margin next to the line in the source code or put the insertion
point in the line and press Ctrl-F8.

STEP 3: Run the debugger by pressing Ctrl-F5

STEP 4: Step through your program

Once execution of your program is halted, you can step through your lines of code using the
following commands on the Debug menu or toolbar:

• Step Over (F8). Executes one source line. If the source line contains a call, executes the
entire routine without stepping through the individual instructions.
• Step Over Expression (Shift-F8). Executes one method call in an expression. If an expression
has multiple method calls, you can use Step Over Expression to step through an expression
and view the value of each method call in the expression in the Local Variables window. Each
time you use the Step Over Expression command, the debugger advances to the next
method call in the expression and the completed method call is underlined. Step Over
Expression behaves like Step Over when there are no additional method calls.
• Step Into (F7). Executes one source line. If the source line contains a call, the IDE stops just
before executing the first statement of the routine. You can also start a debugging session
with the Step Into command. Program execution stops on the first line after the main routine
before any changes have been made to the state of the program.
• Step Out (Ctrl-F7). Executes one source line. If the source line is part of a routine, executes
the remaining lines of the routine and returns control to the caller of the routine. The
completed method call is highlighted in the Source Editor.
STEP 5: View program information

To view variables and expressions, either:

• Use the local variables window


• Or move your mouse over the variable
• Or create a watch
o Select the variable or expression in the Source Editor, right-click, and choose New
Watch (Ctrl-Shift-F7).
The New Watch dialog box opens with the variable or expression entered in the text
field.
o Click OK.
The Watches window opens with the new watch selected.

EXTRA TASK: Use the call stack

The Call Stack window lists the sequence of calls made during the execution of the current thread.

By default, the Call Stack window opens automatically whenever you start a debugging session. To
view the call stack (in case you don’t see it), choose menu Window – Debugging – Call Stack or press
Alt + Shift + 3
Using the profiler

Profiling is the process of examining an application in order to locate memory or performance-


related issues. When profiling a Java application, you can monitor the Java Virtual Machine (JVM)
and obtain data about application performance, including method timing, object allocation and
garbage collection. You can use this data to locate potential areas in your code that can be optimized
to improve performance.

TO DO: Add the following code into the for loop above:

int sum = 0;

for (int i=0; i<100000; i++) {

sum = add(sum, square(i));

Date date = new Date();

System.out.println("Sum is " + sum);

(Remember to fix imports errors before you continue)

TASK 1: Monitor the execution time (CPU Performance)

• Choose menu Profile – Profile Main Project


You might be asked to do some configuration for the first time of profiling

• Select CPU Tab

• Choose Entire Application

• Under Filter, select Profile only project classes

• Click Run

• When asked, click Yes to view the snapshot

A snapshot is a profiling data at a specific point of time. It can be reused for comparison purpose.
The result snapshot of CPU Profiling shows you the execution time of each methods and their
number of invocations

From the snapshot of the example here, you can see that the main method itself takes a lot of time
compared to the other two methods add and square. It’s totally up to you to justify whether it is a
reasonable result. Also notice from the snapshot that both add and square methods are called
100000 times.

TASK 2: Monitor the memory usage

• Choose menu Profile – Profile Main Project


You might be asked to do some configuration for the first time of profiling

• Select Memory Tab

• Choose Record object creation only

• Click Run

• When asked, click Yes to view the snapshot

Notice that 100000 objects of type Date has been created and they account for roughly 251832
Bytes in memory. This is because of the line we’ve recently added. We keep creating new object of
type Date without disposing it. This is one trivial cause of memory leak issues.

LE PHAN HUU BANG


SUN CAMPUS AMBASSADOR FOR NUS

Você também pode gostar