问题
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