Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
0
HANDS-ON DEMO
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.
int sum = 0;
return x*x;
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.
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
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
TO DO: Add the following code into the for loop above:
int sum = 0;
• Click Run
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.
• Click Run
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.