Hello,

I have found a memory leak in the javax.comm (comm.jar) supplied by Sun Microsystems.

Can anyone verify this for me?

you need the comm.jar, and the win32com.dll file to run this sample app I
wrote. For purposes of seeing the memory grow quickly I'm doing a comport
open and close 3 times a second. But in a normal environment it takes about
a week of using the application in real time to crash a system.

If anyone wants the complete comm.jar, and sample code email me at jjakob@onlinetel.com
and I'll send it to you.

see code below for the WatchDogPinger class:


// written by: Jason Jakob jjakob@onlinetel.com
// This application pings a com port at a specified interval, which signifies
// to a WatchDog card that the system is still functioning.
// on termination of this program, the watchdog will reboot a system.
// usefull for telco sytems, pc based or otherwise.

// CURRENTLY THIS SAMPLE HAS A MEMORY LEAK! I CANNOT DETERMINE WHERE IT IS
BEING
// CAUSED. I SUSPECT THAT IT IS WITH THE JAVAX.COMM library
// THE SAMPLE IS PINGING EVERY 300 millisec to show you how quickly the
// memory usage increases. It will continue to use memory until the VM
// runs out of memory.

//ANY IDEAS? jjakob@onlinetel.com

import java.util.*;
import javax.comm.*;

public class WatchDogPinger extends Thread {
private CommPortIdentifier portId = null;
boolean exitSystem = false;
boolean isDone = false;
String comPort = null;
int pingIntervalInMillisec=1000;

public WatchDogPinger(String comPort, int pingIntervalInMillisec) {
//copy our values
this.comPort = comPort;
this.pingIntervalInMillisec = pingIntervalInMillisec;

//start our watchdog thread pinging the com port
this.start();

//wait for user input to kill the app
System.out.println("\n\nPRESS Q TO QUIT:\n\n");
String inString = "";
while (!inString.equals("Q") && !inString.equals("q")) {
byte[] inData = new byte[10];
try {
System.in.read(inData);
} catch(Exception e) {}
inString = (new String(inData)).trim();
}

//shut the thread down gracefully
exitSystem();
while (!isDone()) {
try { Thread.sleep(250); } catch(Exception ioe) {}
}

//goodbye
}

private void Log(String text) {
try {
System.out.println("System Time: " + System.currentTimeMillis() + " "
+ text);
} catch(Exception e) {
Log("ERROR writing to log file " + e.getMessage());
}
}

public void exitSystem() {
Log("STOPPING WATCHDOG SYSTEM");
exitSystem = true;
}

public boolean isDone() {
return isDone;
}

public void run() {
Log("STARTING WATCHDOG SYSTEM");

try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
if (portList.hasMoreElements()) {
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
break; //we found our port so kick out
} else
portId = null;
} else
portId = null;
}
} else {
Log("No serial ports found");
}
} catch (Exception e) {
Log("error writing data " + e.getMessage());
}

//start loop to send data
while (!exitSystem) {
if(portId!=null)
SendData();
else
Log("Invalid Port specified!");

try {
//System.gc();
sleep(pingIntervalInMillisec);
} catch(Exception e) {}
}

isDone = true;
}

private void SendData() {
SerialPort serialPort = null;

try {
serialPort = (SerialPort)portId.open("WatchDogApp", 2000);

Log("Pinging() Port: " + comPort);

//we don't really need to send anything just opening
//it is enough for the watchdog card

if(serialPort!=null)
serialPort.close();
serialPort = null;

} catch (Exception e) {
Log("error opening port " + e.getMessage());
}
}

public static void main(String[] args) {
WatchDogPinger sw = null;

if (args.length>0) {
try {
sw = new WatchDogPinger(args[1], //com port to ping
Integer.parseInt(args[2])); //interval in milliseconds
} catch (Exception e) {
System.out.println("usage:\n\t WatchDogPinger [comport] [pingintervalinmillisec]\nexample:\n\tjava
-classpath comm.jar WatchDogPinger COM1 60000\n");
}
} else {
sw = new WatchDogPinger("COM1", //com port to ping
300); //interval in milliseconds
}
}
}