Tips for Using IntelliJ Efficiently — Debugging Like a Pro | by Ivelina Yordanova | Mar, 2022

Ideas and tips I discovered and use each day

Picture by Douglas Lopes on Unsplash

You’d be stunned by the quantity of individuals that also use print statements within the code to debug and people would possibly do the job however gained’t get you far sufficient when it’s good to debug one thing extra complicated or in case you purpose to be extra environment friendly.

Typically, that has nothing to do with the extent of expertise or data however with placing in the time and effort to discover the options of the IDE of selection. So, I’m gonna share some cool tips you might need missed up to now and will find yourself saving you a number of time and time is cash (both yours or your organization’s).

Line breakpoint.. with a spin

I’m not gonna blow your thoughts by telling you which you can add a line breakpoint by clicking on the left bar on that line. Everybody that’s ever debugged one thing in any IDE is aware of this

I’m gonna concentrate on one thing extra fascinating

Break on situation

A proper click on on an already positioned breakpoint would open a pop-up window with a couple of choices.

One of many issues I exploit mostly is the Situation part, the place you’ll be able to put an expression evaluating to boolean utilizing the variables in the scope of the road and the debugger would solely break if the situation is true.

Suspending single thread solely

By default, within the pop up you’ll see the All radio button chosen. Nevertheless, in case you select the Thread one, you’ll be able to droop solely the thread executing the road of code the place the breakpoint is.

That is normally helpful when you may have threads, which, if suspended, would possibly throw exceptions and die, like threads consuming from Kafka or querying a database with a timeout. Having just one thread cease would possibly enable you to hold the app operating and wholesome when you look at the little bit of code that may be problematic.

Print debug statements… on steroids

Bear in mind how I stated print statements usually are not nice. Effectively, I meant these ones you place within the code. They aren’t excellent as a result of it’s good to restart the app each time it’s good to edit or add prints and, what’s worse, you would possibly by accident commit these and both expose delicate data or simply trigger a number of spam within the logs.

So, the choice is…..drum roll.. print utilizing the choices IntelliJ gives.

Within the default pop up, by default the Droop checkbox is chosen. Should you deselect it, the window will broaden revealing an entire lot extra choices. A type of is Consider and log which signifies that each time the debugger would usually droop (regardless of if it’s a traditional breakpoint, with a situation, suspending one or all threads) it would print regardless of the assertion in that textbox evaluates to as a substitute. It could possibly both be a string literal or something utilizing the variables within the scope.

Typically, although, you can not pinpoint the precise line inflicting the issue. For instance in multithreaded purposes exceptions are being swallowed and seem as negative effects some other place or you aren’t certain the place the exception is thrown from as a result of lack of correct logging otherwise you simply must strategy the issue extra typically. In these circumstances, you’ll be able to add a breakpoint exception.

Should you hit Ctrl + Shift + F8 | Cmd + Shift +F8 you’ll see this window itemizing all presently added breakpoints.

Moreover, you’ll be able to both add a breakpoint for Any Exception for the present undertaking’s language or a particular exception class.

Understand that typically frameworks like Spring might need inside exceptions on startup so it’s a bit annoying to start out up your app with that type of exception and get suspended in locations you aren’t serious about.

Additionally, as you’ll be able to see in the appropriate panel, these breakpoints additionally comply with the identical guidelines and supply the identical choices as the road ones.

As soon as the execution is suspended there are few issues within the Debugger pane I need to level out.

Variables

That is the tab most acquainted to individuals — in addition to hovering over variables in scope, you’ll be able to overview their values on the time within the variables pane.

Frames

Barely much less acquainted tab is the frames one.

The fascinating bit right here is just not solely that you just see the stacktrace (what was the app’s path to this line of code) but additionally which you can really journey again in time. You may click on on the person frames and overview the values of the variables in that scope on the time the code went via it. Which is extremely helpful when monitoring down sudden values.

Threads

The final tab right here is the Threads one. It gives a option to test the place all the opposite threads are (if they’re additionally suspended) at that second.

Typically, sadly, a breakpoint is just not sufficient and it’s good to use some extra critical instruments. Largely these are instruments used to detect reminiscence leaks and excessive CPU utilization. The Profiler in IntelliJ gives a number of the functionalities that beforehand we wanted exterior instruments for.

You may open the Profiler pane via the View Menu|Device Home windows|Profiler or it’d already be seen someplace within the left|backside|proper corners. If there’s a Java course of operating it would seem there.

The fascinating bits are hidden within the context menu.

CPU and Reminiscence Reside Charts

It will present you a timeline graph of the reminiscence and CPU utilization of the app whereas it’s operating.

It may be very helpful if you wish to gauge the place the app is at when it comes to useful resource utilization or detect any irregular spikes or drops in both of these.

Seize Reminiscence Snapshot

A reminiscence snap is a step additional or a complimentary step to the dwell charts. You may see the precise objects and primitives filling up that reminiscence and this would possibly enable you to establish the place a leak is coming from.

The opposite 2 choices I’ve used approach much less to be sincere however will point out rapidly

Attaching profiler

Attaching a profiler or operating the app with a profiler offers you a number of data like an inventory of all strategies executed whereas the profiler was operating, all occasions that occured (comparable to GC, reminiscence alloc and many others) and extra.

Get Thread Dump

It will print the stacktrace for all threads within the app at the moment.

Hope you discovered some new bits right here that you just’ll go on to make use of each day. IntelliJ does quite a bit for you so don’t be afraid to click on round.

If you wish to be taught extra about save time and keep away from errors take a look at my earlier article — 10 IntelliJ Shortcuts For Efficient Refactoring.

More Posts