0

I have a simple Java server application implemented using com.sun.net.HttpServer API. It receives REST requests almost every second, reads a large HashMap for some texts and simply sends back the texts after some processing. The server part simply looks like this:

public class Server implements Runnable {
    static final Logger logger = LogManager.getLogger(Server.class);
    static int serverPort = 9002;
    HttpServer server;

    public Server() {
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            server = HttpServer.create(new InetSocketAddress(serverPort), 0);
            logger.info("EventRetriever REST server listening to port: " + serverPort);
            server.createContext("/getEvents", new MedatadaHandler());
            server.setExecutor(null);
            server.start();
        } catch (IOException e) {
            logger.error(e);
        }
    }

    class MedatadaHandler implements HttpHandler {
    @Override
            public void handle(HttpExchange he) throws IOException {
            //...
            String response = requestEvents();
            he.sendResponseHeaders(200, response.length());
            OutputStream os = he.getResponseBody();
            os.write(response.toString().getBytes());
            os.close();
    }
}
//some other functions...

    public String requestEvents(){
    //read from a HashMap, some processing, creating a JSON and sends it while logging it in file
            logger.info(jsonString);
            return jsonString;
    }
}

}

While running as a docker container, I see the memory usage increases over time, like an average of few MB over few seconds. Apparently, somewhere here we have a memory leak. Is it the unused objects, the j4logger loggings, or whatnot, I don't really know. I just tried to call a System.gc() explicitly once every few minutes. It does some remedy, but I am not sure where the problem is coming from.

What is the problem and how to fix it?

EDIT: how do I know the problem is coming from this part of program? Well, I have other thread responsible for filling those HashMaps every 5 mins, then going to sleep. Even when that's on sleep, I see memory increase. The only part is this server which is serving every second.

Tina J
  • 4,983
  • 13
  • 59
  • 125
  • 3
    What makes you think the problem is in *this* section of code? – user207421 Mar 30 '19 at 02:09
  • 1
    We can't find your memory leak for you with the information you have provided. Or even make an *intelligent* guess about where it is. The best we can do is point you at a Q&A that explains how you can do it for yourself; see the dup. – Stephen C Mar 30 '19 at 02:23
  • @user207421 Well, I have other thread responsible for filling those HashMaps every 5 mins, then going to sleep. Even when that's on sleep, I see memory increase. The only part is this server which is serving every second. – Tina J Mar 30 '19 at 02:24
  • @StephenC finding a leak is a generic question. I already read that page before asking for helps here. Its not a duplicate as I provided very specific snippet. – Tina J Mar 30 '19 at 02:25
  • 1
    I know. But you didn't provide an MCVE. And your probably can't. And without an MCVE we cannot help you. Providing you with generic instructions on how to solve your own problem is the best we can do. – Stephen C Mar 30 '19 at 02:29
  • You are creating a new Thread for every `Server` instance. – Aniket Sahrawat Mar 30 '19 at 02:31
  • @AniketSahrawat I just have one server instance. It serves all incoming requests.. – Tina J Mar 30 '19 at 02:33
  • 1
    I can see no leaks in the code you have shown us. Have you used `jhat` or similar to investigate the source of the leaks yet? What is the type / size / count of the objects that are being leaked? – Stephen C Mar 30 '19 at 02:36
  • Actually I did some tests with Eclipse Memory Analyzer. I saved two snapshots one after 10 mins of another. I couldn't find much info :-/ (but it suspected the leak is coming from other threads, not this server!) – Tina J Mar 30 '19 at 02:40
  • 1
    If a memory analyzer can't find anything, there is a distinct possibility that you don't have a memory leak at all. An increase in memory utilization doesn't necessarily mean there is a leak. It could also mean that 1) your application's memory requirements are variable, and/or 2) the GC thinks that the heap needs to be bigger to meet the *ergonomics* goals that have been set for it (explicitly or implicitly). – Stephen C Mar 30 '19 at 04:37
  • @StephenC Interesting. Here is my new question with your dump request: https://stackoverflow.com/questions/55428131 – Tina J Mar 30 '19 at 04:52
  • The increase is unfortunately huge. It went from 450MB to 2.5GB after a few hours. Doesn't look normal. – Tina J Mar 30 '19 at 04:53

0 Answers0