title: Java Memory Leak in HTTP requests
body: <p>I have a Java application that is runs constantly. This application makes HTTP requests to a cloud server. The problem is that at each request the memory consumption increases until it reaches the point that the machine complete freezes. I isolated parts of the code and I'm sure the problem is with this code block making this http requests. Analyzing the JVM numbers, via prometheus / Grafana, I see that the use of non-heap memory (codecache and metaspace) are constantly increasing, as shown <a href="https://i.stack.imgur.com/lC5V1.png" rel="nofollow noreferrer">here</a></p>
  <p>In the image above, whenever there is a drop in the line, it is when 98% of memory consumption reached, and Monit kills the app.</p>
  <p>The method that is causing this memory consumption, is below (it is executed approx. 300 times until it exhausts a little more than 1.5GB of available memory in the initialization).</p>
  <pre><code>public AbstractRestReponse send(RestRequest request){
          BufferedReader in = null;
          OutputStream fout = null;
          URLConnection conn = null;
          InputStreamReader inputStreamReader = null;
          String result = &quot;&quot;;
          try {
              MultipartEntityBuilder mb = MultipartEntityBuilder.create();// org.apache.http.entity.mime
              for (String key : request.getParams().keySet()) {
                  String value = (String) request.getParams().get(key);
  //                System.out.println(key + &quot; = &quot; + value);
                  mb.addTextBody(key, value);
              }
              
              if (request.getFile() != null) {
                  mb.addBinaryBody(&quot;file&quot;, request.getFile());
              }
              org.apache.http.HttpEntity e = mb.build();
      
              conn = new URL(request.getUrl()).openConnection();
              conn.setDoOutput(true);
              conn.addRequestProperty(e.getContentType().getName(), e.getContentType().getValue());// header &quot;Content-Type&quot;...
              conn.addRequestProperty(&quot;Content-Length&quot;, String.valueOf(e.getContentLength()));
              fout = conn.getOutputStream();
              e.writeTo(fout);// write multi part data...
              
              inputStreamReader = new InputStreamReader(conn.getInputStream());
              in = new BufferedReader(inputStreamReader);
              String line;
              while ((line = in.readLine()) != null) {
                  result += line;
              }
              String text = result.toString(); 
              return objectMapper.readValue(text, FacialApiResult.class);
              
          }catch (Exception e) {
              e.printStackTrace();
              return null;
          }finally {
              try {
                  inputStreamReader.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  conn.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  fout.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  in.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              
          }
          
      }
  </code></pre>