I have a Java client – server application. The client is designed to run arbitrary user code. If the user code running on the client creates an OutOfMemoryError then the client ends up in an ugly state. Normally the client would be sending messages (via RMI) to the server until the code the client is running terminates and the client gracefully disconnects with the server.
What would people recommend for the OOM situation on the client? Could I catch it and kill the client process? Presumably I would not be able to push any commands out from the server because the client will be unresponsive.
If possible, I would like the client process to be terminated without having to log on to the client machine and kill it “by hand”. Clearly it would be best if the user code did not cause this error in the first place, but that is up to the user to debug and my framework to deal with in as friendly way as possible. Thanks for your ideas!
It’s a bad idea to catch
OutOfMemoryError. If you catch it, you’ll probably not have enough memory to kill the process anyway…One good practice when developing a server side application is to use a socket timeout. If your client doesn’t send any command for a given amount of time, the connection is dropped. This makes your server more reliable, more secure, and prevents situations like yours happening.
Another thing you can do is to try to make your client app “
OutOfMemoryErrorproof”, not in the way that it can’t run out of memory, but in the way that it shouldn’t make your application crash.