1. 程式人生 > >使用maven時,如何修改JVM的配置參數;maven命令執行時到底消耗多少內存?

使用maven時,如何修改JVM的配置參數;maven命令執行時到底消耗多少內存?

option oca sin port more https exe exc style

maven是使用java啟動的,因此依賴JVM,那麽如何修改JVM參數?

MAVEN_OPTS

在系統的環境變量中,設置MAVEN_OPTS,用以存放JVM的參數,具體設置的步驟,參數示例如下:

  MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M

  或者臨時設置 export MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M

在mvn中添加MAVEN_OPTS

找到Maven的安裝目錄,在bin目錄下,編輯mvn.bat(linux下,mvn)

  set MAVEN_OPTS=-Xms256m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256M

Vim mvn:

技術分享圖片

設置JVM最大堆heap內存為一個很小的值5m,如圖,報錯:GC overhead limit exceeded

技術分享圖片

查看詳細信息:OOM了

技術分享圖片

關於下圖標識的含義: 27M表示構建結束的時刻所使用的堆heap大小;535M表示構建結束時刻的分配的堆大小

分配的堆大小需要 > 使用的堆大小,不然就會內存溢出OOM

技術分享圖片技術分享圖片

Here :

Final Memory: 47M/535M

47M is the used memory and 535M

the current allocated memory (heap size) by the JVM at the end of the build.
But these information could not give you the peak value.

Why the used memory (43M) cannot be considered as the peak ?

  • Because the peak may have occurred before the end of the build.

Why the heap size (636M) cannot be considered as the peak ?

  • Because the peak may be superior to the heap size if the GC reduced the heap size between the peak and the end of the build .
    It may be the case for a very long build that performs intensive tasks at the beginning and lighter tasks then.

  • Because the peak may be inferior to the heap size if the GC has increased the heap size until this value but the application never needs to consume as much as memory.

To get the memory peak during the execution of the maven build, monitor the Java application (maven execution) with any JVM tool designed for : JVisualVM or JConsole for example.

Here‘s an screenshot of a maven build of a Spring Boot application monitored by JVisualVM that shows that neither the heap size or the used size at the end of build are the peak value :

技術分享圖片

JVisualVM is the best as it can be used to monitor several processes at once. And when running the maven build, make sure to open(double click) the relevant processes generated under the Applicationwindow of the JVisualVM as the build processes are killed once after the built is completed making you unable to monitor the process. By opening the relevant process before it is being killed, will allow to monitor the build process well.

Yes, and if you have e.g. unit tests, the surefire plugin starts parallel an additional process without using MAVEN_OPTS for that.

maven的單元測試插件會另外起個進程,這個進程的堆大小不受MAVEN_OPTS的配置約束,所有說maven命令行執行,內存到底夠不夠,關鍵看兩個參數,一個是主進程的堆heap的峰值peak;一個是單測插件surefire起的進程的所需堆heap的峰值peak。因為一個項目有多個模塊moduls,那就得看最消耗heap的那個模塊的峰值peak

如下圖所示,會在某個時刻出現一個所需堆heap的極值,如果系統不能滿足這個大小,就會OOM(out of memory)內存溢出

技術分享圖片

技術分享圖片

另外surefire插件到底可以jvm再拉出幾個進程,以及每個進程的jvm配置是如何的,都是可以配置的,參考:http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

還有就是surefire可以不另起進程,這樣減少內存的使用,見插件官網說明

至於配置幾個並行進程,就要看設備內存已經項目執行時間要求。要麽時間換空間,要麽空間換時間。Choosing the right forking strategy and parallel execution settings can have substantial impact on the memory requirements and the execution time of your build system.

export MAVEN_OPTS=-Xmx512m這個參數的左右是,可以減少GC的壓力

How do I interpret the "Final Memory" reported by Maven? "66M/188M" for
example, what does each of the figures mean?


In general the JVM only looks for more memory (up to -Xmx) when it needs
it, so consider that to be the peak requirement (not peak usage as the JVM
will always ask for more than usage on the basis that it needs room to
allocate objects)

If you run Maven with -Xmx2048m you will remove the pressing need for GC,
but the total memory might still only be 188MB as that may just have been
the largest amount of that 2048MB that the JVM needed at some point in time
during the build

參考:

1、http://grokbase.com/t/maven/dev/12bvc2s2bt/final-memory-reported-by-maven

2、https://stackoverflow.com/questions/45418317/how-to-find-the-peak-memory-usage-in-a-maven-build?answertab=votes#tab-top

3、http://maven.apache.org/surefire/maven-surefire-plugin/

使用maven時,如何修改JVM的配置參數;maven命令執行時到底消耗多少內存?