【问题标题】:Out of Memory exception while running multithreaded code运行多线程代码时出现内存不足异常
【发布时间】:2013-09-05 00:58:00
【问题描述】:

我正在做一个项目,我将在其中拥有不同的捆绑包。举个例子,假设我有 5 个捆绑包,每个捆绑包都有一个方法名称 process

现在,我正在使用下面的多线程代码并行调用所有这 5 个捆绑包的 process 方法。

但不知何故,每次我运行下面的多线程代码时,它总是给我内存不足的异常。但是如果我是按顺序运行的意思是,一个一个地调用进程方法,那么它不会给我任何内存不足的异常。

下面是代码-

public void callBundles(final Map<String, Object> eventData) {

    // Three threads: one thread for the database writer, two threads for the plugin processors
    final ExecutorService executor = Executors.newFixedThreadPool(3);

    final Map<String, String> outputs = (Map<String, String>)eventData.get(Constants.EVENT_HOLDER);

    for (final BundleRegistration.BundlesHolderEntry entry : BundleRegistration.getInstance()) {
        executor.submit(new Runnable () {
            public void run() {
                try {
                    final Map<String, String> response = entry.getPlugin().process(outputs);

                    //process the response and update database.
                    System.out.println(response);
                } catch (Exception e) {
                    e.printStackTrace();
                }               
            }
        });
    }
}

以下是例外,每当我在多线程代码上运行时都会遇到。

JVMDUMP006I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" - please wait.
JVMDUMP032I JVM requested Heap dump using 'S:\GitViews\Stream\goldseye\heapdump.20130904.175256.12608.0001.phd' in response to an event
JVMDUMP010I Heap dump written to S:\GitViews\Stream\goldseye\heapdump.20130904.175256.12608.0001.phd
JVMDUMP032I JVM requested Java dump using 'S:\GitViews\Stream\goldseye\javacore.20130904.175256.12608.0002.txt' in response to an event
UTE430: can't allocate buffer
UTE437: Unable to load formatStrings for j9mm
JVMDUMP010I Java dump written to S:\GitViews\Stream\goldseye\javacore.20130904.175256.12608.0002.txt
JVMDUMP032I JVM requested Snap dump using 'S:\GitViews\Stream\goldseye\Snap.20130904.175256.12608.0003.trc' in response to an event
UTE001: Error starting trace thread for "Snap Dump Thread": -1
JVMDUMP010I Snap dump written to S:\GitViews\Stream\goldseye\Snap.20130904.175256.12608.0003.trc
JVMDUMP013I Processed dump event "systhrow", detail "java/lang/OutOfMemoryError".
ERROR: Bundle BullseyeModellingFramework [1] EventDispatcher: Error during dispatch. (java.lang.OutOfMemoryError: Failed to create a thread: retVal -1073741830, errno 12)
java.lang.OutOfMemoryError: Failed to create a thread: retVal -1073741830, errno 12

JVMDUMP006I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" - please wait.
JVMDUMP032I JVM requested Heap dump using 'S:\GitViews\Stream\goldseye\heapdump.20130904.175302.12608.0004.phd' in response to an event
JVMDUMP010I Heap dump written to S:\GitViews\Stream\goldseye\heapdump.20130904.175302.12608.0004.phd
JVMDUMP032I JVM requested Java dump using 'S:\GitViews\Stream\goldseye\javacore.20130904.175302.12608.0005.txt' in response to an event

我在 Eclipse 中使用 JDK1.6.0_26 作为已安装的 JRE。

【问题讨论】:

  • 我猜这就是我在上面的代码中使用的?
  • 是的,我尝试在池大小为 1 的情况下这样做,我得到了同样的错误.. :(
  • 这将每秒钟创建 1000 个 3 个线程(3000 个线程)的线程池...您可能需要考虑创建一个全局执行器而不是本地实例...
  • 就我个人而言,我会创建一个集中处理池,所以不管是谁在调用callBundles 或从哪里调用,所有请求都进入同一个池。这使您能够根据需要管理线程池...
  • 你在输出映射中积累了一些东西吗?它可以长得很大,不是吗?

标签: java multithreading out-of-memory executorservice


【解决方案1】:

callBundles() 的每次调用都会通过创建一个自己的执行器来创建一个新的线程池。每个线程都有自己的堆栈空间!所以如果你说你启动 JVM,第一次调用将创建三个线程,总堆为 3M(1024k 是 64 位 JVM 的默认堆栈大小),下一次调用另一个 3M 等等。1000 次调用/s 将需要3GB/秒!

第二个问题是你从来没有shutdown()创建的执行器服务,所以线程将一直存在,直到垃圾收集器删除执行器(finalize()也调用shutdown())。但是 GC 永远不会清除栈内存,所以如果栈内存有问题,堆没有满,GC 永远也帮不上忙!

您需要使用 一个 ExecutorService,假设使用 10 到 30 个线程或自定义 ThreadPoolExecutor,使用 3-30 个缓存线程和 LinkedBlockingQueue。如果可能,在您的应用程序停止之前调用服务上的shutdown()

检查应用程序的物理 RAM、负载和响应时间,以调整参数堆大小、最大线程数和池中线程的保持活动时间。查看代码的其他锁定部分(数据库连接池的大小,...)以及服务器的 CPU/内核数。线程池大小的起点可能是 CPU/核心数加 1。I/O 等待越多越有用。

【讨论】:

    【解决方案2】:

    主要问题是您并没有真正正确地使用线程池。如果您所有的“进程”线程都具有相同的优先级,那么没有充分的理由不创建一个大型线程池并将所有 Runnable 任务提交给它。注意 - 在这种情况下,“大”是通过实验和分析确定的:调整它直到您在速度和内存方面的表现达到您的预期。

    这是我所描述的一个例子:

    // Using 10000 purely as a concrete example - you should define the correct number
    public static final LARGE_NUMBER_OF_THREADS = 10000;
    // Elsewhere in code, you defined a static thread pool
    public static final ExecutorService EXECUTOR = 
        Executors.newFixedThreadPool(LARGE_NUMBER_OF_THREADS);
    
    public void callBundles(final Map<String, Object> eventData) {
    
    final Map<String, String> outputs = 
        (Map<String, String>)eventData.get(Constants.EVENT_HOLDER);
    
    for (final BundleRegistration.BundlesHolderEntry entry : BundleRegistration.getInstance()) {
        // "Three threads: one thread for the database writer, 
        // two threads for the plugin processors"
        // so you'll need to repeat this future = E.submit() pattern two more times
        Future<?> processFuture = EXECUTOR.submit(new Runnable() {
            public void run() {
                final Map<String, String> response = 
                    entry.getPlugin().process(outputs);
                //process the response and update database.
                System.out.println(response);
            }
        }
        // Note, I'm catching the exception out here instead of inside the task
        // This also allows me to force order on the three component threads
        try {
            processFuture.get();
        } catch (Exception e) {
            System.err.println("Should really do something more useful");
            e.printStackTrace();
        }
        // If you wanted to ensure that the three component tasks run in order, 
        // you could future = executor.submit(); future.get(); 
        // for each one of them
    }
    

    为了完整起见,您还可以使用cached thread pool 来避免重复创建短期线程。但是,如果您已经担心内存消耗,那么固定池可能会更好。

    当您使用 Java 7 时,您可能会发现 Fork-Join 是比一系列 Future 更好的模式。不过,无论哪种方式最适合您的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多