问题描述
我有以下函数启动jsvc守护程序来接收UDP消息:
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);
}
超时为0时,套接字阻止直到另一条消息进入。这是触发连续运行以下while循环的原因:
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;
}
}
唯一新的创建的对象是下面的MessageConstructor:
The only new objects created are the MessageConstructor below:
在constructTagMessageFromBinary函数内部,一个从ByteArrayInputStream填充的Message,它将收到的UDP消息转换为int。
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;
}
最后,消息是一个pojo。
Lastly, the message is a pojo.
公共类消息{
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.
我在MessageConstructor类中做错了什么来接收以下stackoverflower:
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
推荐答案
问题是我在循环中打开数据库连接而没有关闭它。
the problem was that I open a database connection in the loop without closing it.
这篇关于抛出无限循环的java stackoverflowerror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!