1. 程式人生 > >IdeaxError: Could not create the Java Virtual Machine.

IdeaxError: Could not create the Java Virtual Machine.

在執行java程式時,遇到問題"Could not create the Java Virtual Machine."如下截圖:

大概原因,就是java堆記憶體不足以執行JVM,需要增加記憶體。

網上搜索此問題,大部分都是針對某個程式進行修改JVM記憶體的解決方法,比如eclipse,等。試問,若是其他程式出現問題了呢?

現在給出一個全域性的java虛擬機器修改記憶體的方法。在WIN XP,WIN 7,WIN8都可以。

解決方案:增加一個系統環境變數

變數名:_JAVA_OPTIONS

變數值:-Xmx512M

儲存後,就OK!!

下面給出關於java堆記憶體的一個介紹,這是一個英文網頁的翻譯過來的。

關於java堆記憶體:

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:

  • -Xmx to set the maximum heap memory size
  • -Xms to set the minimum heap memory size  

Tips to set the Java heap memory size

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.  

Do not set -Xmx to too small value 

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. 

Set -Xms to a small value

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. 

How to set java heap size in Tomcat 

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堆記憶體:

  • - xmx設定最大的堆記憶體的大小
  • xms設定最小堆記憶體大小

建議設定Java堆記憶體大小

管理伺服器的Java堆記憶體大小是非常重要的整個效能取決於這個記憶體大小,當然還有其他因素影響效能。 所以讓我們來看看你可以設定這些引數來控制Java堆記憶體大小。

不設定- xmx太小价值  

如果你為你的伺服器設定- xmx太小,那麼您的應用程式可能無法正常工作,你可能會得到記憶體不足異常。 所以不要設定過小,因為這是你們的最大記憶體分配為Java和它不能利用記憶體超出設定的值。

它 總是明智的- xmx較高的值,如果你有足夠的記憶體空間可用的伺服器上。 另一方面如果你- xmx值設定為較高的值其他資源將無法執行以及你已經保留你的一些Java記憶體。 所以設定最大堆大小的記憶體之前就檢查多少記憶體是免費的。 為此,停止應用伺服器並檢查空閒記憶體,因此您可以設定最大的記憶體大小。 例如,如果你有512的空閒記憶體,那麼你可以設定堆記憶體300安全即-Xmx300m。

xms設定為一個較小的值

如果你設定xms更高的值可能會耗盡記憶體。 所以總是試圖保持它像-Xms16m一個較小的值。 在tomcat中當你重新啟動伺服器,啟動一個Java程序,過程需要相同的xms tomcat的預設值,如果它是更高的你可能會失去記憶。

現在讓我們看看如何在不同的伺服器上設定java堆記憶體。

如何設定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”