I'm working on an application to manage a database. It's designed as a server/client, with the server dealing with ALL the database's transactions.
It kinda works like this:
- When server starts, it opens a socket and starts listening on port.
When client starts, it searches for the socket/port and if open, the server launches a new thread:
while (true) { new Thread(new comm.Protocol(serverSocket.accept()).start(); }
Once in the thread, the server creates the needed objects for communication (ObjectInputStream & ObjectOutputStream), and waits for the client to send the login info. Upon reception, the server tries to create a new connection:
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection(dbName, username, password);
If successful, it saves the connection and keeps looping on waiting for the client's request. Communication is made with a DTO, which contains the client's request type and data objects needed for the queries. The server takes the DTO and switches the request type, performing the requested query. It then sets the result in the form of a Plain Old Java Object which models the queried table, or a java.util.ArrayList<POJO-Typed-Object> for multiresult queries on the DTO and sends it back to the client.
It apparently works fine, the thing is, after some queries, specially the ones with a large quantity of rows, the server application runs out of memory.
PreparedStatements are closed after usage and so are ResultSets, so I don't have a clue where's the memory going.
Any suggestion will be greatly appreciated.
java.lang.OutOfMemoryError: Java Heap space, that's the error. @Brandon, the thread keeps looping until an exit request is made, then the loop is terminated.