小编Srk*_*kic的帖子

Java进程内存无限增长.内存泄漏?

我们在Solaris 10上运行了一个java进程,为大约200-300个并发用户提供服务.管理员报告说,进程使用的内存会随着时间的推移而显着增加.它在几天内达到2GB,并且永远不会停止增长.

我们已经倾倒堆和使用Eclipse内存分析器进行分析,但无法看到任何不寻常那里.堆大小非常小.

增加内存统计记录之后,我们的应用,我们发现通过"顶"工具,管理员使用,并通过MemoryMXBean和运行时库报道的使用报告的内存使用量之间的差异.

这是两者的输出.

Memory usage information 

From the Runtime library
Free memory: 381MB
Allocated memory: 74MB
Max memory: 456MB
Total free memory: 381MB

From the MemoryMXBean library.
Heap Committed: 136MB
Heap Init: 64MB
Heap Used: 74MB
Heap Max: 456MB
Non Heap Committed: 73MB
Non Heap Init: 4MB
Non Heap Used: 72MB

Current idle threads: 4
Current total threads: 13
Current busy threads: 9
Current queue size: 0
Max threads: 200
Min threads: 8
Idle Timeout: 60000

  PID USERNAME …
Run Code Online (Sandbox Code Playgroud)

java memory heap

10
推荐指数
1
解决办法
1491
查看次数

Java 8 - 有效的最终变量、lambda 和 try/catch/finally 块

因此,我开始使用 Java 8 流/lambda 表达式,并遇到了一些有趣的问题,我不太确定如何解决。所以我在这里请求你的帮助。

有问题的示例代码:

public void insertBlankPages(File inputFile, String outputFile, final int OFFSET) {
    PDDocument newDocument;
    PDDocument oldDocument;
    try {
        newDocument = createNewDocument();
        oldDocument = createOldDocument(inputFile);
        List<PDPage> oldPages = getAllPages(oldDocument);

        oldPages.stream()
                .limit(oldPages.size() - OFFSET)
                .forEach(page -> {
                    newDocument.addPage(page);
                    newDocument.addPage(new PDPage(page.getMediaBox()));
                });

        newDocument.save(outputFile);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (COSVisitorException e) {
        e.printStackTrace();
    } finally {
        newDocument.close();
        oldDocument.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,编译器抱怨在finally块中调用了close()。错误是:“变量 newDocument 可能尚未初始化”。旧文档也一样。

当然,我继续初始化变量,如下所示:

PDDocument newDocument = null;
PDDocument oldDocument = null;
Run Code Online (Sandbox Code Playgroud)

现在我收到编译器错误“lambda 表达式中使用的变量应该是有效的最终变量”。

这件事该怎么办呢?

createNewDocument 和 …

java lambda finally try-catch

5
推荐指数
1
解决办法
2888
查看次数

标签 统计

java ×2

finally ×1

heap ×1

lambda ×1

memory ×1

try-catch ×1