java 调试_我最喜欢的Java调试技术

 2023-09-06 阅读 20 评论 0

摘要:java 调试This article is about techniques which I have used to debug codeBases of various kinds, such as: 本文是关于我用来调试各种代码库的技术,例如: CodeBase with high concurrent nature. 具有高度并发性的CodeBase。 CodeBase with a lot of prop

java 调试

This article is about techniques which I have used to debug codeBases of various kinds, such as:

本文是关于我用来调试各种代码库的技术,例如:

  1. CodeBase with high concurrent nature.

    具有高度并发性的CodeBase。
  2. CodeBase with a lot of proprietary (unsupported) libraries.

    带有许多专有(不受支持)库的CodeBase。
  3. CodeBase with a lot of deprecated/unwanted code.

    具有许多不推荐使用/不需要的代码的CodeBase。
  4. CodeBase with memory leaks.

    内存泄漏的CodeBase。
  5. CodeBase where every JVM can talk to every other JVM.

    每个JVM可以与每个其他JVM进行通信的CodeBase。

So let’s look at them one by one.

因此,让我们一一看一下。

具有高并发性的CodeBase。 (CodeBase with high concurrent nature.)

It may happen that for serving a request, JVM uses many threads for eg:

为了满足请求,JVM可能会使用许多线程,例如:

Req -> tomcatThread-1 -> executorThread-2 -> BizThread-3->…`

Let say, we find that exception is coming in BizThread-3. Now as a debugger, we want to understand the Request flow. But the stacktrace will not be able to provide the complete request flow (for example, what happened in executorThread-2 and what happened in tomcatThread-1 and so on).

假设,我们发现BizThread-3中出现了异常。 现在,作为调试器,我们希望了解请求流。 但是stacktrace将无法提供完整的请求流(例如, executorThread-2中发生的事情以及tomcatThread-1中发生的事情,等等)。

Technique 1.1: Write a custom java-agent which will be used to effectively add log.debug() to the start and end of every method of certain java packages. This will give us some insight into what all is getting called.

技术1.1:编写一个自定义的Java代理 ,该代理将用于将log.debug()有效地添加到某些Java包的每个方法的开头和结尾。 这将使我们对所有被调用的内容有一些了解。

Technique 1.2: In certain frameworks, if supported, use AOP to proxy all methods and effectively add log.debug().

技术1.2:在某些框架中,如果受支持,则使用AOP代理所有方法并有效地添加log.debug()

带有许多专有(不受支持)库的CodeBase。 (CodeBase with a lot of proprietary (unsupported) libraries.)

Sometimes we find ourself in a situation where, after hours of debugging, we nail the problem that xyz-gov-secret library is misbehaving and this library is now unsupported.

有时,我们发现自己处于这样一种情况:经过数小时的调试,我们发现xyz-gov-secret库行为不当,并且现在不支持该库的问题。

Technique 2.1: Roll up your sleeves and install eclipse-decompiler and dive into the code base.

技术2.1:卷起袖子并安装eclipse-decompiler 并深入研究代码库。

具有许多不推荐使用/不需要的代码的CodeBase。 (CodeBase with a lot of deprecated/unwanted code.)

This is a classic one: we sometimes find ourselves in a method of 500+ lines with tons of deprecated if-else. Now, how do we figure out what is the code flow for a particular call, which if-else are going to use, and which is the dead code?

这是一个经典的例子:有时我们会发现自己的方法是使用500多个行,其中包含大量不推荐使用的if-else。 现在,我们如何确定特定调用的代码流是什么,if-else将使用什么,死代码是什么?

Technique 3.1: We can use a tool called jacoco agent. It collects the execution details during runtime and can color-code the code in eclipse.Basically, it is the same tool, generally used in analyzing code coverage by JUnit Test.

技术3.1:我们可以使用称为jacoco agent的工具。 它收集运行时的执行细节,并可以在eclipse中对代码进行颜色编码。基本上,它是同一工具,通常用于JUnit Test分析代码覆盖率。

内存泄漏的CodeBase。 (CodeBase with memory leaks.)

Every developer has a day when, in their local system, all goes good, in production OutOfMemory :(

每个开发人员都有一天在他们的本地系统中一切正常,在生产OutOfMemory中:(

Technique 4.1: JVM provides techniques to capture heap dumps in case of outOfMemory.

技术4.1: JVM提供了在内存不足的情况下捕获堆转储的技术。

Add the following as an argument while starting the JVM -XX:+HeapDumpOnOutOfMemoryError . This will capture the heap dump and put it in a file, which can be used to analyze what is eating up the memory.

在启动JVM -XX时添加以下内容作为参数:+ HeapDumpOnOutOfMemoryError 这将捕获堆转储并将其放入文件中,该文件可用于分析正在消耗内存的内容。

Technique 4.2: You can also take the heap dump/thread dump of a running JVM using jProfiler/Jvisiualvm.

技术4.2:您还可以使用jProfiler / Jvisiualvm获得正在运行的JVM的堆转储/线程转储。

每个JVM可以与每个其他JVM进行通信的CodeBase。 (CodeBase where every JVM can talk to every other JVM.)

When you are thrown into a spaghetti distributed environment, it becomes difficult to track down the request flow.

当您陷入意粉分布式环境时,很难追踪请求流。

Technique 5.1: You can use tools like Wireshark. Wireshark captures the network data and represents it in a nice UI. You can then view the HTTP request/response flowing through the system

技术5.1:您可以使用Wireshark之​​类的工具。 Wireshark捕获网络数据并在一个漂亮的UI中表示它。 然后,您可以查看通过系统的HTTP请求/响应

荣誉奖 (Honorable mentions)

Technique 6.1: In a single-threaded environment, intentionally insert try catch in order to quickly know the stacktrace.

技术6.1:在单线程环境中,有意插入try catch以快速了解stacktrace。

try {throw new RuntimeException(); 
} catch(Exception e){e.printStackTrace();
}

Technique 6.2: Using eclipse breakpoint or using conditional breakpoint.

技术6.2:使用蚀断点或有条件断点。

Technique 6.3: https://en.wikipedia.org/wiki/Rubber_duck_debugging

技术6.3: https //en.wikipedia.org/wiki/Rubber_duck_debugging

Motivation of article: Team Learning/Knowledge Sharing.
文章动机:团队学习/知识共享。

翻译自: https://www.freecodecamp.org/news/how-to-debug-java-code-4a28442e0959/

java 调试

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/7411.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息