title: OutOfMemoryError on Java on macOS
body: <p>I am developing a Java server application which used to run fine on a PC running Debian. Recently, I moved it in production on a MacMini with macOS High Sierra. Everything seemed to be working fine for weeks until recently, I got random OutOfMemoryErrors almost once a day. When it happens, the server app seems to stop responding to most request and I need to kill it and start it again.</p>
  
  <p>When it happens, on the Activity Monitor, my server app is using about about 2GB or 4GB, which is well below the limit I set for the app (which is -Xmx8G).</p>
  
  <p>I don't know where should I look to fix this issue and I need your insight.</p>
  
  <p>Some notes: The MacMini has 16GB. The server is running headlessly on Java SDK 1.8. It is launch under a GNU screen. The app is using a lot of HashMaps and using Lucene with a FSDirectory to index documents and manage queries.</p>
  
  <p><strong>EDIT</strong>: Here's an example of the error:</p>
  
  <pre><code>Exception in thread "NRT Reopen Thread" java.lang.OutOfMemoryError: unable to create new native thread
      at java.lang.Thread.start0(Native Method)
      at java.lang.Thread.start(Thread.java:717)
      at org.apache.lucene.index.ConcurrentMergeScheduler.merge(ConcurrentMergeScheduler.java:553)
      at org.apache.lucene.index.IndexWriter.maybeMerge(IndexWriter.java:2247)
      at org.apache.lucene.index.IndexWriter.getReader(IndexWriter.java:511)
      at org.apache.lucene.index.StandardDirectoryReader.doOpenFromWriter(StandardDirectoryReader.java:293)
      at org.apache.lucene.index.StandardDirectoryReader.doOpenIfChanged(StandardDirectoryReader.java:268)
      at org.apache.lucene.index.StandardDirectoryReader.doOpenIfChanged(StandardDirectoryReader.java:258)
      at org.apache.lucene.index.DirectoryReader.openIfChanged(DirectoryReader.java:140)
      at org.apache.lucene.search.SearcherManager.refreshIfNeeded(SearcherManager.java:156)
      at org.apache.lucene.search.SearcherManager.refreshIfNeeded(SearcherManager.java:58)
      at org.apache.lucene.search.ReferenceManager.doMaybeRefresh(ReferenceManager.java:176)
      at org.apache.lucene.search.ReferenceManager.maybeRefreshBlocking(ReferenceManager.java:253)
      at org.apache.lucene.search.ControlledRealTimeReopenThread.run(ControlledRealTimeReopenThread.java:226)
  </code></pre>