简体   繁体   English

Java增加堆大小

[英]Java Increase Heap size

I have a java application form which i am accessing records from database,processing it and again updaing the database. 我有一个Java应用程序表单,我正在从数据库访问记录,对其进行处理,然后再次更新数据库。 I am doing this by converting my java application to JAR and executing it from command prompt.During testing there are only 20 records per table in database.For that i am executing the jar as follows to avoid out of memory error, 我是通过将Java应用程序转换为JAR并在命令提示符下执行它来完成此操作的。在测试过程中,数据库中每个表只有20条记录。为此,我按以下方式执行jar以避免内存不足错误,

java -jar -Xmx512m MyApp.jar

If database contains much record(50,000),how to increase the heap size while executing jar or How to increase the heap size dynamically based on needs while executing the jar. 如果数据库包含大量记录(50,000),则如何在执行jar时增加堆大小,或者如何在执行jar时根据需要动态增加堆大小。

Thanks 谢谢

The maximal heap size is set before running the program by using the -Xmx option you already used. 在运行程序之前,通过使用已经使用的-Xmx选项来设置最大堆大小。 Based on this information, the JVM will use as much heap size as it needs, up to the given amount. 根据此信息,JVM将使用所需的堆大小,直到给定的数量。 So if your program only needs 1 MB of RAM, the JVM will only use that amount, if it needs 1G and you only gave it 512M, you will get an OutOfMemoryError. 因此,如果您的程序仅需要1 MB的RAM,则JVM将仅使用该数量,如果需要1G且您仅给它512M,则将收到OutOfMemoryError。

So, basically, you can give your program more heap space if you need to, and the JVM will dynamically adjust the used heap size according to its needs. 因此,基本上,可以根据需要为程序提供更多的堆空间,并且JVM将根据其需要动态调整使用的堆大小。

Sometimes this is not the intended behavior, and the lower bound for heap size can be set with -Xms. 有时这不是预期的行为,可以使用-Xms设置堆大小的下限。 Then the JVM will always reserve a heap of the size between Xms and Xmx. 然后,JVM将始终保留Xms和Xmx之间大小的堆。

-Xmx2048m -XX:-UseGCOverheadLimit

Set this in your VM Arguement. 在虚拟机参数中设置此设置。 You can set less memory size in place of 2048 您可以设置较少的内存大小来代替2048

This will increase your max heapSize to 1Gb. 这会将您的最大heapSize增加到1Gb。

java -Xms256m -Xmx1024m -jar MyApp.jar

where -Xms specifies the initial Java heap size and -Xmx the maximum Java heap size. 其中-Xms指定初始Java堆大小,而-Xmx指定最大Java堆大小。

Parameters to the JVM must be passed before the -jar part. 必须在-jar部分之前传递JVM的参数。 The reason for that can be seen when we execute "java -version" : 当我们执行“ java -version”时,可以看到原因:

Usage: 用法:

java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

The maximum is the maximum heap you would need ever not how much it will actually use. 最大的是你需要永远没有多少将实际使用的最大堆。 It's the point at which you would rather than application crash than keep running. 这是您不希望应用程序崩溃而不是继续运行的点。 The JVM will dynamically manage the memory used up to this point. JVM将动态管理到目前为止使用的内存。

How to increase the heap size dynamically based on needs while executing the jar. 执行jar时如何根据需要动态增加堆大小。

So if you want the JVM to use 256 MB, but you want it to dynamically grow up to 30 GB you would set it like this 因此,如果您希望JVM使用256 MB,但您希望它动态增长到30 GB,则可以这样设置

java -ms256m -mx30g -jar MyApp.jar

Even the minimum is not guaranteed to be used. 即使是最小值也不能保证被使用。 The JVM will do minimal effort to keep the memory usage up to this level. JVM将尽最大努力将内存使用率保持在此水平。 A Hello World program will not use 256 MB of heap no matter what you set the minimum to. 无论您将最小值设置为多少,Hello World程序都不会使用256 MB的堆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM