Time class generating OutOfMemory Error

≯℡__Kan透↙ 提交于 2019-12-25 18:32:18

问题


I am using the Time class and creating it of 20 tps, but I am getting below error.

 Exception in thread "pool-1-thread-9" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:597)
        at java.util.Timer.<init>(Timer.java:137)
        at java.util.Timer.<init>(Timer.java:106)
        at mcarbon.ucip.SheduleInMemRemove.<init>(SheduleInMemRemove.java:22)
        at mcarbon.ucip.XML_RPC_UCIP_DEBIT_CREDIT.run(XML_RPC_UCIP_DEBIT_CREDIT.java:229)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
        at java.lang.Thread.run(Thread.java:619)

Code:-

class SheduleInMemRemove{
            Timer timer;
        Config cfg;
          String key;

           public SheduleInMemRemove(String key,int seconds,Config cfg,String MSG,String IP,String PORT,boolean isResponse)
           {
                this.key=key;
                this.cfg=cfg;
                 LoggerWR.logger.info("[IN URL] [inside SheduleInMemRemove constructor][going to sleep to IN memory key["+key+"] [wake up after "+seconds+" seconds] [MSG]["+MSG+"] [isResponse]["+isResponse+"]");
                cfg.addInMem(key,IP,PORT,MSG,isResponse);
                //LoggerWR.logger.info("[IN URL] [Going to shedule]");
                timer = new Timer();
                timer.schedule(new WakeUpInTask(),seconds*1000);
                //LoggerWR.logger.info("[IN URL] [Done]");
           }
                class WakeUpInTask extends TimerTask
                {
                public void run() {
                //System.out.format("Time's up!%n");
                LoggerWR.logger.info("[IN URL] [Inside the SheduleInMemRemove class going to remove the key]["+key+"] ["+cfg.removeInMem(key)+"]");
                 //cfg.removeInMem(key);
                    timer.cancel(); //Terminate the timer thread
                }
        }
    }

pls help


回答1:


Each new thread is going to require a big chunk of memory to use for its call stack. Apparently, you're launching more threads than you have memory for.

If you're creating a large number of timers, you should consider using a ScheduledExecutorService instead.



来源:https://stackoverflow.com/questions/23844057/time-class-generating-outofmemory-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!