multithreading - Exception: java.lang.OutOfMemoryError, unable to create a new native thread -


i creating new threads every 5 seconds , using threadpool exector. making sure threads getting closed. getting

exception in thread "timer-0" java.lang.outofmemoryerror: unable create new native thread.

is happening because creating many threads , not closing them? or creating new ones frequently?

can please tell me if doing wrong in code?

public class api{     private   mongostoreexecutor executor = new mongostoreexecutor(10,50);     private class mongostoreexecutor extends threadpoolexecutor {         public mongostoreexecutor(int queuesize, int maxthreadpoolsize) {             super(10, maxthreadpoolsize, 30, timeunit.seconds,                  new arrayblockingqueue<runnable>(queuesize),                 new threadpoolexecutor.callerrunspolicy());         }     }      public timertask alertingpoolsdata() throws exception, unknownhostexception {         timeertask task  = new timertask(){             public void run(){                 pools = alertingpools.values().toarray();                 list<future<?>> tasks = new arraylist<future<?>>(pools.length);                 (object pool : pools) {                     tasks.add(executor.submit(new dataaccumulation(timestartsecdata,                         timeendsec,pool, jsonarrayresult,dataresult)));                 }                 for( future<?> f: tasks) {                     f.get(2 * 1000l, timeunit.milliseconds);                 }             }         } catch (exception e) {             e.printstacktrace();         }          long interval = 5 * 1000l;         tm.scheduleatfixedrate(task,(interval -              (system.currenttimemillis() % interval)), interval);         return task;     } } 

my hunch has code can't see: suspect you're calling alertingpoolsdata on , on again somewhere. result, suspect scheduling timertask on , on again. each 1 of component timertasks repeatedly creating unknown number of future<?>s (one each of elements of pools), each of going mongostoreexecutor.

if of above case, you've got positive first derivative on thread count curve. result, you're going see quadratic increase in thread count on time. you'll start hitting upper limits of native threads pretty quickly.

as suggested in comments, modify current implementation. i'd suggest combination of scheduledexecutorservice , forkjoinpool.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -