我有以下函数可以启动一个 jsvc 守护进程来接收 UDP 消息:
@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);
}
超时值为 0 时,套接字会阻塞,直到有另一条消息进来。这就是触发通过以下 while 循环连续运行的原因:
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;
}
}
唯一创建的新对象是下面的 MessageConstructor:
在constructTagMessageFromBinary 函数内部,从ByteArrayInputStream 填充的Message 将接收到的UDP 消息转换为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;
}
最后,消息是一个pojo。
公共类消息{
private int type;
//getters and setters omitted
}
我已将内存泄漏缩小到以下几行:
tmc = new MessageConstructor();
message = tmc.constructMessageFromBinary(rcvMsg);
如果我将它们注释掉,只要守护程序运行,内存就永远不会增长并保持一致。
我在 MessageConstructor 类中做错了什么来接收以下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