本文介绍了时间类产生OutOfMemory错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Time类并创建20 tps的时间类,但出现了以下错误.

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)

代码:-

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
                }
        }
    }

请帮助

推荐答案

每个新线程都将需要大量内存才能用于其调用堆栈.显然,您启动的线程多于内存.

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.

如果要创建大量计时器,则应考虑使用 ScheduledExecutorService .

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

这篇关于时间类产生OutOfMemory错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:32