使用maven時,如何修改JVM的配置引數;maven命令執行時到底消耗多少記憶體?

weixin_34292959發表於2018-01-19

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/

相關文章