Fixing “Cannot allocate memory” with Solr 5.2.1

If you run Solr on a VM, it may appear to start and fail.

bin/solr start -p 8000 -f

If you run it with “-f” (preventing backgrounding the process) you will then seen an out of memory error:

Starting Solr on port 8983 from /root/solr-5.2.1/server

OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000e8000000, 402653184, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 402653184 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /root/solr-5.2.1/server/hs_err_pid16824.log

This is easily fixed by adding a memory argument (-m) – the default is 512 so if you’re hitting this error you’re likely using a cheap VM and need to lower this, or rebuild it with more RAM:

bin/solr start -p 8000 -f -m 256m

This is what you should see:

Starting Solr on port 8000 from /root/solr-5.2.1/server

0    [main] INFO  org.eclipse.jetty.util.log  [   ] – Logging initialized @1403ms
816  [main] INFO  org.eclipse.jetty.server.Server  [   ] – jetty-9.2.10.v20150310
914  [main] WARN  org.eclipse.jetty.server.handler.RequestLogHandler  [   ] – !RequestLog
948  [main] INFO  org.eclipse.jetty.deploy.providers.ScanningAppProvider  [   ] – Deployment monitor [file:/root/solr-5.2.1/server/contexts/] at interval 0
3991 [main] INFO  org.eclipse.jetty.webapp.StandardDescriptorProcessor  [   ] – NO JSP Support for /solr, did not find org.apache.jasper.servlet.JspServlet
4030 [main] WARN  org.eclipse.jetty.security.SecurityHandler  [   ] – ServletContext@o.e.j.w.WebAppContext@457e2f02{/solr,file:/root/solr-5.2.1/server/solr-webapp/webapp/,STARTING}{/solr.war} has uncovered http methods for path: /
4136 [main] INFO  org.apache.solr.servlet.SolrDispatchFilter  [   ] – SolrDispatchFilter.init()WebAppClassLoader=1582797472@5e5792a0
4164 [main] INFO  org.apache.solr.core.SolrResourceLoader  [   ] – JNDI not configured for solr (NoInitialContextEx)
4165 [main] INFO  org.apache.solr.core.SolrResourceLoader  [   ] – using system property solr.solr.home: /root/solr-5.2.1/server/solr
4167 [main] INFO  org.apache.solr.core.SolrResourceLoader  [   ] – new SolrResourceLoader for directory: '/root/solr-5.2.1/server/solr/'
4392 [main] INFO  org.apache.solr.core.SolrXmlConfig  [   ] – Loading container configuration from /root/solr-5.2.1/server/solr/solr.xml
4534 [main] INFO  org.apache.solr.core.CoresLocator  [   ] – Config-defined core root directory: /root/solr-5.2.1/server/solr
4566 [main] INFO  org.apache.solr.core.CoreContainer  [   ] – New CoreContainer 2073621255
4567 [main] INFO  org.apache.solr.core.CoreContainer  [   ] – Loading cores into CoreContainer [instanceDir=/root/solr-5.2.1/server/solr/]
4567 [main] INFO  org.apache.solr.core.CoreContainer  [   ] – loading shared library: /root/solr-5.2.1/server/solr/lib
4568 [main] WARN  org.apache.solr.core.SolrResourceLoader  [   ] – Can't find (or read) directory to add to classloader: lib (resolved as: /root/solr-5.2.1/server/solr/lib).
4603 [main] INFO  org.apache.solr.handler.component.HttpShardHandlerFactory  [   ] – created with socketTimeout : 600000,connTimeout : 60000,maxConnectionsPerHost : 20,maxConnections : 10000,corePoolSize : 0,maximumPoolSize : 2147483647,maxThreadIdleTime : 5,sizeOfQueue : -1,fairnessPolicy : false,useRetries : false,
5305 [main] INFO  org.apache.solr.update.UpdateShardHandler  [   ] – Creating UpdateShardHandler HTTP client with params: socketTimeout=600000&connTimeout=60000&retry=true
5311 [main] INFO  org.apache.solr.logging.LogWatcher  [   ] – SLF4J impl is org.slf4j.impl.Log4jLoggerFactory
5327 [main] INFO  org.apache.solr.logging.LogWatcher  [   ] – Registering Log Listener [Log4j (org.slf4j.impl.Log4jLoggerFactory)]
5330 [main] INFO  org.apache.solr.core.CoreContainer  [   ] – Node Name:
5331 [main] INFO  org.apache.solr.core.CoreContainer  [   ] – No authentication plugin used.
5547 [main] INFO  org.apache.solr.core.CoresLocator  [   ] – Looking for core definitions underneath /root/solr-5.2.1/server/solr
5599 [main] INFO  org.apache.solr.core.CoresLocator  [   ] – Found 0 core definitions
5609 [main] INFO  org.apache.solr.servlet.SolrDispatchFilter  [   ] – user.dir=/root/solr-5.2.1/server
5610 [main] INFO  org.apache.solr.servlet.SolrDispatchFilter  [   ] – SolrDispatchFilter.init() done
5651 [main] INFO  org.eclipse.jetty.server.handler.ContextHandler  [   ] – Started o.e.j.w.WebAppContext@457e2f02{/solr,file:/root/solr-5.2.1/server/solr-webapp/webapp/,AVAILABLE}{/solr.war}
5693 [main] INFO  org.eclipse.jetty.server.ServerConnector  [   ] – Started ServerConne