Skip to content, Skip to search


Talk:Debugging Exercises

4,084 bytes removed, 09:19, 11 December 2015
Move E8 and E9 to main page
==Exercise 8: Profiling==
* Identifying bottleneck functions in a code segment
Debugging often involves finding the function which performs poorly - in terms of computation time and/or memory resources used. For the code to perform as desired , fixing the poorly performing function may often lead to a simple solution.
Exercise 8 in pretty straightforward with the main function calling two functions - doStuff() and doMoreStuff() . Both functions are called one after the other for two minutes. Our goal is to find how much time is taken by each function in the two minute run. One solution a programmer may think of is to keep track of time taken by functions in some flag variables using System time, but this method does not scale up for many function calls. Since the tutorial is for "debugging", we will not making any changes to code but will be using a profiling tool - Jvisualvm [] which is a bare bones profiler with basic functionalities and is freely available.
'''Steps for exercise'''
# Insert a breakpoint on the <code>while</code> statement in main method
# Launch JvisualVM. All running java applications are shown in the in Applications Tab (left margin) of JvisualVM. Double Click to select E8PerceivingPerformance. Go to the profiler tab and click the CPU option. '''Note''' Click on the settings checkbox on upper right corner of Jvisualvm. In CPU settings , make sure the class being profiled is net.imagej.trouble.** instead of net.imagej.trouble.visible.** - [[File:E8Settings.png|center|400px|Settings change]]
# Switch to Eclipse and resume the execution of code
# Wait for the stipulated time , twiddle your thumbs. Or you can switch to JvisualVM and see how much time is taken by each of function in real time.
# Example output
[[File:E8ProfilingResults.PNG|center|400px|Profiling Results]]
|Which process takes more time ? doStuff or doMoreStuff ?
|Answer -'''doStuff'''. doStuff() takes 954 ms while doMoreStuff() takes 710 ms.
|Are the number of invocations/function calls of both functions same ?
|Yes. both are called 83,573,448 times.
'''Take Home Point'''
# Use a profiler to find the function which takes more time.
==Exercise 9: Multiple threads==
Developing codes may often involve using multiple threads instead of sequential execution of statements. New threads may be spawned anytime when required. Essentially, threads should be used to run independent processes but if threads work on the same process there is no guarantee of execution of statements in different threads in a particular order.
E9 exercise on multiple threads focuses on this issue and also highlight an additional property of breakpoint that can be helpful in debugging program (Stop Virtual Machine).
Even though no code changes, sometimes debugging affects code execution. The Exercise 9 creates two parallel processes which are interdependent on each other. Each process throws up an error if the other process introduces a delay more than one second. So pausing for more than one second ,while debugging the program throws up an exception. So the act of debugging introduces errors in this case.
'''Steps for the exercise'''
* Run E9 with no breakpoint. Note that it works fine
* Now set a breakpoint in the “getName” method of either the Even or Odd LonelyRunnable
* Set the breakpoints property to “Suspend thread”
* Debug E9. After hitting the breakpoint, wait for a few seconds. Eventually you should see that a different thread is paused and “Expecting Exception”.
* resume the threads and see the stack trace.
* Now modify the breakpoint and tell it to suspend VM instead of just the thread.
* Debug again. When you hit a breakpoint, make sure you resume the VM and not just the thread.
'''Takehome point'''
* In case of multithreaded programming, stop the virtual machine at the breakpoint to freeze the state of all threads.
[[File:E9StopVM.PNG|center|400px|thumb|Setting a breakpoint on an exception]]
[[User:Hinerm|Hinerm]] ([[User talk:Hinerm|talk]])
== Exercise 2 - Ellen's Questions ==
Bureaucrat, emailconfirmed, incoming, administrator, uploaders