简单的java守护进程循环接收内存不足异常

时间:2013-06-24 21:11:14

标签: java loops memory-leaks daemon out-of-memory

我有以下java守护进程实现了apache commons守护进程。它运行在带有icedtea6的ec2亚马逊ami上。

public class MainDaemon implements Daemon {

private Thread myThread; 
private boolean stopped = false;

@Override
public void init(DaemonContext daemonContext) throws DaemonInitException, Exception {

    String[] args = daemonContext.getArguments(); 

    myThread = new Thread(){

        @Override
        public synchronized void start() {
            MainDaemon.this.stopped = false;
            super.start();
        }

        @Override
        public void run() {             
            while(!stopped){

                byte[] rcvMsg = incomingBinaryMessage;

                MessageCreator tmc = new MessageCreator();
                Message message = null;
                try {
                    message = tmc.createMessage(rcvMsg);
                System.out.println(message);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
}

@Override
public void start() throws Exception {
    myThread.start();
}

@Override
public void stop() throws Exception {
    stopped = true;
    try{
        myThread.join(1000);
    }catch(InterruptedException e){
        System.err.println(e.getMessage());
        throw e;
    }
}

@Override
public void destroy() {
    myThread = null;
}
}

MessageCreator只是从字节数组输入流中读取数据,将输出放入一个对象并返回它:

public Message createMessage(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:

public class Message {

private int type;

public int getType() {
    return type;
}
public void setType(int type) {
    this.type = type;
}
}

当守护进程运行时,内存缓慢爬升,直到出现内存不足的异常。

我知道这是在messageconstructor类中发生的,因为当我注释掉对此方法的调用时,内存对于守护程序运行的整个会话保持不变。

几小时后我收到以下错误:

Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:3221)
    at java.lang.String.<init>(String.java:233)
    at java.lang.StringBuilder.toString(StringBuilder.java:447)
    at sun.net.www.protocol.jar.Handler.parseContextSpec(Handler.java:138)
    at sun.net.www.protocol.jar.Handler.parseURL(Handler.java:84)
    at java.net.URL.<init>(URL.java:614)
    at java.net.URL.<init>(URL.java:482)
    at sun.misc.URLClassPath$JarLoader.checkResource(URLClassPath.java:688)
    at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:773)
    at sun.misc.URLClassPath.getResource(URLClassPath.java:185)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:209)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:435)
    at com.mysql.jdbc.PreparedStatement.getInstance(PreparedStatement.java:872)
    at com.mysql.jdbc.ConnectionImpl.clientPrepareStatement(ConnectionImpl.java:1490)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4253)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4152)
    at MySQLConnect.insertTagEncounters(MySQLConnect.java:89)
    at DataControllerImp.saveMessage(DataControllerImp.java:35)
    at MainDaemon$1.run(MainDaemon.java:74)

在泄漏内存的运行循环中创建MessageConstructor我做错了什么?

0 个答案:

没有答案