【问题标题】:How to resolve java.lang.outOfMemoryError in ThreadPoolExecutor program?如何解决 ThreadPoolExecutor 程序中的 java.lang.outOfMemoryError?
【发布时间】:2014-08-17 04:02:51
【问题描述】:

我正在运行一个 ThreadPoolExecutor 程序,它创建 1000 个 url 并发送到执行器服务。

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:673)
    at java.util.concurrent.ThreadPoolExecutor.addThread(ThreadPoolExecutor.java:681)
    at java.util.concurrent.ThreadPoolExecutor.addIfUnderCorePoolSize(ThreadPoolExecutor.java:706)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:650)
    at MyProgramName.main(MyProgramName.java:175)
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL
2014-08-16 20:46:21.341 java[5100:d07] java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:673)
    at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShutdown.java:312)
    at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.java:232)
    at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoShutdown.java:118)
2014-08-16 20:46:21.342 java[5100:d07] (
    0   CoreFoundation                      0x00007fff9127825c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff9031ce75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff91277e09 -[NSException raise] + 9
    3   JavaNativeFoundation                0x000000010c754c3f JNFCallStaticVoidMethod + 213
    4   libawt.jnilib                       0x0000000115fb8151 setBusy + 53
    5   CoreFoundation                      0x00007fff911a8d67 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    6   CoreFoundation                      0x00007fff911a8cd7 __CFRunLoopDoObservers + 391
    7   CoreFoundation                      0x00007fff9119a608 __CFRunLoopRun + 1368
    8   CoreFoundation                      0x00007fff91199e75 CFRunLoopRunSpecific + 309
    9   HIToolbox                           0x00007fff8fdd9a0d RunCurrentEventLoopInMode + 226
    10  HIToolbox                           0x00007fff8fdd97b7 ReceiveNextEventCommon + 479
    11  HIToolbox                           0x00007fff8fdd95bc _BlockUntilNextEventMatchingListInModeWithFilter + 65
    12  AppKit                              0x00007fff8ac5d24e _DPSNextEvent + 1434
    13  AppKit                              0x00007fff8ac5c89b -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
    14  libawt.jnilib                       0x0000000115fb987f -[NSApplicationAWT nextEventMatchingMask:untilDate:inMode:dequeue:] + 124
    15  AppKit                              0x00007fff8ac5099c -[NSApplication run] + 553
    16  libawt.jnilib                       0x0000000115fb7f30 +[AWTStarter startAWT:] + 1495
    17  libawt.jnilib                       0x0000000115fb78aa -[CPerformer perform] + 93
    18  Foundation                          0x00007fff8c64313e __NSThreadPerformPerform + 229
    19  CoreFoundation                      0x00007fff911a95b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    20  CoreFoundation                      0x00007fff9119ac62 __CFRunLoopDoSources0 + 242
    21  CoreFoundation                      0x00007fff9119a3ef __CFRunLoopRun + 831
    22  CoreFoundation                      0x00007fff91199e75 CFRunLoopRunSpecific + 309
    23  java                                0x000000010be86843 java + 18499
    24  java                                0x000000010be8629a java + 17050
    25  java                                0x000000010be83a98 java + 6808
    26  ???                                 0x0000000000000005 0x0 + 5
)
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->MyURLURLURLURLRURL

我已经对互联网和 stackoverflow 进行了一些研究。一些答案说我需要减少堆栈大小。有人说我需要使用 ulimit -a 来增加 ulimit。我完全糊涂了。我对这些答案感到困惑,因为它们并非特定于 ThreadExecutor。请帮帮我

我在 Mac OS、i7 四核处理器、16 GB RAM、1 Tb HD 中运行代码

我的程序代码在这个问题中Why does the following executor service java Thread program doesn't shut down?

【问题讨论】:

  • 您的系统有多少内存,您为程序分配了多少内存(-Xms 和 -Xmx)?
  • 我的系统有 1Tb 硬盘。我可以知道如何找到为我的程序分配的内存。我不知道怎么找到那个。请帮帮忙。

标签: java multithreading memory memory-management threadpool


【解决方案1】:

真正的问题是您试图使用太多堆栈内存创建太多线程。

您可以减少线程堆栈的默认大小(如建议的那样),但这不是一个完整的解决方案。 (当您决定在更多 URL 上运行您的程序时,您将再次遇到同样的问题。最终您将遇到无法进一步减小堆栈大小的“墙”;例如,因为您收到 StackOverflowError例外。)

更好的解决方案是不要创建这么多线程。您已将程序配置为使用 infinite 线程池。坦率地说,这很荒谬。改变这个:

ThreadPoolExecutor executor = 
      new ThreadPoolExecutor(Integer.MAX_VALUE, Integer.MAX_VALUE, 
                             20, TimeUnit.MILLISECONDS, blockingQueue);

更现实的东西:

ThreadPoolExecutor executor = 
      new ThreadPoolExecutor(100, 100, 
                             20, TimeUnit.MILLISECONDS, blockingQueue);

超过某一点,添加额外的线程不会使应用程序运行得更快。实际上,它通常会开始使应用程序运行速度变慢,有时会灾难性地变慢。

【讨论】:

  • 哇。这就像一种魅力。我可以知道为什么改变这个工作。我也在 ThreadPoolExecutor 文档中阅读了这个,但它是模棱两可的,我认为将池大小设置为 Integer.MAX_VALUE 会使事情变得更容易。但是几分钟后我收到此错误 线程“pool-1-thread-990”中的异常 java.lang.OutOfMemoryError: Java heap space at java.io.BufferedReader.(BufferedReader.java:80) at java.io .BufferedReader.(BufferedReader.java:91) at MyProgram$MyRunnable.run(MyProgram.java:232) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.jav
  • 另外,如果我在收到 "pool-1-thread-990" java.lang.OutOfMemoryError: Java heap space at java.io.BufferedReader. 时终止程序,我的程序会不会t 处理管道中的 url 或者它仍然会被搁置?
  • “最佳数字是多少” - 对此没有一般性的答案。让它成为一个调整参数......然后调整它。
  • 我想在这里回答会帮助其他几个用户。
  • @user3705478 你应该尝试每个问题问一个问题。
【解决方案2】:

当 JVM 向操作系统请求新线程时,您将面临“java.lang.OutOfMemoryError: Unable to create new native thread”。每当底层操作系统无法分配新的本地线程时,就会抛出这个 OutOfMemoryError。本机线程的确切限制非常依赖于平台,因此我们建议通过运行一个小测试来找出您的限制,以找出特定于平台的线程限制。但是,一般来说,导致 java.lang.OutOfMemoryError: Unable to create new native thread 的情况会经历以下几个阶段:

  1. 在 JVM 中运行的应用程序请求新的 Java 线程
  2. JVM 本机代码向操作系统代理创建新本机线程的请求
  3. 操作系统尝试创建一个新的本机线程,该线程需要为线程分配内存
  4. 操作系统将拒绝本机内存分配,因为 32 位 Java 进程大小已耗尽其内存地址空间 - 例如(2-4) 已达到 GB 进程大小限制 - 或操作系统的虚拟内存已完全耗尽
  5. 抛出java.lang.OutOfMemoryError: Unable to create new native thread错误

在您的情况下,解决此问题的最佳方法绝对是遵循建议的限制线程创建的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2012-04-27
    • 2018-12-25
    • 2014-06-15
    • 1970-01-01
    相关资源
    最近更新 更多