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

smurfit stone container 2004_AR
smurfit stone container  2004_ARsmurfit stone container  2004_AR
smurfit stone container 2004_ARfinance30
 
ameren InvestorMeetings_0308
ameren InvestorMeetings_0308ameren InvestorMeetings_0308
ameren InvestorMeetings_0308finance30
 
smurfit stone container Q107_Release
smurfit stone container Q107_Releasesmurfit stone container Q107_Release
smurfit stone container Q107_Releasefinance30
 
ameren Lehman_090507
ameren Lehman_090507ameren Lehman_090507
ameren Lehman_090507finance30
 
smurfit stone container 1999_AR
smurfit stone container  1999_ARsmurfit stone container  1999_AR
smurfit stone container 1999_ARfinance30
 
Frukostseminarie 8 Maj
Frukostseminarie 8 MajFrukostseminarie 8 Maj
Frukostseminarie 8 Majwickedone
 
Bestemming Hnw@Amersfoort
Bestemming Hnw@AmersfoortBestemming Hnw@Amersfoort
Bestemming Hnw@AmersfoortRoyVermeulen
 

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.docxketurahhazelhurst
 
CIS 170 Focus Dreams/newtonhelp.com
CIS 170 Focus Dreams/newtonhelp.comCIS 170 Focus Dreams/newtonhelp.com
CIS 170 Focus Dreams/newtonhelp.combellflower82
 
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   llflowe
 
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   bellflower42
 
Cis 170 Extraordinary Success/newtonhelp.com
Cis 170 Extraordinary Success/newtonhelp.com  Cis 170 Extraordinary Success/newtonhelp.com
Cis 170 Extraordinary Success/newtonhelp.com amaranthbeg143
 
CIS 170 Effective Communication - tutorialrank.com
CIS 170 Effective Communication - tutorialrank.comCIS 170 Effective Communication - tutorialrank.com
CIS 170 Effective Communication - tutorialrank.comBartholomew19
 
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 startedshyaminfo04
 
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 startedash52393
 
Cis 170 Effective Communication / snaptutorial.com
Cis 170 Effective Communication / snaptutorial.comCis 170 Effective Communication / snaptutorial.com
Cis 170 Effective Communication / snaptutorial.comBaileyao
 
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 f2812Pantech ProLabs India Pvt Ltd
 
CIS 170 Inspiring Innovation/tutorialrank.com
 CIS 170 Inspiring Innovation/tutorialrank.com CIS 170 Inspiring Innovation/tutorialrank.com
CIS 170 Inspiring Innovation/tutorialrank.comjonhson110
 
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.comHarrisGeorg51
 
Cis 170 Education Organization -- snaptutorial.com
Cis 170   Education Organization -- snaptutorial.comCis 170   Education Organization -- snaptutorial.com
Cis 170 Education Organization -- snaptutorial.comDavisMurphyB99
 
CIS 170 Education Specialist / snaptutorial.com
CIS 170  Education Specialist / snaptutorial.comCIS 170  Education Specialist / snaptutorial.com
CIS 170 Education Specialist / snaptutorial.comMcdonaldRyan138
 
CIS 170 Exceptional Education - snaptutorial.com
CIS 170   Exceptional Education - snaptutorial.comCIS 170   Exceptional Education - snaptutorial.com
CIS 170 Exceptional Education - snaptutorial.comDavisMurphyB33
 
Cis 170 Education Organization / snaptutorial.com
Cis 170 Education Organization / snaptutorial.comCis 170 Education Organization / snaptutorial.com
Cis 170 Education Organization / snaptutorial.comBaileya126
 

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

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

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