问题描述
我每20秒取一次串口列表,然后打开每一个串口,向串口里写数据,再把串口关闭掉。可是发现一个问题。内存一直在增长,最后溢出。报nUnsetOwner: Error 8 in MapViewOfFileException in thread "Thread-2" java.lang.OutOfMemoryError: unable to create new native thread 有谁遇到过这样的问题。 问题补充:import java.util.Vector;public class test3 implements Runnable{public static void main(String args[]) { test3 bcb = new test3(); Thread t = new Thread(bcb); t.start(); }public void run() {// TODO Auto-generated method stubwhile (true) {this.findAllList();try {Thread.sleep(20000);} catch (InterruptedException e) {e.printStackTrace();}}}public void findAllList(){BrilliantCommonImpl bci = new BrilliantCommonImpl();Vector base = bci.getAllCharacterSendBase();//查询所有的COM口if(base!=null&&base.size()>0){for(int i=0;i<base.size();i++){Vector details = (Vector)base.get(i);String videoId =(String)details.get(0);String assetSite = "255" ;String comPort = (String)details.get(3);String sendDigit = "9600";// 初始化串口SerialCommunication1 port = new SerialCommunication1(comPort);if (port.checkComport(comPort)) {// 判断串口是否有效port.open(sendDigit);//打开串口Vector details1 = bci.getAllCharacterSendDetails(videoId);if (details1 != null && details1.size() > 0) {for (int j = 0; j < details1.size(); j++) {Vector details2 = (Vector)details1.get(j);String csdId = (String) details2.get(0);String lineId = (String) details2.get(1);String rowId = (String) details2.get(2);String systemType = (String) details2.get(3);String tagCode = (String) details2.get(4);String information = (String) details2.get(5);String uom = (String)details2.get(6);String realData = "";if (tagCode != null && tagCode.trim().length() > 0) {String key = "001" + systemType + "real";realData = bci.getRealData(key,tagCode);}String dealInformation = bci.dealInformation(information, realData,uom);byte[] sendData = bci.dealData(lineId, rowId, assetSite, dealInformation);port.write1(sendData);// 向串口里写东西System.out.println("端口="+comPort+",数据="+sendData); }}port.close1();//关闭串口}}}}}import java.util.*;import java.io.*;import javax.comm.*;public class SerialCommunication1{String portName;CommPortIdentifier portId; InputStream inputStream; OutputStream outputStream; SerialPort serialPort;public SerialCommunication1(String comPort){//创建一个名字为 "COM"+n 的串口通信对象Enumeration portList=CommPortIdentifier.getPortIdentifiers();while (portList.hasMoreElements()){portId = (CommPortIdentifier)portList.nextElement(); if (portId.getPortType()==CommPortIdentifier.PORT_SERIAL) { if(portId.getName().equals(comPort)) {portName=comPort;break;} } else {System.out.println("找不到串口!"+comPort);}}}public boolean checkComport(String comPort){//创建一个名字为 "COM"+n 的串口通信对象if(portId.getName().equals(comPort)){return true;}else{return false;}}public void open(String sendDigit){//打开串口以及输入输出流try{serialPort=(SerialPort)portId.open("Serial_Communication", 2000);}catch(PortInUseException e){System.out.println(portId.getName()+"端口正被占用!");} try {serialPort.setSerialPortParams(Integer.valueOf(sendDigit),SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);} //Integer.valueOf(sendDigit) 设置串口速率 //SerialPort.DATABITS_8 数据位8位 // SerialPort.STOPBITS_1 停止位1们 //SerialPort.PARITY_NONE 奇偶校验无 catch(UnsupportedCommOperationException e){System.out.println("不支持通信");} try { outputStream=serialPort.getOutputStream(); inputStream=serialPort.getInputStream(); } catch(IOException e){System.out.println("无法初始化输入输出流!");} serialPort.notifyOnDataAvailable(true);}public void write1(byte[]s){ try {outputStream.write(s); outputStream.flush(); } catch(IOException e){System.out.println("往串口写入结尾发生错误!");}}public void close1(){try{ outputStream.close(); inputStream.close(); }catch(IOException e){System.out.println("输入输出流关闭失败!");}finally{serialPort.close();}}}
解决方案
每20秒循环一次,第一次循环所有的串口发数据时有问题吗?
解决方案二:
代码贴一下