java虛擬機內存不足,“Could not create the Java Virtual Machine”問題解決方案



java虛擬機內存不足,“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”



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM