在运行java程序时,遇到问题"Could not create the Java Virtual Machine."以下截图:java
大概缘由,就是java堆内存不足以运行JVM,须要增长内存。tomcat
网上搜索此问题,大部分都是针对某个程序进行修改JVM内存的解决方法,好比eclipse,等。试问,如果其余程序出现问题了呢?安全
如今给出一个全局的java虚拟机修改内存的方法。在WIN XP,WIN 7,WIN8均可以。服务器
解决方案:增长一个系统环境变量app
变量名:_JAVA_OPTIONSeclipse
变量值:-Xmx512Mide
保存后,就OK!!性能
下面给出关于java堆内存的一个介绍,这是一个英文网页的翻译过来的。this
关于java堆内存:spa
Java -Xmx is the configuration parameter to control the amount of memory Java uses on a system. Basically these settings are there to control the Heap memory size of Java. There are two settings related to Java heap memory:
Managing the Java heap memory size for a server is very crucial as the whole performance depends on this memory size, off course there are other factors which affects the performance. So let’s see how you can set these parameters to control the Java heap memory size.
If you set -Xmx too small for your server then your application may not work properly and you may get Out of memory exception. So never set this too small as this is the maximum amount of memory you are allocating for Java and it cannot utilize memory beyond the set value.
It is always advisable to set -Xmx to a higher value if you have enough memory space available on your server. On the other hand if you set -Xmx value to a higher value your other resources will not be able to perform well as you have already reserved some of your memory for Java. So before setting the maximum heap size memory just check how much memory is free. To do so, stop your application server and check the free memory and accordingly you can set the maximum memory size. For example if you have 512M free memory then you can set heap memory to 300M safely i.e. -Xmx300m.
If you set -Xms to higher value you might run out of memory. So always try to keep it to a small value like -Xms16m. In tomcat when you restart the server it starts a Java process and that process takes the same -Xms as the default value for the tomcat and if it is higher you may get out of memory.
Now let’s see how to set java heap memory in different servers.
To set the java heap size in tomcat you need to edit the catalina.sh (On Linux) or catalina.bat (On Windows) file. You can find these files inside the bin directory of tomcat. Open the appropriate file and search for CATALINA_OPTS and set the value as shown below, you can change the max and min value to whatever you want.
set CATALINA_OPTS=-Xms64m -Xmx512m
export CATALINA_OPTS=”-Xms64m -Xmx512m”
setenv CATALINA_OPTS “-Xms64m -Xmx512m”
大概翻译以下:
Java - xmx配置参数控制Java系统上使用的内存量。 基本上这些设置有控制Java堆内存的大小。 有两种设置相关的Java堆内存:
管理服务器的Java堆内存大小是很是重要的整个性能取决于这个内存大小,固然还有其余因素影响性能。 因此让咱们来看看你能够设置这些参数来控制Java堆内存大小。
若是你为你的服务器设置- xmx过小,那么您的应用程序可能没法正常工做,你可能会获得内存不足异常。 因此不要设置太小,由于这是大家的最大内存分配为Java和它不能利用内存超出设定的值。
它 老是明智的- xmx较高的值,若是你有足够的内存空间可用的服务器上。 另外一方面若是你- xmx值设置为较高的值其余资源将没法执行以及你已经保留你的一些Java内存。 因此设置最大堆大小的内存以前就检查多少内存是免费的。 为此,中止应用服务器并检查空闲内存,所以您能够设置最大的内存大小。 例如,若是你有512的空闲内存,那么你能够设置堆内存300安全即-Xmx300m。
若是你设置xms更高的值可能会耗尽内存。 因此老是试图保持它像-Xms16m一个较小的值。 在tomcat中当你从新启动服务器,启动一个Java进程,过程须要相同的xms tomcat的默认值,若是它是更高的你可能会失去记忆。
如今让咱们看看如何在不一样的服务器上设置java堆内存。
设 置java堆大小在tomcat中您须要编辑catalina.sh(在Linux上)或catalina。 蝙蝠(在Windows上)文件。 你能够找到这些文件在tomcat的bin目录。 打开相应的文件和搜索CATALINA_OPTS和设置值以下所示,你能够改变任何你想要的最大和最小值。
设置CATALINA_OPTS = -Xms64m -Xmx512m
出口CATALINA_OPTS = " -Xms64m -Xmx512m”
setenv CATALINA_OPTS”-Xms64m -Xmx512m”