简体   繁体   English

在Java中使用执行程序时出现内存不足错误

[英]Out of Memory Error when using Executors in java

I am using the Executors framework(fixed thread pool with unbounded blocking queue) to execute tasks concurrently. 我正在使用Executors框架(具有无限制阻塞队列的固定线程池)来同时执行任务。

But when I run a load test with about 10000 tasks created, there is a huge build up of heap memory (2.1 GB) with about 3.5 million Executable objects. 但是,当我运行带有创建的约10000个任务的负载测试时,堆内存(2.1 GB)会大量增加,其中包含约350万个可执行对象。

I am not sure if the unbounded queue is causing this build up. 我不确定是否无限制的队列导致了这种累积。

Memory Analyzer report : 内存分析器报告:

One instance of "java.util.concurrent.ThreadPoolExecutor" loaded by "" occupies 2,299,506,584 (94.97%) bytes. 由“”加载的“ java.util.concurrent.ThreadPoolExecutor”的一个实例占用2,299,506,584(94.97%)个字节。 The instance is referenced by com.test.ScheduleBean @ 0x743592b28 , loaded by "org.jboss.modules.ModuleClassLoader @ 0x741b4cc40". 实例由com.test.ScheduleBean @ 0x743592b28引用,并由“ org.jboss.modules.ModuleClassLoader @ 0x741b4cc40”加载。

Any pointers appreciated! 任何指针表示赞赏!

        //The Executors are loaded in a hashmap
        HashMap<String,Executor> poolExecutorMap = new HashMap<String,Executor>();

       //Executor is a fixed thread pool one
       Executor poolExecutor = Executors.newFixedThreadPool(threadCount);

      //then add the executor to the hashmap
       poolExecutorMap.put("Executor", poolExecutor);



    //then a list of tasks are pulled from a database and passed as runnable objects to the executors

      Class<?> monitorClass=null;

      List<Task> list = getAllTasksToProcess();

     for (int i = 0; i < list.size(); i++) {
      Task task = list.get((int) i);

     monitorClass = Class.forName(task.getTask_event_name());
        Constructor<?> ctor;
        ctor = monitorClass.getConstructor(Task.class);
        Object object = ctor.newInstance(task);
        logger.debug("Adding  task number : "+task.getTask_sequence_id());
        poolExecutorMap.get("Executor").execute((Runnable) object);
}



// the executor classes have an execute method which sends a http notification.

Write OQL in MemoryAnalyzerTool 在MemoryAnalyzerTool中编写OQL

select * from java.util.concurrent.ThreadPoolExecutor 从java.util.concurrent.ThreadPoolExecutor中选择*

and execute the query. 并执行查询。 It will list the object in the separate window. 它将在单独的窗口中列出对象。 Then right click on the instances generated then 然后右键单击生成的实例,然后

path to GC roots --> Exclude soft/weak/phantom references GC根目录的路径->排除软/弱/幻像引用

It will help you in understanding who is holding strong reference of the suspected objects. 它将帮助您了解谁对可疑对象拥有强大的参考力。

Thanks for all the responses. 感谢您的所有回复。

The real problem was the way tasks were being pulled from the database. 真正的问题是从数据库中提取任务的方式。 Duplicate tasks were being added to the task queue and hence the queue build up. 重复的任务被添加到任务队列中,因此队列得以建立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM