Laguh
Laguh

Reputation: 673

How to count breakpoint hits using IntelliJ during debugging

I need to count the number of times a given line of code is hit during execution. I want to use it via debugging tool on my IDE and not a code or performance tool solution.

I am already using YourKit to profile the project, and do not want number of line calls via this tool.

I also do not want to count hits using code (with System.out.print and a variable) because I will be checking the procedure at different branches.

I already checked IntelliJ IDEA help.

Also checked both this posts: Debugging and counting breakpoint hits Counting breakpoint hits

But none of them replies to what I am looking for.

I am currently using IntelliJ's breakpoint options: Log to console .

this code snippet is just an example and not the actual code I will use it for

The way I am using the breakpoint option in IntelliJ I get a log message every time the breakpoint is hit, and then I have to count the number of messages.

enter image description here I would like to get an actual number of hits so I do not have to count messages.

If anyone knows a simple direct solution to achieving this via IntelliJ I really appreciate. Thanks in advance.

Upvotes: 11

Views: 5623

Answers (4)

DarthGizka
DarthGizka

Reputation: 4675

Like ABika said, enabling the debugger pane Overhead view ...

enable debugger Overhead view

... lets you see the hit counts for your breakpoints.

However, in scenarios like this it can often be useful to set breakpoints to be non-stopping if they are merely intended for counting. Simply right-click a breakpoint or select it in the Breakpoints list and remove the tick mark next to Suspend:

Suspend check box in breakpoint options

This makes the breakpoint turn yellow:

breakpoint turned yellow

Then you can run your code in debug mode without having to stop even a single time unless you want to.

Unlike the Variables or Frames views, the Overhead view remains available even after debugging has ended:

breakpoint hit count in Overhead view

This can be a real lifesaver if a breakpoint can get hit hundreds or thousands of times. The configurable breakpoint conditions and filters can also be extremely useful in such scenarios.

Upvotes: 2

ABika
ABika

Reputation: 677

The Overhead tab in the Debugger view has a "Hits" column. You don't even have to have Suspend or Log to Console being enabled for the breakpoint to be counted. See the help page Monitor debugger overhead.

Overhead tab in Intellij IDEA  Debugger

Upvotes: 13

bb1950328
bb1950328

Reputation: 1599

The approach of DaSqy Stc is good, but it doesn't work when there are other log messages are printed or if you want to track multiple breakpoints individually.

Solution is to keep the option "log to console" on and click inside the console, press [Ctrl]+[F] and search after "Breakpoint reached at RemoveFromList.remove(RemoveFromList.java:38)" and it says 1/6. The right number is the one you want.

Upvotes: 1

SDJSK
SDJSK

Reputation: 1326

enter image description here

enter image description here

Scroll to the end, and watch the line number

Upvotes: 5

Related Questions