I have the following function that starts a jsvc daemon for receiving UDP messages:
@Override
public void start() throws Exception {
byte[] buf = new byte[1000];
DatagramPacket dgp = new DatagramPacket(buf, buf.length);
DatagramSocket sk;
sk = new DatagramSocket(1000);
sk.setSoTimeout(0);
byte[] rcvMsg = null;
run(sk, dgp, rcvMsg);
}
With a timeout of 0, the socket blocks until a another message comes in. This is what triggers the continuous run through the following while loop:
MessageConstructor tmc =null;
Message message = null;
public void run(DatagramSocket sk, DatagramPacket dgp, byte[] rcvMsg){
while(true){
try {
sk.receive(dgp);
} catch (IOException e) {
e.printStackTrace();
}
rcvMsg = dgp.getData();
tmc = new MessageConstructor();
message = tmc.constructMessageFromBinary(rcvMsg);
tmc =null;
message = null;
}
}
The only new objects created are the MessageConstructor below:
And inside of the constructTagMessageFromBinary function a Message that is populated from a ByteArrayInputStream which converts the received UDP message to an int.
public Message constructTagMessageFromBinary(byte[] rcvMsg) {
Message message = new Message();
ByteArrayInputStream bais = new ByteArrayInputStream(rcvMsg);
DataInput input = new DataInputStream(bais);
try {
int MsgType = 0;
MsgType = input.readShort();
message.setType(MsgType);
return message;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Lastly, the message is a pojo.
public class Message {
private int type;
//getters and setters omitted
}
I have narrowed the memory leak down to the lines:
tmc = new MessageConstructor();
message = tmc.constructMessageFromBinary(rcvMsg);
If I comment them out, the memory never grows and stays consistent for as long as the daemon runs.
What am I doing wrong within the MessageConstructor class to receive the following stackoverflowerror:
Service exit with a return value of 143
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.commons.daemon.support.DaemonLoader.start(DaemonLoader.java:243)
Caused by: java.lang.NullPointerException
at MainDaemon.start(MainDaemon.java:116)
... 5 more
Cannot start daemon
Service exit with a return value of 5
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.commons.daemon.support.DaemonLoader.start(DaemonLoader.java:243)
Caused by: java.lang.NullPointerException
at MainDaemon.start(MainDaemon.java:117)
... 5 more
Cannot start daemon
Service exit with a return value of 5
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
Service exit with a return value of 143
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.commons.daemon.support.DaemonLoader.start(DaemonLoader.java:243)
Caused by: java.lang.StackOverflowError