简体   繁体   中英

Java program Freezes

I have written a java program which reads data from a com port in its own thread and puts it in a database. When exceptions occur (database or com) it restarts itself with runtime.exec. After a day or so the program sometimes freezes. I cannot keep track of when this occurs exactly but it seems to happen after a little while. Does one of you guys know what may be the problem? Thank !

If you simply catch the IO exceptions, and do runtime.exec, you may get out of memory, due to too many JVMs... Are you sure the old program (and JVM) are completely gone when doing a new runtime.exec? That is, as @extraneon put it, are you sure your program is not creating another instance of your program, which in turn creates another instance of your program, ...?

Depending on how you implemented your software you may have a design issue.

If you use Runtime.exec() from a starter application, that would be fine. If you have something like:

try {
    doStuff();
} catch(Throwable t) {
    Runtime.exec( ... );
}

you will get more than one copy of your software running, each taking some resources and never giving those back. In such a case you should actually just clean up the failing thread and start a new thread (if possible).

Just to be clear, a starter app in my view is something with in the main not much more than:

while(true) {
    Process p = Runtime.exec( your COM communication program );
    if ( p.waitFor() == 0 { // Clean exit
      break;  // exit while loop.
    }
    // Otherwise p closed with an error and a new process should be started
    // which is done in the next iteration of the loop
}

When you are starting the processes with Runtime.exec() are you consuming the stdout and stderr streams? If no, the application would freeze when it fills the OS buffer. Depending on how much output does the application produce and how often it throws exceptions, your numbers might vary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM