mesos/marthon調(diào)優(yōu)java heap & mesos allocated memory

最近為了嚴格控制控制每個java程序在mesos上的資源占用,通過cgroup對cpu&mem做了嚴格限制介褥,這樣設置對整個mesos-slave機器起到了保護作用吮廉,但是最近遇到服務經(jīng)常由于oom被kill -9, 個別服務會影響用戶體驗。為了解決這個問題说铃,專門對java Heap和mesos分配的內(nèi)存做了調(diào)優(yōu)访惜。經(jīng)過調(diào)整后,線上節(jié)省了75G的內(nèi)存

==================

JVM Memory Component

JVM-Memory-Area-Parts.jpg
  • Heap: The heap is the runtime data area from which memory for all class instances and arrays is allocated.

    How to determine a proper java Heap size

    • Profile the service (what it does), estimate the request throuput

    • See if the service needs to load the 3rd party date file (such as the robot training datasets, the dataset size is prety large), if yes, the the -Xmx must be larger than the dataset size

    • Configure GC, watch the heap value after each Full GC, that value is the lowest value you should set.

      1. How to config gc in java application
      
      GC_LOG_FILE='service-gc.log' 
      
      java -jar  -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:MetaspaceSize=100m  -Xloggc:$GC_LOG_FILE \
       -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=10M \
       package.jar
      
       the gc.log output:
       
       2017-07-28T05:59:17.749+0800: 49606.298: [Full GC (Ergonomics) [PSYoungGen: 288K->0K(348672K)] [ParOldGen: 699337K->75536K(699392K)] 699625K->75536K(1048064K), [Metaspace: 73744K->73744K(1116160K)], 0.1417127 secs] [Times: user=0.42 sys=0.00, real=0.14 secs]
      

2017-07-28T08:55:43.627+0800: 60192.176: [Full GC (Ergonomics) [PSYoungGen: 256K->0K(348672K)] [ParOldGen: 699270K->72322K(699392K)] 699526K->72322K(1048064K), [Metaspace: 73764K->73764K(1116160K)], 0.1451565 secs] [Times: user=0.43 sys=0.00, real=0.14 secs]
2017-07-28T11:37:47.821+0800: 69916.370: [Full GC (Ergonomics) [PSYoungGen: 384K->0K(347648K)] [ParOldGen: 699259K->73371K(699392K)] 699643K->73371K(1047040K), [Metaspace: 73894K->73894K(1116160K)], 0.1603913 secs] [Times: user=0.48 sys=0.00, real=0.16 secs]
2017-07-28T14:22:50.322+0800: 79818.872: [Full GC (Ergonomics) [PSYoungGen: 416K->0K(348672K)] [ParOldGen: 699275K->73551K(699392K)] 699691K->73551K(1048064K), [Metaspace: 73942K->73918K(1116160K)], 0.1654482 secs] [Times: user=0.49 sys=0.00, real=0.17 secs]

   ```
      
  **In aboue gc log, the service -Xmx is set 1024M, the OldGen in heap changes from  699275K->73551K(699392K), so the full gc collected almost 620M memory, to tune this value we should set the -Xmx larger than 70M (690M - 620M), in this case, we can try to set it as 520M
  `-Xms=512m -Xmx=512m`, then keep watching the gc log to determine if 512m is enough, if this setting is low, OOM error will occur**
  • Method area: Method area is created on virtual machine startup, shared among all java virtual machine threads and it is logically part of heap area. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors.

  • Stack (-Xss: default value 1024K): holds local variables and partial results, and plays a part in method invocation and return. Once a thread is created, it will hold 1024K memory at most, and this can be dynamically expanded, so it means each thread might occupy 1K ~ 1024K, the size depends on the thread function

    How to determine a proper java stack size

    • Check the thread count of the jvm process.

        e.g: java pid is 26065
        
        cat /proc/26065/status  |grep Threads
        Threads:   1569
        
        This java process has 1569 threads, ease thread takes 1k ~ 1024K memory
      
    • Estimate the stack size as per the threads count

        This is really not straightforward, it depends on what the thread does and how it handles the data, but at least each thread won't take more than 1M, if it does, a StackOverfowError will thrown. For this service instance, it occupies 2.8G mem in total (2G is for Heap), so each thread takes 0.5M on average. 
        
      
      

Notes: As per above result, we set -Xms=2048m -Xmx=2048m, and limit the mesos memory as 4096m, if there are more concurrent requests, we can increase mesos limited memory

  • Native Method: full gc will call native method, will use certain amount of memory, this will only take tiny part of the jvm memory

  • pc (Program counter) register: Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method, namely the current method (§2.6) for that thread. If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine's pc register is undefined.

  • Summary: Basically JVM memory size = Heap + Stack. Keep monitoring the full gc and decide the reasonalbe heap value.

Refrence: http://howtodoinjava.com/core-java/garbage-collection/jvm-memory-model-structure-and-components/

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末腻扇,一起剝皮案震驚了整個濱河市债热,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌幼苛,老刑警劉巖窒篱,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡墙杯,警方通過查閱死者的電腦和手機配并,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來霍转,“玉大人荐绝,你說我怎么就攤上這事”芟” “怎么了低滩?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長岩喷。 經(jīng)常有香客問我恕沫,道長,這世上最難降的妖魔是什么纱意? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任婶溯,我火速辦了婚禮,結果婚禮上偷霉,老公的妹妹穿的比我還像新娘迄委。我一直安慰自己,他們只是感情好类少,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布叙身。 她就那樣靜靜地躺著,像睡著了一般硫狞。 火紅的嫁衣襯著肌膚如雪信轿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天残吩,我揣著相機與錄音财忽,去河邊找鬼。 笑死泣侮,一個胖子當著我的面吹牛即彪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播旁瘫,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼祖凫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了酬凳?” 一聲冷哼從身側響起惠况,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎宁仔,沒想到半個月后稠屠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年权埠,在試婚紗的時候發(fā)現(xiàn)自己被綠了榨了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡攘蔽,死狀恐怖龙屉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情满俗,我是刑警寧澤转捕,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站唆垃,受9級特大地震影響五芝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辕万,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一枢步、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧渐尿,春花似錦醉途、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至渔彰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間推正,已是汗流浹背恍涂。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留植榕,地道東北人再沧。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像尊残,于是被迫代替她去往敵國和親炒瘸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容