简体   繁体   English

Java-ThreadFactory内存泄漏?

[英]Java - ThreadFactory memory leak?

This code: 这段代码:

    while (true) {
        new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return null;
            }
        };
    }

make the JVM go out of memory very quickly. 使JVM非常快地耗尽内存。

Why? 为什么?

I have tried to run this code with JRE 1.7.0_60 x86_64 on Windows 7 with default options and here are the results: 我尝试使用默认选项在Windows 7上使用JRE 1.7.0_60 x86_64运行此代码,结果如下:

  1. Author's code being run as is doesn't seem to perform any allocation at all, most likely because JIT detects unused references; 照原样运行作者的代码似乎根本不执行任何分配,这很可能是因为JIT检测到未使用的引用。
  2. Modified version of code that outputs created threadFactory s to System.out results in "saw-like" heap usage pattern: 将创建的threadFactory输出到System.out的代码的修改版本导致“锯状”堆使用模式:

在此处输入图片说明

Which means that both allocation and garbage collection takes place. 这意味着分配和垃圾回收都会发生。

Back to your question: I think you missed some significant parts of the code, or put -Xmx to extremely low value, or some other reason. 回到您的问题:我认为您错过了代码的重要部分,或者将-Xmx设置为极低的值,或者是其他原因。 The code you posted is ok, though. 不过,您发布的代码还可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM