不停的实例化对象导致OOM
使用axis调用webService,系统运行一段时间后,出现了 OOM,还好日志中 记下了错误信息。
Exception in thread "Thread-1301" java.lang.OutOfMemoryError: Java heap space at org.apache.xerces.dom.DeferredDocumentImpl.createChunk(Unknown Source) at org.apache.xerces.dom.DeferredDocumentImpl.ensureCapacity(Unknown Source) at org.apache.xerces.dom.DeferredDocumentImpl.createNode(Unknown Source) at org.apache.xerces.dom.DeferredDocumentImpl.createDeferredDocument(Unknown Source) at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source) at org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at org.apache.axis.utils.XMLUtils.newDocument(XMLUtils.java:369) at org.apache.axis.utils.XMLUtils.newDocument(XMLUtils.java:388) at org.apache.axis.configuration.FileProvider.configureEngine(FileProvider.java:179) at org.apache.axis.AxisEngine.init(AxisEngine.java:172) at org.apache.axis.AxisEngine.<init>(AxisEngine.java:156) at org.apache.axis.client.AxisClient.<init>(AxisClient.java:52) at org.apache.axis.client.Service.getAxisClient(Service.java:104) at org.apache.axis.client.Service.<init>(Service.java:113) at catic.hotel.server.webservice.PmsServiceData.<init>(PmsServiceData.java:26) at catic.hotel.server.task.SendCmdTask.<init>(SendCmdTask.java:200) at catic.hotel.server.task.TaskFactory.getTask(TaskFactory.java:155) at catic.hotel.server.server.ReadData.read(ReadData.java:82) at catic.hotel.server.server.ReadData.run(ReadData.java:57)
由于系统会频繁的进入到 SendCmdTask,而我又在此构造方法中不停的new PmsServiceData(),而PmsService的无参构造方法中又new Service(),相当于导致了Service对象不停的被实例化,时间久了,则出现OOM。
解决方法:
对PmsServiceData使用单例模式,即:
private static PmsServiceData pmsServiceData = null; // 单例 public static synchronized PmsServiceData getInstance(){ if(null == pmsServiceData){ pmsServiceData = new PmsServiceData(); } return pmsServiceData; }
问题解决。