java - 为什么在 Java 中调试(单步执行)时 https 连接如此慢?

标签 java eclipse debugging networking

我正在尝试调试 Java 代码的一些问题,该代码建立了 http/https 连接。我想知道为什么在调试时连接变得非常慢(下载一个小网页超过一分钟),即使单步执行网络工作的方法也是如此。如果有什么补救措施。

下面我发布了一个示例(您可以将 https: 更改为 http: ,并尝试从 Eclipse 中按 F11 进行调试 - 以进行单步调试,在第一个 main() 语句中放置一个断点,并在暂停时按 F6

我的结果(时间以毫秒为单位):

                        conn time   total time
http  (not stepping)        60           350
http  (stepping over)     1100          1500
https (not stepping)       570          1300
https (stepping over)    21000         83000
<小时/>

编辑:禁用在一步操作后显示方法结果选项(howlger的答案恰本地建议了补救措施)后,时间变得更加更合理(大约十分之一)对于 https)。

http  (stepping over 2)     150           450
https (stepping over 2)    2000          7000
<小时/>

我的场景:Java 8 (1.8.0_121-b13)、64 位、Win-7、Eclipse Photon(也使用过 Oxygen)。

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class TestConn {

    public static void downloadFromUrl(final URL url) throws IOException {
        long t0 = System.currentTimeMillis();
        URLConnection conn = url.openConnection();
        System.out.println("conn msecs: " +
              (System.currentTimeMillis() - t0) + " url=" + url);
        System.out.println("=====================================");
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            int cont = 0;
            while ((line = in.readLine()) != null) {
                if (cont++ < 4)
                    System.out.println(line);
            } 
            if(cont >=4) 
                System.out.printf("== total lines: %d (%d skipped)\n",cont,cont-4);
            System.out.println("==============done=======================");
        } 
    }

    public static void testConn(String urls) {
        try {
            long t0 = System.currentTimeMillis();
            downloadFromUrl(new URL(urls));
            System.out.println("Done , total time msecs: " + 
              (System.currentTimeMillis() - t0));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        testConn("https://www.example.com/"); // breakpoint here, step-over with F6
        System.out.println("bye");
    }
}

最佳答案

这就是为什么在窗口 > 首选项:Java > 调试中有首选项在步骤操作后显示方法结果(如果 VM 支持) ;可能很慢)

自 Eclipse 2018-09 (4.9) 起,可以为此设置超时(默认为 7 秒):

关于java - 为什么在 Java 中调试(单步执行)时 https 连接如此慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244437/

相关文章:

java - 是否有任何开源库可以获取java bean的getters的所有值?

java - 不会溢出的变量

java - 在 Eclipse 中构建 Java 后如何自动导出 WAR?

c++ - Eclipse Cygwin,在 PATH 中找不到程序 "\xlC"

python - mod_wsgi/Python 正在优化吗?

java - Java中提取子字符串

java - 异步调用重复我的 ArrayList 条目

更新 Android SDK 后 Eclipse ADT(主题和图标)损坏

debugging - 在不修改源代码的情况下调用 runtime.GC()/memprofile

vb.net - 调试触发构建错误(未找到文件)和防病毒程序,Visual Basic (V11 2012)