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