search
HomeJavajavaTutorialDebug Non-Responsive Applications

Read in other languages: English Português 中文

There are many debugger tutorials that teach you how to set line breakpoints, log values, or evaluate expressions. While this knowledge alone gives you many tools to debug your application, real-world scenarios can be a little more complicated and require a more advanced approach.

In this article, we will learn how to locate the code that causes a UI crash without much prior knowledge of the project and fix the broken code on the fly.

The problem

If you want to follow the example, start by cloning this repository: https://github.com/flounder4130/debugger-example

Suppose you have a complex application that crashes when you perform some action. You know how to reproduce the error, but the difficulty is that you don't know which part of the code is responsible for this functionality.

Depurar Aplicaciones No Responsivas

In our example app, the crash occurs when you click the Button N. However, it is not so easy to find the code that is responsible for this action:

Depurar Aplicaciones No Responsivas

Let's see how we can use the debugger to find it.

Method breakpoints

The advantage of method breakpoints over line breakpoints is that they can be used in entire class hierarchies. How is this useful in our case?

If you look at the example project, you will see that all action classes are derived from the Action interface with a single method: perform().

Depurar Aplicaciones No Responsivas

Setting a method breakpoint on this interface method will suspend the application every time one of the derived methods is called. To set a method breakpoint, click the line that declares the method.

Start the debugging session and click the Button N. The application is suspended on ActionImpl14. Now we know where the code corresponding to this button is located.

Depurar Aplicaciones No Responsivas

Although in this article we are focused on finding the bug, this technique can also save you a lot of time when you want to understand how something works in a large codebase.

Pause application

The approach with method breakpoints works well, but it is based on the assumption that we know something about the parent interface. What if this assumption is wrong, or we can't use this approach for some other reason?

Well, we can even do it without breakpoints. Click the Button N, and while the application hangs, go to IntelliJ IDEA. From the main menu, select Run | Debugging Actions | Pause Program.

Depurar Aplicaciones No Responsivas

The application will suspend, allowing us to examine the current state of the threads in the Threads & Variables tab. This gives us an idea of ​​what the application is doing at that moment. Since it is hanging, we can identify the method causing the block and trace it back to the call site.

This approach has some advantages over a more traditional thread dump, which we'll cover shortly. For example, it provides you with information about variables in a convenient form and allows you to control further execution of the program.

Tip: For more tips and tricks with Pause Program see Debugging without breakpoints and Debugger.godMode()

Thread Dumps

Finally, we can use a thread dump, which is not strictly a debugger feature. It is available regardless of whether you are using the debugger.

Click the Button N. While the application is crashing, go to IntelliJ IDEA. From the main menu, select Run | Debugging Actions | Get Thread Dump.

Explore the available threads on the left, and in AWT-EventQueue you will see what is causing the problem.

Depurar Aplicaciones No Responsivas

The disadvantage of thread dumps is that they only provide a snapshot of the state of the program at the time they were made. You cannot use thread dumps to explore variables or control program execution.

In our example, we don't need to resort to a thread dump. However, I still wanted to mention this technique as it can be useful in other cases, such as when you are trying to debug an application that has been launched without the debugging agent.

Understand the problem

Regardless of the debugging technique, we arrive at ActionImpl14. In this class, someone intended to do the work in a separate thread, but confused Thread.start() with Thread.run(), which runs code in the same thread as the calling code.

IntelliJ IDEA's static analyzer even warns us about this at design time:

Depurar Aplicaciones No Responsivas

A method that does heavy lifting (or sleeps a lot in this case) is called on the UI thread and blocks it until the method finishes. That's why we can't do anything in the UI for a while after clicking the Button N.

HotSwap

Now that we have discovered the cause of the error, let's correct the problem.

We could stop the program, recompile the code and then run it again. However, it is not always wise to redeploy the entire application just because a small change was made.

Let's do it the smart way. First, fix the code using the suggested quick fix:

Depurar Aplicaciones No Responsivas

After the code is ready, click Run | Debugging Actions | Reload Changed Classes. A balloon appears, confirming that the new code has arrived in the VM.

Depurar Aplicaciones No Responsivas

Let's go back to the app and check. Clicking Button N no longer crashes the app.

Tip: Keep in mind that HotSwap has its limitations. If you are interested in extended HotSwap capabilities, it might be a good idea to take a look at advanced tools like DCEVM or JRebel

Summary

Using our reasoning and a couple of debugger features, we were able to locate the code that was causing a UI crash in our project. We then proceeded to fix the code without wasting time on recompilation and redistribution, which can be lengthy in real-world projects.

I hope you find the techniques described useful. Let me know what you think!

If you are interested in more articles related to debugging and profiling, check out some of my other articles:

  • Debugger.godMode() – Hack a JVM Application with the Debugger
  • Troubleshoot Slow Debugger
  • What's Wrong with createDirectories()? - Guide to CPU Profiling
  • Debug without Breakpoints

Stay tuned for more!

The above is the detailed content of Debug Non-Responsive Applications. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, SvelteTop 4 JavaScript Frameworks in 2025: React, Angular, Vue, SvelteMar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20: Key Performance Boosts and New FeaturesNode.js 20: Key Performance Boosts and New FeaturesMar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

Iceberg: The Future of Data Lake TablesIceberg: The Future of Data Lake TablesMar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue FixedSpring Boot SnakeYAML 2.0 CVE-2022-1471 Issue FixedMar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How can I implement functional programming techniques in Java?How can I implement functional programming techniques in Java?Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools