运行多线程代码时出现内存不足异常

时间:2013-09-05 00:58:01

标签: java multithreading out-of-memory executorservice

我正在开展一个项目,我将在其中使用不同的Bundles。我们举一个例子,假设我有5个Bundle,每个bundle都有一个方法名process

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

但不知何故,每当我运行下面的多线程代码时,它总是会给我内存异常。但是,如果我按顺序运行它意味着,逐个调用进程方法,那么它不会给我任何Out of memory异常。

以下是代码 -

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

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

2 个答案:

答案 0 :(得分:2)

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

第二个问题是你永远不会shutdown()创建的执行程序服务,所以线程将继续存在,直到垃圾收集器删除执行程序(finalize()也调用shutdown())。但GC永远不会清除堆栈内存,所以如果堆栈内存出现问题并且堆未满,GC将永远无法帮助!

您需要使用一个 ExecutorService,比方说10到30个线程或自定义ThreadPoolExecutor,其中包含3-30个缓存线程和LinkedBlockingQueue。如果可能,请在应用程序停止之前调用服务shutdown()

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

答案 1 :(得分:1)

主要问题是你没有真正正确地使用线程池。如果所有“进程”线程具有相同的优先级,则没有充分的理由不创建一个大型线程池并将所有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比一系列期货更好。无论如何,最适合您的需求。