java虚拟机内存不足

来源:互联网 发布:手机怎么改淘宝店铺名 编辑:程序博客网 时间:2024/05/21 03:55

在运行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堆大小 

 

设 置或catalina

。 蝙蝠(在Windows上)文件。 你可以找到这些文件在tomcat的bin目录。 打开相应的文件和搜索CATALINA_OPTS和设置值如下所示,你可以改变任何你想要的最大和最小值。

设置CATALINA_OPTS = -Xms64m -Xmx512m

出口CATALINA_OPTS = " -Xms64m -Xmx512m”

setenv CATALINA_OPTS”-Xms64m -Xmx512m”


原创粉丝点击