在運行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」