title: Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread, during an http call
body: <p>Sometimes I'm getting a runtime the error below. </p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
      at java.lang.Thread.start0(Native Method)
      at java.lang.Thread.start(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.addWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
      at okhttp3.internal.connection.RealConnectionPool.put(RealConnectionPool.kt:101)
      at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:258)
      at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:109)
      at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:77)
      at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:162)
      at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:35)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
      at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
      at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
      at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
      at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
      at okhttp3.RealCall.execute(RealCall.kt:66)
      at com.ids.decoretorPatternHttpCall.MTConnect.httpCall(MTConnect.java:39)
      at com.ids.factoryPatternProtocol.CallFactory.checkProtocol(CallFactory.java:100)
      at com.ids.factoryPatternProtocol.CallFactory.listDevices(CallFactory.java:46)
      at com.ids.main.IdsMain.main(IdsMain.java:94)
  </code></pre>
  
  <p>After lots of checks, I would have understood what's happening. When the device switched off, the application recieve a connection time out and this event throw the error above. I've read some online topics about this  problem and it should be the loop code. But now the application has been working for 2h and it's running well.</p>
  
  <p>Method with loop that calls httpcall(). The MTConnect's Constructor receives  the server name(ip) and the port.
  mt.httpCall() return a buffer of byte[], otherwise catch an exception(IOException). But in this case I get OutOfMemoryError. </p>
  
  <pre><code>
  @Override
  protected void checkProtocol(List&lt;DeviceMTConnect&gt; devicesFactory, int line, DeviceDAO devDAO, Logger logger, ConsoleHandler ch, long minute) {
      DeviceMTConnect device = null;
      long time = 0;
      int i = 0;
  
  
      if (minute &gt; 0) {
          time = minute * 60 * 1000L;
      }
  
  
      for (DeviceMTConnect d : devicesFactory) {
          d.setLinea(linea);
          d.setDeviceState(1);
          //  System.out.println(d);
  
  
      }
  
      while (true) {
          try {
              while (i &lt; devicesFactory.size()) {
  
                  devicesFactory.get(i).setStatoDevice(devDAO.getDeviceStatus(devicesFactory.get(i).getDeviceCode(), logger, ch));
  
                  if (devicesFactory.get(i).getDeviceState() &gt; 0) {
                      if (devicesFactory.get(i).getProtocol().equalsIgnoreCase(PROT1)) {
  
                          device = devicesFactory.get(i);
                          MTConnect mt = new MTConnect(device.getIp(), device.getPort());
  
                          ParserMTConnect pmtConnect = new ParserMTConnect()
                        pmtConnect.parser(mt.httpCall(), device, logger, ch);
  
                          //System.out.println();
                      }
                      if (device.getProtocol().equalsIgnoreCase(PROT2)) {
  
                      }
                      i++;
                      Thread.sleep(SleepingTime);
  
                  } else {
                      i++;
                  }
  
              }
          } catch (InterruptedException e) {
  
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
          } catch (NumberFormatException e) {
  
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
  
          } catch (IOException e) {
  
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
  
          } catch (ParserConfigurationException e) {
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
          } catch (SAXException e) {
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
          } catch (ParseException e) {
              logger.warning(e.getMessage());
              Utility.checkDeviceState(devDAO, device, logger, ch, time);
          } finally {
              if (i &lt; devicesFactory.size()) {
                  i++;
              } else if (i == devicesFactory.size()) {
                  i = 0;
              }
          }
      }
  }
  
  </code></pre>
  
  <pre><code>public class MTConnect extends URLPath {
  
  public MTConnect(String serverIP, String port) {
          super(serverIP, port);
  
      }
  
  
      private byte[] buffer;
  
  
      public String getComponetPath() 
      {
          return componentPath="http://"+this.serverIP.toLowerCase()+":"+this.port+"/current?path="+"//Controller/Components/Path/DataItems/DataItem[@type=\"EXECUTION\" or @type=\"PART_COUNT\"]";
      }
  
  
      @Override
  
      public byte[] httpCall() throws IOException,NumberFormatException {
  
          OkHttpClient client = new OkHttpClient();
          Request request = new Request.Builder().url(this.getComponetPath()).build();
  
          Response response;
  
                  response = client.newCall(request).execute();
  
              buffer=response.body().bytes();
  
  
          return buffer;
      }
  
  }
  </code></pre>
  
  <p>I thought to catch OutOfmemoryError, but I found this on StackOverflow:</p>
  
  <blockquote>
    <p>Errors tend to signal the end of your application as you know it. It typically cannot be recovered from and should cause your VM to exit. Catching them should not be done except to possibly log or display and appropriate message before exiting.</p>
  </blockquote>
  
  <p>I'm trying to find out the reason about this error.</p>