java.lang.OutOfMemoryError: unable to create new native thread

前端 未结 3 1516
臣服心动
臣服心动 2020-12-09 22:46

i saw comment like this

one place i have seen this problem is if you keep creating threads, and instead of calling start(), call run() directly on the

3条回答
  •  感情败类
    2020-12-09 23:37

    When you call run() method no new thread should be created. And your objects will be collected by Garbage collector when they are not referenced.

    Your other part of code may be creating lot of Threads.

    Try using ThreadPoolExecutor (thread pooling) in your code to limit threads in your application, And tune your threadpool size accordingly for better performance.

    You can also check following to debug your issue: (referenced from link) There are a few things to do if you encounter this exception.

    • Use the lsof -p PID command (Unix platforms) to see how many threads are active for this process.
    • Determine if there is a maximum number of threads per process defined by the operating system. If the limit is too low for the application, try raising the per-process thread limit.
    • Examine the application code to determine if there is code that is creating threads or connections (such as LDAP connections) and not destroying them. You could dump the Java threads to see if there are an excessive number has been created.
    • If you find that too many connections are opened by the application, make sure that any thread that the application creates is destroyed. An enterprise application (.ear) or Web application (.war) runs under a long-running JVM. Just because the application is finished does not mean that the JVM process ends. It is imperative that an application free any resources that it allocates. Another solution would be for the application to use a thread pool to manage the threads needed.

提交回复
热议问题