我的 java 程序时不时地死机,并且可能有内存泄漏。我的遥测有用吗?

My java program freezes every now and then and may have a memory leak. Is my telemetry useful?

我最近开始学习 java(我现在有额外的空闲时间)并使用 netbeans 8.2 为名为 torn 的游戏编写代码。此代码是一个 运行nable,它可以连续从站点检索数据并将其存储以备后用。 运行 中期天气很冷,我被告知为 url 连接添加连接超时和读取超时可能会有所帮助。我这样做了,现在又 运行ning 了(到目前为止还不错),我在等着看这个问题是否会再次发生。

我的问题是关于查找内存泄漏。我读到存活世代数是判断是否存在内存泄漏的一个指标。这是一个screenshot of the telemetry after a few hours of running. The code for the project也可用(OtherFactionsStats.java是主class)。 (也非常欢迎任何关于改进我的代码的注释 :D )。感谢您的帮助。

我想我终于发现了您的资源泄漏:在第 154 行的 LogFactionPersonalStat 中,您打开了一个带有 in = new BufferedReader(... 的文件,该文件永远不会关闭。

我建议你了解一下try with resources

同一文件中的代码,例如第 128 行到 130 行将从

    FileWriter file = new FileWriter("" + path + completed); //set completion status to incomplete
    file.write("0"); //zero means not yet complete
    file.close();

    try (FileWriter file = new FileWriter("" + path + completed)) { //set completion status to incomplete
        file.write("0"); //zero means not yet complete
    }

变化不大,但现在您不能忘记关闭 FileWriter(或 BufferedWriterBufferedReader


另一个注意事项:RunUpdate 第 53 行看起来很可疑(这种模式重复了几次):

    Logger.getLogger("Error reading from file");

在这里你为 "Error reading from file" 创建了一个日志记录器,但是你从不使用那个记录器向日志中写入任何东西。

你可能想写这样的东西

    Logger.getLogger(OtherFactionsStats.class.getName()).severe("Error: Possible timeout or problems with file");

    Logger.getLogger(OtherFactionsStats.class.getName()).log(Level.Severe, "Error: Possible timeout or problems with file");