SlideShare a Scribd company logo
1 of 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(quot;Sum is quot; + 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(quot;Sum is quot; + 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

More Related Content

Viewers also liked (8)

smurfit stone container 2004_AR
smurfit stone container  2004_ARsmurfit stone container  2004_AR
smurfit stone container 2004_AR
 
ameren InvestorMeetings_0308
ameren InvestorMeetings_0308ameren InvestorMeetings_0308
ameren InvestorMeetings_0308
 
smurfit stone container Q107_Release
smurfit stone container Q107_Releasesmurfit stone container Q107_Release
smurfit stone container Q107_Release
 
ameren Lehman_090507
ameren Lehman_090507ameren Lehman_090507
ameren Lehman_090507
 
smurfit stone container 1999_AR
smurfit stone container  1999_ARsmurfit stone container  1999_AR
smurfit stone container 1999_AR
 
Frukostseminarie 8 Maj
Frukostseminarie 8 MajFrukostseminarie 8 Maj
Frukostseminarie 8 Maj
 
HYP Presentation
HYP PresentationHYP Presentation
HYP Presentation
 
Bestemming Hnw@Amersfoort
Bestemming Hnw@AmersfoortBestemming Hnw@Amersfoort
Bestemming Hnw@Amersfoort
 

Similar to Debugger & Profiler in NetBeans

CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
keturahhazelhurst
 

Similar to Debugger & Profiler in NetBeans (20)

ID E's features
ID E's featuresID E's features
ID E's features
 
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docxCASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
CASE STUDY InternetExcel Exercises, page 434, textRecord your.docx
 
CIS 170 Focus Dreams/newtonhelp.com
CIS 170 Focus Dreams/newtonhelp.comCIS 170 Focus Dreams/newtonhelp.com
CIS 170 Focus Dreams/newtonhelp.com
 
CIS 170 Life of the Mind/newtonhelp.com   
CIS 170 Life of the Mind/newtonhelp.com   CIS 170 Life of the Mind/newtonhelp.com   
CIS 170 Life of the Mind/newtonhelp.com   
 
CIS 170 Imagine Your Future/newtonhelp.com   
CIS 170 Imagine Your Future/newtonhelp.com   CIS 170 Imagine Your Future/newtonhelp.com   
CIS 170 Imagine Your Future/newtonhelp.com   
 
Cis 170 Extraordinary Success/newtonhelp.com
Cis 170 Extraordinary Success/newtonhelp.com  Cis 170 Extraordinary Success/newtonhelp.com
Cis 170 Extraordinary Success/newtonhelp.com
 
CIS 170 Effective Communication - tutorialrank.com
CIS 170 Effective Communication - tutorialrank.comCIS 170 Effective Communication - tutorialrank.com
CIS 170 Effective Communication - tutorialrank.com
 
Devry cis 170 c i lab 1 of 7 getting started
Devry cis 170 c i lab 1 of 7 getting startedDevry cis 170 c i lab 1 of 7 getting started
Devry cis 170 c i lab 1 of 7 getting started
 
Devry cis 170 c i lab 1 of 7 getting started
Devry cis 170 c i lab 1 of 7 getting startedDevry cis 170 c i lab 1 of 7 getting started
Devry cis 170 c i lab 1 of 7 getting started
 
Cis 170 Effective Communication / snaptutorial.com
Cis 170 Effective Communication / snaptutorial.comCis 170 Effective Communication / snaptutorial.com
Cis 170 Effective Communication / snaptutorial.com
 
Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812Getting started with code composer studio v3.3 for tms320 f2812
Getting started with code composer studio v3.3 for tms320 f2812
 
CIS 170 Inspiring Innovation/tutorialrank.com
 CIS 170 Inspiring Innovation/tutorialrank.com CIS 170 Inspiring Innovation/tutorialrank.com
CIS 170 Inspiring Innovation/tutorialrank.com
 
Cis 170 c Enhance teaching / snaptutorial.com
Cis 170 c  Enhance teaching / snaptutorial.comCis 170 c  Enhance teaching / snaptutorial.com
Cis 170 c Enhance teaching / snaptutorial.com
 
Cis 170 Education Organization -- snaptutorial.com
Cis 170   Education Organization -- snaptutorial.comCis 170   Education Organization -- snaptutorial.com
Cis 170 Education Organization -- snaptutorial.com
 
CIS 170 Education Specialist / snaptutorial.com
CIS 170  Education Specialist / snaptutorial.comCIS 170  Education Specialist / snaptutorial.com
CIS 170 Education Specialist / snaptutorial.com
 
CIS 170 Exceptional Education - snaptutorial.com
CIS 170   Exceptional Education - snaptutorial.comCIS 170   Exceptional Education - snaptutorial.com
CIS 170 Exceptional Education - snaptutorial.com
 
Cis 170 Education Organization / snaptutorial.com
Cis 170 Education Organization / snaptutorial.comCis 170 Education Organization / snaptutorial.com
Cis 170 Education Organization / snaptutorial.com
 
ArduinoWorkshop2.pdf
ArduinoWorkshop2.pdfArduinoWorkshop2.pdf
ArduinoWorkshop2.pdf
 
Vb%20 tutorial
Vb%20 tutorialVb%20 tutorial
Vb%20 tutorial
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Debugger & Profiler in NetBeans

  • 1. 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(quot;Sum is quot; + 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; }
  • 2. 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.
  • 3. 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
  • 4. 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(quot;Sum is quot; + 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.
  • 5. 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