From e47d11017af9eff6581591d5d73c1e55676b0955 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期四, 05 六月 2025 09:16:24 +0800 Subject: [PATCH] 优化执行内存 --- README.md | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 177 insertions(+), 1 deletions(-) diff --git a/README.md b/README.md index e1d63f9..393f5e4 100644 --- a/README.md +++ b/README.md @@ -154,4 +154,180 @@ 1. 淇敼瀹炰綋绫绘枃浠朵腑鐨勫瓧娈靛畾涔� 2. 淇敼瀵瑰簲鐨凷QL鏂囦欢涓殑琛ㄧ粨鏋勫畾涔� -3. 鎵цSQL璇彞鏇存柊鏁版嵁搴撹〃缁撴瀯 \ No newline at end of file +3. 鎵цSQL璇彞鏇存柊鏁版嵁搴撹〃缁撴瀯 + +## 鎬ц兘浼樺寲 + +绯荤粺鍦ㄥ鐞嗗ぇ閲忔暟鎹椂鍙兘浼氶亣鍒板唴瀛樺崰鐢ㄨ繃楂樼殑闂锛岀壒鍒槸鍦ㄦ墽琛屼互涓嬫搷浣滄椂锛� + +1. 閲囪喘璁㈠崟鏁版嵁鍚屾(`syncPurchaseOrderDetails`鏂规硶) +2. 閫佽揣鍗曟暟鎹鐞� + +### 宸插疄鏂界殑浼樺寲鎺柦 + +1. **鍒嗘壒澶勭悊鏁版嵁**锛氬皢澶ч噺鏁版嵁鍒嗘垚灏忔壒娆¤繘琛屽鐞嗭紝鍑忓皯涓�娆℃�у唴瀛樺崰鐢� +2. **鍙婃椂閲婃斁瀵硅薄寮曠敤**锛氬鐞嗗畬姣曞悗灏嗕笉鍐嶄娇鐢ㄧ殑瀵硅薄寮曠敤璁句负null锛屽府鍔〨C鍥炴敹鍐呭瓨 +3. **浼樺寲SQL鏌ヨ**锛氶伩鍏嶄竴娆℃�у姞杞藉ぇ閲忔暟鎹埌鍐呭瓨 + +### JVM璋冧紭寤鸿 + +鍦ㄥ惎鍔ㄥ簲鐢ㄦ椂锛屽彲浠ラ�氳繃浠ヤ笅JVM鍙傛暟浼樺寲鍐呭瓨浣跨敤锛� + +```bash +java -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar XkyCollection.jar +``` + +鍙傛暟璇存槑锛� + +- `-Xms512m`锛氬垵濮嬪爢鍐呭瓨澶у皬涓�512MB +- `-Xmx1024m`锛氭渶澶у爢鍐呭瓨澶у皬涓�1024MB锛堟牴鎹湇鍔″櫒瀹為檯鍙敤鍐呭瓨璋冩暣锛� +- `-XX:MetaspaceSize=128m`锛氬垵濮嬪厓绌洪棿澶у皬涓�128MB +- `-XX:MaxMetaspaceSize=256m`锛氭渶澶у厓绌洪棿澶у皬涓�256MB +- `-XX:+UseG1GC`锛氫娇鐢℅1鍨冨溇鏀堕泦鍣紝閫傚悎澶у唴瀛樺簲鐢� +- `-XX:MaxGCPauseMillis=200`锛氭渶澶C鏆傚仠鏃堕棿鐩爣涓�200姣 + +### 鐩戞帶寤鸿 + +1. 浣跨敤JConsole鎴朧isualVM绛夊伐鍏风洃鎺у簲鐢ㄥ唴瀛樹娇鐢ㄦ儏鍐� +2. 鍏虫敞GC鏃ュ織锛屽垎鏋愬唴瀛樹娇鐢ㄦā寮� +3. 鍦ㄧ敓浜х幆澧冧腑锛屽彲浠ユ坊鍔犱互涓嬪弬鏁板紑鍚疓C鏃ュ織锛� + ``` + -Xloggc:/path/to/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps + ``` + +## 甯歌闂鎺掓煡 + +### 鍐呭瓨婧㈠嚭锛圤utOfMemoryError锛� + +濡傛灉閬囧埌鍐呭瓨婧㈠嚭闂锛� + +1. 妫�鏌ユ槸鍚︽湁澶ч噺鏁版嵁涓�娆℃�у姞杞藉埌鍐呭瓨 +2. 纭鏄惁鏈夊唴瀛樻硠婕忥紙瀵硅薄鍒涘缓鍚庢湭琚噴鏀撅級 +3. 澧炲姞JVM鍫嗗唴瀛樺ぇ灏� +4. 鑰冭檻浣跨敤鍒嗛〉鏌ヨ鎴栨祦寮忓鐞嗗ぇ鏁版嵁閲� + +### 鏈嶅姟鍗¢】 + +濡傛灉鏈嶅姟鍑虹幇鍗¢】锛� + +1. 妫�鏌ユ槸鍚︽湁闀挎椂闂磋繍琛岀殑浜嬪姟 +2. 纭鏁版嵁搴撹繛鎺ユ槸鍚︽甯搁噴鏀� +3. 鏌ョ湅GC鏃ュ織锛岀‘璁ゆ槸鍚﹂绻佸彂鐢烣ull GC +4. 浼樺寲鏁版嵁搴撴煡璇紝娣诲姞閫傚綋鐨勭储寮� + +## 鏈�浣冲疄璺� + +1. 閬垮厤鍦ㄩ珮宄版湡鎵ц澶ч噺鏁版嵁鍚屾鎿嶄綔 +2. 瀵逛簬瀹氭椂浠诲姟锛岄�夋嫨鍦ㄧ郴缁熻礋杞借緝浣庣殑鏃舵鎵ц +3. 浣跨敤閫傚綋鐨勬壒澶勭悊澶у皬锛堝缓璁�100-500鏉¤褰曪級 +4. 瀹氭湡娓呯悊涓嶅啀闇�瑕佺殑鍘嗗彶鏁版嵁 + +## 寮傛浠诲姟澶勭悊 + +绯荤粺浣跨敤寮傛浠诲姟澶勭悊鏈哄埗鏉ユ墽琛岃�楁椂鎿嶄綔锛岄伩鍏嶉樆濉炰富绾跨▼鍜屽畾鏃朵换鍔¤皟搴︾嚎绋嬨�備富瑕佸寘鎷互涓嬪嚑涓儴鍒嗭細 + +### 绾跨▼姹犻厤缃� + +绯荤粺閰嶇疆浜嗕袱涓笓鐢ㄧ嚎绋嬫睜锛� + +1. **閲囪喘璁㈠崟鍚屾绾跨▼姹� (purchaseTaskExecutor)** + - 鏍稿績绾跨▼鏁帮細1锛堢‘淇濆悓涓�鏃堕棿鍙湁涓�涓噰璐悓姝ヤ换鍔″湪鎵ц锛� + - 鏈�澶х嚎绋嬫暟锛�2 + - 闃熷垪瀹归噺锛�5 + - 鎷掔粷绛栫暐锛欳allerRunsPolicy锛堣皟鐢ㄨ�呯嚎绋嬫墽琛岋級 + +2. **閫氱敤寮傛浠诲姟绾跨▼姹� (taskExecutor)** + - 鏍稿績绾跨▼鏁帮細5 + - 鏈�澶х嚎绋嬫暟锛�10 + - 闃熷垪瀹归噺锛�25 + - 鎷掔粷绛栫暐锛欳allerRunsPolicy锛堣皟鐢ㄨ�呯嚎绋嬫墽琛岋級 + +### 瀹氭椂浠诲姟浼樺寲 + +鎵�鏈夊畾鏃朵换鍔¢兘浣跨敤寮傛鎵ц鏂瑰紡锛岄槻姝簰鐩搁樆濉烇細 + +1. **閲囪喘璁㈠崟鍚屾浠诲姟**锛氭瘡澶�12:05鎵ц涓�娆★紝浣跨敤涓撶敤绾跨▼姹� +2. **璁惧瀹炴椂鏁版嵁鑾峰彇**锛氭瘡5鍒嗛挓鎵ц涓�娆★紝浣跨敤閫氱敤绾跨▼姹� +3. **琛ュ伩閫昏緫**锛氭瘡5鍒嗛挓鎵ц澶氭锛屼娇鐢ㄩ�氱敤绾跨▼姹� +4. **閽夐拤鏁版嵁鍚屾**锛氭瘡53鍒嗛挓鎵ц涓�娆★紝浣跨敤閫氱敤绾跨▼姹� + +### 浠诲姟鎵ц鐘舵�佺鐞� + +浣跨敤 AtomicBoolean 鏍囪浠诲姟鎵ц鐘舵�侊紝閬垮厤鍚屼竴浠诲姟閲嶅鎵ц锛� + +```java +private final AtomicBoolean isRunning = new AtomicBoolean(false); + +// 浠诲姟寮�濮嬪墠妫�鏌� +if(!isRunning. + +compareAndSet(false,true)){ + log. + +info("涓婁竴娆′换鍔¤繕鍦ㄦ墽琛屼腑锛岃烦杩囨湰娆℃墽琛�"); + return; + } + +// 浠诲姟缁撴潫鍚庨噸缃姸鎬� + finally{ + isRunning. + +set(false); +} +``` + +### 寮傛浠诲姟鎵ц娴佺▼ + +1. 瀹氭椂鍣ㄨЕ鍙戜换鍔� +2. 妫�鏌ヤ换鍔℃槸鍚﹀凡鍦ㄨ繍琛岋紝濡傚凡杩愯鍒欒烦杩� +3. 灏嗕换鍔℃彁浜ゅ埌鐩稿簲鐨勭嚎绋嬫睜寮傛鎵ц +4. 瀹氭椂鍣ㄧ珛鍗宠繑鍥烇紝涓嶇瓑寰呬换鍔″畬鎴� +5. 浠诲姟鍦ㄧ嚎绋嬫睜涓墽琛屽畬姣曞悗閲嶇疆鐘舵�佹爣璁� + +杩欑鏈哄埗纭繚浜嗗嵆浣挎煇涓换鍔℃墽琛屾椂闂磋緝闀匡紝涔熶笉浼氬奖鍝嶅叾浠栧畾鏃朵换鍔$殑姝e父鎵ц銆� + +## 鏈�鏂颁紭鍖栨洿鏂� + +鍦ㄦ渶杩戠殑浼樺寲涓紝鎴戜滑杩涗竴姝ユ敼杩涗簡绯荤粺鎬ц兘鍜岀ǔ瀹氭�э細 + +1. **閫佽揣鍗曟暟鎹鐞嗕紭鍖�** + - 瀹炵幇浜� XkyService.GetSaveDetail() 鏂规硶鐨勫垎鎵瑰鐞� + - 姣忔壒澶勭悊10鏉¢�佽揣鍗曟暟鎹紝鍑忓皯鍐呭瓨鍗犵敤 + - 澧炲己浜嗗紓甯稿鐞嗭紝鍗曟潯鏁版嵁寮傚父涓嶄細褰卞搷鏁存壒澶勭悊 + +2. **寮傛浠诲姟澶勭悊澧炲己** + - 涓� DeliveryNoticeService 娣诲姞浜� @Async 娉ㄨВ鐨勫紓姝ュ鐞嗘柟娉� + - 瀹炵幇浜� processAsyncBatch 鏂规硶锛屾敮鎸佸苟琛屽鐞嗗鎵规暟鎹� + - 浼樺寲浜嗘棩蹇楄褰曪紝渚夸簬闂鎺掓煡 + +3. **閿欒澶勭悊鏀硅繘** + - 鎵�鏈夊叧閿柟娉曢兘娣诲姞浜嗚缁嗙殑鏃ュ織璁板綍 + - 瀹炵幇浜嗘洿缁嗙矑搴︾殑寮傚父鎹曡幏鍜屽鐞� + - 闃叉鍗曚釜浠诲姟澶辫触瀵艰嚧鏁翠釜娴佺▼涓柇 + +### 寮傛鎵ц娴佺▼绀轰緥 + +浠ヤ笅鏄竴涓吀鍨嬬殑寮傛鎵ц娴佺▼锛� + +``` +涓荤嚎绋�: 寮�濮嬪鐞�100鏉℃暟鎹� +涓荤嚎绋�: 灏嗘暟鎹垎涓�5鎵癸紝姣忔壒20鏉� +涓荤嚎绋�: 鎻愪氦鎵规1鍒板紓姝ョ嚎绋嬫睜 +涓荤嚎绋�: 鎻愪氦鎵规2鍒板紓姝ョ嚎绋嬫睜 +涓荤嚎绋�: 鎻愪氦鎵规3鍒板紓姝ョ嚎绋嬫睜 +涓荤嚎绋�: 鎻愪氦鎵规4鍒板紓姝ョ嚎绋嬫睜 +涓荤嚎绋�: 鎻愪氦鎵规5鍒板紓姝ョ嚎绋嬫睜 +涓荤嚎绋�: 鍏ㄩ儴鏁版嵁澶勭悊鎻愪氦瀹屾垚 +寮傛绾跨▼1: 寮�濮嬪鐞嗘壒娆�1 +寮傛绾跨▼2: 寮�濮嬪鐞嗘壒娆�2 +寮傛绾跨▼3: 寮�濮嬪鐞嗘壒娆�3 +寮傛绾跨▼1: 鎵规1澶勭悊瀹屾垚 +寮傛绾跨▼1: 寮�濮嬪鐞嗘壒娆�4 +寮傛绾跨▼2: 鎵规2澶勭悊瀹屾垚 +寮傛绾跨▼2: 寮�濮嬪鐞嗘壒娆�5 +寮傛绾跨▼3: 鎵规3澶勭悊瀹屾垚 +寮傛绾跨▼1: 鎵规4澶勭悊瀹屾垚 +寮傛绾跨▼2: 鎵规5澶勭悊瀹屾垚 +``` + +杩欑鏂瑰紡纭繚浜嗕富绾跨▼涓嶄細琚暱鏃堕棿闃诲锛屽悓鏃跺厖鍒嗗埄鐢ㄤ簡绯荤粺璧勬簮杩涜骞惰澶勭悊銆� \ No newline at end of file -- Gitblit v1.9.3