On JAVA, a socket server is implemented, the database is PostgreSQL. The problem is that when sending JSON messages of 1,500,000 characters in size, the memory grows by an average of 20 MB, when receiving JSON back it is already 60 MB. A JSON parser is used, an exemplary implementation of processing incoming messages:

public static void main(String args[]) { try { Integer b = 0; a = new ServerSocket(8572); new conf("server.json"); ms = new mysql(); ps = new postgresql(); System.out.println("[ur.java] Server started!"); while(true) new ur(b++, a.accept()); } catch(Exception e) { e.printStackTrace(); } } public ur(Integer a, Socket b) throws IOException { this.cb = a; this.ca = b; q.offer(this); ty = new type(this); setDaemon(true); setPriority(MAX_PRIORITY); start(); System.out.println("[ur.java] Machine "+a+" in socket stream!"); br = new BufferedReader(new InputStreamReader(ca.getInputStream(), "UTF-8")); } public void run() { try { String s; while ((s = br.readLine()) != null) ty.data(s.trim()); } catch (Exception e) { e.printStackTrace(); } } 

Outgoing messages from the database:

  ResultSet rs = connection.createStatement().executeQuery("bla bla"); if(rs.next()) { socket.getOutputStream().write((rs.getString(1)+"\0").getBytes("UTF-8")); } 

Since the project consists of a heap of classes, it’s really impossible to lay out everything important, if some kind person has a desire to help, it would be ideal to contact via Skype

  • - memory flows constantly and in the end-end takes all available? Or after some time is fixed at some value and does not change much? - tried to drive through something like mat ? - KoVadim
  • It reaches the maximum and falls a bit, now the test server with 256 MB of RAM, and we are thinking of transferring it to the server for releases, there will be others besides this project and it will be scary if it unloads 24 GB of RAM in the end. Hanging at maximum speed is stable, but sometimes it is impossible to establish a connection with the server. Not tried - GeneralProger2
  • br.flush ()? - Gorets
  • 256 MB for a serious java product may be very small. So it is all available memory and eats away. Does he need to work like that? Since there is not enough memory, then until the PS clears up, the server cannot accept a new connection - it is logical. I recommend to run on a server with 4-8 gigabytes of memory and see. Whatever process has eaten all the memory - can be limited. To do this, you need to run java with special keys - `-Xmx` and -Xms . first heap, second stack, for example java -Xmx1024M -Xms512M your_server - KoVadim
  • In my opinion, you need to use "heavy" variables only once. Can you complete the profiling and add to the question? I after all faced such problem when used FloatBuffer through BufferUtil.allocate (). As a result, I fixed a memory leak by allocating a buffer into the same variable. - SOCIOPATH

0