benji
benji

Reputation: 2451

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.

Why?

Upvotes: 0

Views: 190

Answers (1)

Alexey Malev
Alexey Malev

Reputation: 6533

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:

  1. Author's code being run as is doesn't seem to perform any allocation at all, most likely because JIT detects unused references;
  2. Modified version of code that outputs created threadFactorys to System.out results in "saw-like" heap usage pattern:

enter image description here

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. The code you posted is ok, though.

Upvotes: 3

Related Questions