用java 实现串口通讯,先是用线程控制每20秒取一次串口列表,然后分别打开每一个串口,向串口里写东西,再关闭串口。结果发现内存一直增加。我推断是打开串口的方法造成的内存增加,有没有什么办法可以解决? 
报的错误:
nUnsetOwner: Error 8 in MapViewOfFile

Exception in thread "Thread-2" 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 com.sun.comm.Win32SerialPort.<init>(Win32SerialPort.java:51)
at com.sun.comm.Win32Driver.getCommPort(Win32Driver.java:69)
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:314

解决方案 »

  1.   

    socke.close();
    线程也要处理,当所有连接关闭后,让线程处于停止状态,向串口写完东西后调用flush()方法下
      

  2.   

    我使用的是线程调度 每20秒刷新一次。
    public static void main(String args[]) {
    BrilliantCommonSend bcb = new BrilliantCommonSend();
      Thread t = new Thread(bcb);
      t.start();   

    }
    public void run() {
    // TODO Auto-generated method stub
    while (true) {
    BrilliantCommonImpl.getFindAllList();// 写串口的方法
    try {
    Thread.sleep(20000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    其实我觉得就是操作串口的时候,打开串口的时候造成的,内存一直上长。
    我写了一个测试方法
    for(int i=0;i<1000000;i++){
    //BrilliantCommonImpl.getFindAllList();
    port.open1();
    //if (port.checkComport("COM3")) {
    byte[] sendData = bci.dealData("0", "0", "255", "中国人");
    port.write1(sendData);
    System.out.println(i);
    //}
        port.close1();
       
    }
      

  4.   

    能看下BrilliantCommonSend这个类是怎么写的吗
      

  5.   

    我把整个代码提取一下发出来import java.util.Vector;public class BrilliantCommonSend implements Runnable{
    public static void main(String args[]) {
          BrilliantCommonSend bcb = new BrilliantCommonSend ();
      Thread t = new Thread(bcb);
      t.start();   

    }
    public void run() {
    // TODO Auto-generated method stub
    while (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();//关闭串口
    }
    }
    }

    } }package com.brilliant.common;import java.util.*;import java.io.*;import javax.comm.*;public class SerialCommunication{
    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();
    }
    }
    }
      

  6.   

    另外还有一个问题: 我用swing画的界面 ,在swing中启动,运行一段时间后,内存上升了,把swing界面展开或者收起来,内存会降低。 
      

  7.   

    检查一下 程序中 你有没有把什么东西装到容器里 (如Map, List等)。 并且你从来不清理容器。大部分OutofMemory都是这样造成的。另外:楼上说 “导致不停的创建对象,内存撑暴了”, 这是不可能的,JVM会自动回收到没有任何引用的对象,单纯不停的new不会造成内存撑爆的情况,除非 这些对象一直有被引用。