title: How to make sure Solr/Lucene won't die with java.lang.OutOfMemoryError?
body: <p>I'm really puzzled why it keeps dying with java.lang.OutOfMemoryError during indexing even though it has a few GBs of memory.</p>
  
  <p>Is there a fundamental reason why it needs manual tweaking of config files / jvm parameters instead of it just figuring out how much memory is available and limiting itself to that? No other programs except Solr ever have this kind of problem.</p>
  
  <p>Yes, I can keep tweaking JVM heap size every time such crashes happen, but this is all so backwards.</p>
  
  <p>Here's stack trace of the latest such crash in case it is relevant:</p>
  
  <pre><code>SEVERE: java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOfRange(Arrays.java:3209)
      at java.lang.String.&lt;init&gt;(String.java:216)
      at org.apache.lucene.index.TermBuffer.toTerm(TermBuffer.java:122)
      at org.apache.lucene.index.SegmentTermEnum.term(SegmentTermEnum.java:169)
      at org.apache.lucene.search.FieldCacheImpl$StringIndexCache.createValue(FieldCacheImpl.java:701)
      at org.apache.lucene.search.FieldCacheImpl$Cache.get(FieldCacheImpl.java:208)
      at org.apache.lucene.search.FieldCacheImpl.getStringIndex(FieldCacheImpl.java:676)
      at org.apache.lucene.search.FieldComparator$StringOrdValComparator.setNextReader(FieldComparator.java:667)
      at org.apache.lucene.search.TopFieldCollector$OneComparatorNonScoringCollector.setNextReader(TopFieldCollector.java:94)
      at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:245)
      at org.apache.lucene.search.Searcher.search(Searcher.java:171)
      at org.apache.solr.search.SolrIndexSearcher.getDocListNC(SolrIndexSearcher.java:988)
      at org.apache.solr.search.SolrIndexSearcher.getDocListC(SolrIndexSearcher.java:884)
      at org.apache.solr.search.SolrIndexSearcher.search(SolrIndexSearcher.java:341)
      at org.apache.solr.handler.component.QueryComponent.process(QueryComponent.java:182)
      at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:195)
      at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131)
      at org.apache.solr.core.SolrCore.execute(SolrCore.java:1316)
      at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:338)
      at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>