Java性能調優筆記 調優步驟:衡量系統現狀、設定調優目標、尋找性能瓶頸、性能調優、衡量是否到達目標(若是未到達目標,需從新尋找性能瓶頸)、性能調優結束。 尋找性能瓶頸 性能瓶頸的表象:資源消耗過多、外部處理系統的性能不足、資源消耗很少但程序的響應速度卻仍達不到要求。 資源消耗:CPU、文件IO、網絡IO、內存。 外部處理系統的性能不足:所調用的其餘系統提供的功能或數據庫操做的響應速度不夠。 資源消耗很少但程序的響應速度卻仍達不到要求:程序代碼運行效率不夠高、未充分使用資源、程序結構不合理。 CPU消耗分析 CPU主要用於中斷、內核、用戶進程的任務處理,優先級爲中斷>內核>用戶進程。 上下文切換: 每一個線程分配必定的執行時間,當到達執行時間、線程中有IO阻塞或高優先級線程要執行時,將切換執行的線程。在切換時要存儲目前線程的執行狀態,並恢復要執行的線程的狀態。 對於Java應用,典型的是在進行文件IO操做、網絡IO操做、鎖等待、線程Sleep時,當前線程會進入阻塞或休眠狀態,從而觸發上下文切換,上下文切換過多會形成內核佔據較多的CPU的使用。 運行隊列: 每一個CPU核都維護一個可運行的線程隊列。系統的load主要由CPU的運行隊列來決定。 運行隊列值越大,就意味着線程會要消耗越長的時間才能執行完成。 利用率: CPU在用戶進程、內核、中斷處理、IO等待、空閒,這五個部分使用百分比。 文件IO消耗分析 Linux在操做文件時,將數據放入文件緩存區,直到內存不夠或系統要釋放內存給用戶進程使用。因此一般狀況下只有寫文件和第一次讀取文件時會產生真正的文件IO。 對於Java應用,形成文件IO消耗高主要是多個線程須要進行大量內容寫入(例如頻繁的日誌寫入)的動做、磁盤設備自己的處理速度慢、文件系統慢、操做的文件自己已經很大。 網絡IO消耗分析 對於分佈式Java應用,網卡中斷是否是均衡分配到各CPU(cat/proc/interrupts查看)。 內存消耗分析(-Xms和-Xmx設爲相同的值,避免運行期JVM堆內存要不斷申請內存) 對於Java應用,內存的消耗主要在Java堆內存上,只有建立線程和使用Direct ByteBuffer纔會操做JVM堆外的內存。 JVM內存消耗過多會致使GC執行頻繁,CPU消耗增長,應用線程的執行速度嚴重降低,甚至形成OutOfMemoryError,最終致使Java進程退出。 JVM堆外的內存 swap的消耗、物理內存的消耗、JVM內存的消耗。 程序執行慢緣由分析 鎖競爭激烈:不少線程競爭互斥資源,但資源有限, 形成其餘線程都處於等待狀態。 未充分使用硬件資源:線程操做被串行化。 數據量增加:單表數據量太大(如1個億)形成數據庫讀寫速度大幅降低(操做此表)。 調優 JVM調優(最關鍵參數爲:-Xms -Xmx -Xmn -XX:SurvivorRatio -XX:MaxTenuringThreshold) 代大小調優:避免新生代大小設置太小、避免新生代大小設置過大、避免Survivor設置太小或過大、合理設置新生代存活週期。 -Xmn 調整新生代大小,新生代越大一般也意味着更多對象會在minor GC階段被回收,但可能有可能形成舊生代大小,形成頻繁觸發Full GC,甚至是OutOfMemoryError。 -XX:SurvivorRatio調整Eden區與Survivor區的大小,Eden 區越大一般也意味着minor GC發生頻率越低,但可能有可能形成Survivor區過小,致使對象minor GC後就直接進入舊生代,從而更頻繁觸發Full GC。 GC策略的調優:CMS GC多數動做是和應用併發進行的,確實能夠減少GC動做給應用形成的暫停時間。對於Web應用很是須要一個對應用形成暫停時間短的GC,再加上Web應用 的瓶頸都不在CPU上,在G1還不夠成熟的狀況下,CMS GC是不錯的選擇。 (若是系統不是CPU密集型,且重新生代進入舊生代的大部分對象是能夠回收的,那麼採用CMS GC能夠更好地在舊生代滿以前完成對象的回收,更大程度下降Full GC發生的可能) 在調整了內存管理方面的參數後應經過-XX:PrintGCDetails、-XX:+PrintGCTimeStamps、 -XX:+PrintGCApplicationStoppedTime以及jstat或visualvm等方式觀察調整後的GC情況。 出內存管理之外的其餘方面的調優參數:-XX:CompileThreshold、-XX:+UseFastAccessorMethods、 -XX:+UseBaiasedLocking。 程序調優 CPU消耗嚴重的解決方法 CPU us高的解決方法: CPU us 高的緣由主要是執行線程不須要任何掛起動做,且一直執行,致使CPU 沒有機會去調度執行其餘的線程。 調優方案: 增長Thread.sleep,以釋放CPU 的執行權,下降CPU 的消耗。以損失單次執行性能爲代價的,但因爲其下降了CPU 的消耗,對於多線程的應用而言,反而提升了整體的平均性能。 (在實際的Java應用中相似場景, 對於這種場景最佳方式是改成採用wait/notify機制) 對於其餘相似循環次數過多、正則、計算等形成CPU us太高的情況, 則須要結合業務調優。 對於GC頻繁,則須要經過JVM調優或程序調優,下降GC的執行次數。 CPU sy高的解決方法: CPU sy 高的緣由主要是線程的運行狀態要常常切換,對於這種狀況,常見的一種優化方法是減小線程數。 調優方案: 將線程數下降 這種調優事後有可能會形成CPU us太高,因此合理設置線程數很是關鍵。 對於Java分佈式應用,還有一種典型現象是應用中有較多的網絡IO操做和確實須要一些鎖競爭機制(如數據庫鏈接池),但爲了可以支撐搞得併發量,可採用協程(Coroutine)來支撐更高的併發量,避免併發量上漲後形成CPU sy消耗嚴重、系統load迅速上漲和系統性能降低。 在Java中實現協程的框架有Kilim,Kilim執行一項任務建立Task,使用Task的暫停機制,而不是Thread,Kilim承擔了線程調度以及上下切換動做,Task相對於原生Thread而言就輕量級多了,且能更好利用CPU。Kilim帶來的是線程使用率的提高,但同時因爲要在JVM堆中保存Task上下文信息,所以在採用Kilim的狀況下要消耗更多的內存。(目前JDK 7中也有一個支持協程方式的實現,另外基於JVM的Scala的Actor也可用於在Java使用協程) 文件IO消耗嚴重的解決方法 從程序的角度而言,形成文件IO消耗嚴重的緣由主要是多個線程在寫進行大量的數據到同一文件,致使文件很快變得很大,從而寫入速度愈來愈慢,並形成各線程激烈爭搶文件鎖。 經常使用調優方法: 異步寫文件 批量讀寫 限流 限制文件大小 網絡IO消耗嚴重的解決方法 從程序的角度而言,形成網絡IO消耗嚴重的緣由主要是同時須要發送或接收的包太多。 經常使用調優方法: 限流,限流一般是限制發送packet的頻率,從而在網絡IO消耗可接受的狀況下來發送packget。 內存消耗嚴重的解決方法 釋放沒必要要的引用:代碼持有了不須要的對象引用,形成這些對象沒法被GC,從而佔據了JVM堆內存。(使用ThreadLocal:注意在線程內動做執行完畢時,需執行ThreadLocal.set把對象清除,避免持有沒必要要的對象引用) 使用對象緩存池:建立對象要消耗必定的CPU以及內存,使用對象緩存池必定程度上可下降JVM堆內存的使用。 採用合理的緩存失效算法:若是放入太多對象在緩存池中,反而會形成內存的嚴重消耗, 同時因爲緩存池一直對這些對象持有引用,從而形成Full GC增多,對於這種情況要合理控制緩存池的大小,避免緩存池的對象數量無限上漲。(經典的緩存失效算法來清除緩存池中的對象:FIFO、LRU、LFU等) 合理使用SoftReference和WeekReference:SoftReference的對象會在內存不夠用的時候回收,WeekReference的對象會在Full GC的時候回收。 資源消耗很少但程序執行慢的狀況的解決方法 下降鎖競爭: 多線多了,鎖競爭的情況會比較明顯,這時候線程很容易處於等待鎖的情況,從而致使性能降低以及CPU sy上升。 使用併發包中的類:大多數採用了lock-free、nonblocking算法。 使用Treiber算法:基於CAS以及AtomicReference。 使用Michael-Scott非阻塞隊列算法:基於CAS以及AtomicReference,典型ConcurrentLindkedQueue。 (基於CAS和AtomicReference來實現無阻塞是不錯的選擇,但值得注意的是,lock-free算法需不斷的循環比較來保證資源的一致性的,對於衝突較多的應用場景而言,會帶來更高的CPU消耗,所以不必定採用CAS實現無阻塞的就必定比採用lock方式的性能好。 還有一些無阻塞算法的改進:MCAS、WSTM等) 儘量少用鎖:儘量只對須要控制的資源作加鎖操做(一般沒有必要對整個方法加鎖,儘量讓鎖最小化,只對互斥及原子操做的地方加鎖,加鎖時儘量以保護資源的最小化粒度爲單位--如只對須要保護的資源加鎖而不是this)。 拆分鎖:獨佔鎖拆分爲多把鎖(讀寫鎖拆分、相似ConcurrentHashMap中默認拆分爲16把鎖),不少程度上能提升讀寫的性能,但須要注意在採用拆分鎖後,全局性質的操做會變得比較複雜(如ConcurrentHashMap中size操做)。(拆分鎖太多也會形成反作用,如CPU消耗明顯增長) 去除讀寫操做的互斥:在修改時加鎖,並複製對象進行修改,修改完畢後切換對象的引用,從而讀取時則不加鎖。這種稱爲CopyOnWrite,CopyOnWriteArrayList是典型實現,好處是能夠明顯提高讀的性能,適合讀多寫少的場景, 但因爲寫操做每次都要複製一份對象,會消耗更多的內存。 充分利用硬件資源(CPU和內存): 充分利用CPU 在能並行處理的場景中未使用足夠的線程(線程增長:CPU資源消耗可接受且不會帶來激烈競爭鎖的場景下), 例如單線程的計算,能夠拆分爲多個線程分別計算,最後將結果合併,JDK 7中的fork-join框架。 Amdahl定律公式:1/(F+(1-F)/N)。 充分利用內存 數據的緩存、耗時資源的緩存(數據庫鏈接建立、網絡鏈接的建立等)、頁面片斷的緩存。 畢竟內存的讀取確定遠快於硬盤、網絡的讀取, 在內存消耗可接受、GC頻率、以及系統結構(例如集羣環境可能會帶來緩存的同步)可接受狀況下,應充分利用內存來緩存數據,提高系統的性能。 總結: 好的調優策略是收益比(調優後提高的效果/調優改動所需付出的代價)最高的,一般來講簡單的系統調優比較好作,所以儘可能保持單機上應用的純粹性, 這是大型系統的基本架構原則。 調優的三大有效原則:充分而不過度使用硬件資源、合理調整JVM、合理使用JDK包。 學習參考資料: 《分佈式Java應用:基礎與實踐》 補充《分佈式Java應用:基礎與實踐》一些代碼樣例: cpu----------------------------------- CpuNotUseEffectiveDemo [java] view plaincopy /** * */ package tune.program.cpu; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 未充分利用CPU:在能並行處理的場景中未使用足夠的線程(線程增長:CPU資源消耗可接受且不會帶來激烈競爭鎖的場景下) * * @author yangwm Aug 25, 2010 9:54:50 AM */ public class CpuNotUseEffectiveDemo { private static int executeTimes = 10; private static int taskCount = 200; public static void main(String[] args) throws Exception { Task task = new Task(); for (int i = 0; i < taskCount; i++) { task.addTask(Integer.toString(i)); } long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); Thread thread = new Thread(task); thread.start(); thread.join(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) TaskCount Per Round( " + taskCount + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class Task implements Runnable { List tasks = new ArrayList(); Random random = new Random(); boolean exitFlag = false; public void addTask(String task) { List copyTasks = new ArrayList(tasks); copyTasks.add(task); tasks = copyTasks; } @Override public void run() { List runTasks = tasks; List removeTasks = new ArrayList(); for (String task : runTasks) { try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } removeTasks.add(task); } try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } } } } /* Round: 1 ...... Round: 10 Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 10687 ) ms */ CpuUseEffectiveDemo [java] view plaincopy /** * */ package tune.program.cpu; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.CountDownLatch; /** * 充分利用CPU:在能並行處理的場景中使用足夠的線程(線程增長:CPU資源消耗可接受且不會帶來激烈競爭鎖的場景下) * * @author yangwm Aug 25, 2010 9:54:50 AM */ public class CpuUseEffectiveDemo { private static int executeTimes = 10; private static int taskCount = 200; private static final int TASK_THREADCOUNT = 16; private static CountDownLatch latch; public static void main(String[] args) throws Exception { Task[] tasks = new Task[TASK_THREADCOUNT]; for (int i = 0; i < TASK_THREADCOUNT; i++) { tasks[i] = new Task(); } for (int i = 0; i < taskCount; i++) { int mod = i % TASK_THREADCOUNT; tasks[mod].addTask(Integer.toString(i)); } long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); latch = new CountDownLatch(TASK_THREADCOUNT); for (int j = 0; j < TASK_THREADCOUNT; j++) { Thread thread = new Thread(tasks[j]); thread.start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) TaskCount Per Round( " + taskCount + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class Task implements Runnable { List tasks = new ArrayList(); Random random = new Random(); boolean exitFlag = false; public void addTask(String task) { List copyTasks = new ArrayList(tasks); copyTasks.add(task); tasks = copyTasks; } @Override public void run() { List runTasks = tasks; List removeTasks = new ArrayList(); for (String task : runTasks) { try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } removeTasks.add(task); } try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } latch.countDown(); } } } /* Round: 1 ...... Round: 10 Execute summary: Round( 10 ) TaskCount Per Round( 200 ) Execute Time ( 938 ) ms */ fileio------------------------------------------------------------------- IOWaitHighDemo [java] view plaincopy /** * */ package tune.program.fileio; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Random; /** * 文件IO消耗嚴重的緣由主要是多個線程在寫進行大量的數據到同一文件, * 致使文件很快變得很大,從而寫入速度愈來愈慢,並形成各線程激烈爭搶文件鎖。 * * @author yangwm Aug 21, 2010 9:48:34 PM */ public class IOWaitHighDemo { private String fileName = "iowait.log"; private static int threadCount = Runtime.getRuntime().availableProcessors(); private Random random = new Random(); public static void main(String[] args) throws Exception { if (args.length == 1) { threadCount = Integer.parseInt(args[1]); } IOWaitHighDemo demo = new IOWaitHighDemo(); demo.runTest(); } private void runTest() throws Exception { File file = new File(fileName); file.createNewFile(); for (int i = 0; i < threadCount; i++) { new Thread(new Task()).start(); } } class Task implements Runnable { @Override public void run() { while (true) { try { StringBuilder strBuilder = new StringBuilder("====begin====/n"); String threadName = Thread.currentThread().getName(); for (int i = 0; i < 100000; i++) { strBuilder.append(threadName); strBuilder.append("/n"); } strBuilder.append("====end====/n"); BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)); writer.write(strBuilder.toString()); writer.close(); Thread.sleep(random.nextInt(10)); } catch (Exception e) { } } } } } /* C:/Documents and Settings/yangwm>jstack 2656 2010-08-21 23:24:17 Full thread dump Java HotSpot(TM) Client VM (17.0-b05 mixed mode): "DestroyJavaVM" prio=6 tid=0x00868c00 nid=0xde0 waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "Thread-1" prio=6 tid=0x0ab9dc00 nid=0xb7c runnable [0x0b0bf000] java.lang.Thread.State: RUNNABLE at java.io.FileOutputStream.close0(Native Method) at java.io.FileOutputStream.close(FileOutputStream.java:336) at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:320) at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149) - locked (a java.io.FileWriter) at java.io.OutputStreamWriter.close(OutputStreamWriter.java:233) at java.io.BufferedWriter.close(BufferedWriter.java:265) - locked (a java.io.FileWriter) at tune.IOWaitHighDemo$Task.run(IOWaitHighDemo.java:58) at java.lang.Thread.run(Thread.java:717) "Thread-0" prio=6 tid=0x0ab9d400 nid=0x80c runnable [0x0b06f000] java.lang.Thread.State: RUNNABLE at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:292) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) - locked (a java.io.FileWriter) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207) at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:128) - locked (a java.io.FileWriter) at java.io.BufferedWriter.write(BufferedWriter.java:229) - locked (a java.io.FileWriter) at java.io.Writer.write(Writer.java:157) at tune.IOWaitHighDemo$Task.run(IOWaitHighDemo.java:57) at java.lang.Thread.run(Thread.java:717) "Low Memory Detector" daemon prio=6 tid=0x0ab6f800 nid=0xfb0 runnable [0x00000000] java.lang.Thread.State: RUNNABLE "CompilerThread0" daemon prio=10 tid=0x0ab6c800 nid=0x5fc waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "Attach Listener" daemon prio=10 tid=0x0ab67800 nid=0x6fc waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" daemon prio=10 tid=0x0ab66800 nid=0x5a0 runnable [0x00000000] java.lang.Thread.State: RUNNABLE "Finalizer" daemon prio=8 tid=0x0ab54000 nid=0xe74 in Object.wait() [0x0ac8f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) - locked (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177) "Reference Handler" daemon prio=10 tid=0x0ab4f800 nid=0x8a4 in Object.wait() [0x0ac3f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) - locked (a java.lang.ref.Reference$Lock) "VM Thread" prio=10 tid=0x0ab4a800 nid=0x1d0 runnable "VM Periodic Task Thread" prio=10 tid=0x0ab7d400 nid=0x464 waiting on condition JNI global references: 693 C:/Documents and Settings/yangwm> */ LogControl [java] view plaincopy /** * */ package tune.program.fileio; import java.util.concurrent.atomic.AtomicInteger; /** * 日誌控制:採用簡單策略爲統計一段時間內日誌輸出頻率, 當超出這個頻率時,一段時間內再也不寫log * * @author yangwm Aug 24, 2010 10:41:43 AM */ public class LogControl { public static void main(String[] args) { for (int i = 1; i <= 1000; i++) { if (LogControl.isLog()) { //logger.error(errorInfo, throwable); System.out.println("errorInfo " + i); } // if (i % 100 == 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } private static final long INTERVAL = 1000; private static final long PUNISH_TIME = 5000; private static final int ERROR_THRESHOLD = 100; private static AtomicInteger count = new AtomicInteger(0); private static long beginTime; private static long punishTimeEnd; // 因爲控制不用很是精確, 所以忽略此處的併發問題 public static boolean isLog() { //System.out.println(count.get() + ", " + beginTime + ", " + punishTimeEnd + ", " + System.currentTimeMillis()); // 不寫日誌階段 if (punishTimeEnd > 0 && punishTimeEnd > System.currentTimeMillis()) { return false; } // 從新計數 if (count.getAndIncrement() == 0) { beginTime = System.currentTimeMillis(); return true; } else { // 已在計數 // 超過閥門值, 設置count爲0並設置一段時間內不寫日誌 if (count.get() > ERROR_THRESHOLD) { count.set(0); punishTimeEnd = PUNISH_TIME + System.currentTimeMillis(); return false; } // 沒超過閥門值, 且當前時間已超過計數週期,則從新計算 else if (System.currentTimeMillis() > (beginTime + INTERVAL)) { count.set(0); } return true; } } } /* errorInfo 1 errorInfo 2 ...... errorInfo 99 errorInfo 100 errorInfo 601 errorInfo 602 ...... errorInfo 699 errorInfo 700 */ memory------------------------------------------------------------------- MemoryHighDemo [java] view plaincopy /** * */ package tune.program.memory; import java.nio.ByteBuffer; /** * direct bytebuffer消耗的是jvm堆外的內存,但一樣是基於GC方式來釋放的。 * * @author yangwm Aug 21, 2010 9:40:18 PM */ public class MemoryHighDemo { public static void main(String[] args) throws Exception{ Thread.sleep(20000); System.out.println("read to create bytes,so jvm heap will be used"); byte[] bytes=new byte[128*1000*1000]; bytes[0]=1; bytes[1]=2; Thread.sleep(10000); System.out.println("read to allocate & put direct bytebuffer,no jvm heap should be used"); ByteBuffer buffer=ByteBuffer.allocateDirect(128*1024*1024); buffer.put(bytes); buffer.flip(); Thread.sleep(10000); System.out.println("ready to gc,jvm heap will be freed"); bytes=null; System.gc(); Thread.sleep(10000); System.out.println("read to get bytes,then jvm heap will be used"); byte[] resultbytes=new byte[128*1000*1000]; buffer.get(resultbytes); System.out.println("resultbytes[1] is: "+resultbytes[1]); Thread.sleep(10000); System.out.println("read to gc all"); buffer=null; resultbytes=null; System.gc(); Thread.sleep(10000); } } /* D:/study/tempProject/JavaLearn/classes>java -Xms140M -Xmx140M tune.MemoryHighDemo read to create bytes,so jvm heap will be used read to allocate & put direct bytebuffer,no jvm heap should be used ready to gc,jvm heap will be freed read to get bytes,then jvm heap will be used resultbytes[1] is: 2 read to gc all */ ObjectCachePool [java] view plaincopy /** * */ package tune.program.memory; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; /** * 採用合理的緩存失效算法: FIFO、LRU、LFU等 * * @author yangwm Aug 24, 2010 6:06:48 PM */ public class ObjectCachePool<K, V> { public static void main(String[] args) { // FIFO_POLICY int size = 10; int policy = 1; ObjectCachePool<Integer, Integer> objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy); for (int i = 1; i <= 15; i++) { objectCachePool.put(i, i); } for (int i = 15; i >= 1; i--) { objectCachePool.put(i, i); } System.out.println("size(" + size + "), policy(" + policy + ") FIFO "); for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } // LRU_POLICY size = 10; policy = 2; objectCachePool = new ObjectCachePool<Integer, Integer>(size, policy); for (int i = 1; i <= 15; i++) { objectCachePool.put(i, i); } for (int i = 15; i >= 1; i--) { objectCachePool.put(i, i); } System.out.println("size(" + size + "), policy(" + policy + ") LRU "); for (Map.Entry<Integer, Integer> entry : objectCachePool.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } } private static final int FIFO_POLICY = 1; private static final int LRU_POLICY = 2; private static final int DEFAULT_SIZE = 10; private Map<K, V> cacheObjects; public ObjectCachePool() { this(DEFAULT_SIZE); } public ObjectCachePool(int size) { this(size, FIFO_POLICY); } public ObjectCachePool(final int size, final int policy) { switch (policy) { case FIFO_POLICY: cacheObjects = new LinkedHashMap<K, V>(size) { /** * */ private static final long serialVersionUID = 1L; protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }; break; case LRU_POLICY: cacheObjects = new LinkedHashMap<K, V>(size, 0.75f, true) { /** * */ private static final long serialVersionUID = 1L; protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }; break; default: throw new IllegalArgumentException("Unknown policy: " + policy); } } public void put(K key, V value) { cacheObjects.put(key, value); } public void get(K key) { cacheObjects.get(key); } public void remove(K key) { cacheObjects.remove(key); } public void clear() { cacheObjects.clear(); } public Set<Map.Entry<K, V>> entrySet() { return cacheObjects.entrySet(); } } /* size(10), policy(1) FIFO 11, 11 12, 12 13, 13 14, 14 15, 15 5, 5 4, 4 3, 3 2, 2 1, 1 size(10), policy(2) LRU 10, 10 9, 9 8, 8 7, 7 6, 6 5, 5 4, 4 3, 3 2, 2 1, 1 */ ObjectPoolDemo [java] view plaincopy /** * */ package tune.program.memory; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; /** * 使用對象緩存池:建立對象要消耗必定的CPU以及內存,使用對象緩存池必定程度上可下降JVM堆內存的使用。 * * @author yangwm Aug 24, 2010 4:34:47 PM */ public class ObjectPoolDemo { private static int executeTimes = 10; private static int maxFactor = 10; private static int threadCount = 100; private static final int NOTUSE_OBJECTPOOL = 1; private static final int USE_OBJECTPOOL = 2; private static int runMode = NOTUSE_OBJECTPOOL; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { Task task = new Task(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) Thread Per Round( " + threadCount + " ) Object Factor ( " + maxFactor + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class Task implements Runnable { @Override public void run() { for (int j = 0; j < maxFactor; j++) { if (runMode == USE_OBJECTPOOL) { BigObjectPool.getInstance().getBigObject(j); } else { new BigObject(j); } } latch.countDown(); } } static class BigObjectPool { private static final BigObjectPool self = new BigObjectPool(); private final Map<Integer, BigObject> cacheObjects = new HashMap<Integer, BigObject>(); private BigObjectPool() { } public static BigObjectPool getInstance() { return self; } public BigObject getBigObject(int factor) { if (cacheObjects.containsKey(factor)) { return cacheObjects.get(factor); } else { BigObject object = new BigObject(factor); cacheObjects.put(factor, object); return object; } } } static class BigObject { private byte[] bytes = null; public BigObject(int factor) { bytes = new byte[(factor + 1) * 1024 * 1024]; } public byte[] getBytes() { return bytes; } } } /* -Xms128M -Xmx128M -Xmn64M , runMode is NOTUSE_OBJECTPOOL: Round: 1 ...... Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 50672 ) ms -Xms128M -Xmx128M -Xmn64M , runMode is USE_OBJECTPOOL: Round: 1 ...... Execute summary: Round( 10 ) Thread Per Round( 100 ) Object Factor ( 10 ) Execute Time ( 344 ) ms */ ThreadLocalDemo [java] view plaincopy /** * */ package tune.program.memory; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 釋放沒必要要的引用:代碼持有了不須要的對象引用,形成這些對象沒法被GC,從而佔據了JVM堆內存。 * (使用ThreadLocal:注意在線程內動做執行完畢時,需執行 ThreadLocal.set把對象清除,避免持有沒必要要的對象引用) * * @author yangwm Aug 24, 2010 11:29:59 AM */ public class ThreadLocalDemo { public static void main(String[] args) { ThreadLocalDemo demo = new ThreadLocalDemo(); demo.run(); } public void run() { ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(new Task()); System.gc(); } class Task implements Runnable { @Override public void run() { ThreadLocal<byte[]> localString = new ThreadLocal<byte[]>(); localString.set(new byte[1024 * 1024 * 30]); // 業務邏輯 //localString.set(null); // 釋放沒必要要的引用 } } } concurrent----------------------------------------------------------------------- LockHotDemo [java] view plaincopy /** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 鎖競爭的情況會比較明顯,這時候線程很容易處於等待鎖的情況,從而致使性能降低以及CPU sy上升 * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class LockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) Thread Per Round( " + threadCount + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private final Lock lock = new ReentrantLock(); private Handler() { } public static Handler getInstance() { return self; } public void handle(int id) { try { lock.lock(); // execute sth try { Thread.sleep(random.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } } } /* Round: 1 ...... Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 10625 ) ms */ ReduceLockHotDemo [java] view plaincopy /** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 儘量少用鎖:儘量只對須要控制的資源作加鎖操做 * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class ReduceLockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) Thread Per Round( " + threadCount + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private final Lock lock = new ReentrantLock(); private Handler() { } public static Handler getInstance() { return self; } public void handle(int id) { // execute sth don't need lock try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } try { lock.lock(); // execute sth try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } } } /* Round: 1 ...... Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 5547 ) ms */ SplitReduceLockHotDemo [java] view plaincopy /** * */ package tune.program.concurrent; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 儘量少用鎖:儘量只對須要控制的資源作加鎖操做 * 拆分鎖:獨佔鎖拆分爲多把鎖(讀寫鎖拆分、相似ConcurrentHashMap中默認拆分爲16把鎖) * * @author yangwm Aug 24, 2010 11:59:35 PM */ public class SplitReduceLockHotDemo { private static int executeTimes = 10; private static int threadCount = Runtime.getRuntime().availableProcessors() * 100; private static CountDownLatch latch = null; public static void main(String[] args) throws Exception { HandleTask task = new HandleTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < executeTimes; i++) { System.out.println("Round: " + (i + 1)); latch = new CountDownLatch(threadCount); for (int j = 0; j < threadCount; j++) { new Thread(task).start(); } latch.await(); } long endTime = System.currentTimeMillis(); System.out.println("Execute summary: Round( " + executeTimes + " ) Thread Per Round( " + threadCount + " ) Execute Time ( " + (endTime - beginTime) + " ) ms"); } static class HandleTask implements Runnable { private final Random random = new Random(); @Override public void run() { Handler.getInstance().handle(random.nextInt(10000)); latch.countDown(); } } static class Handler { private static final Handler self = new Handler(); private final Random random = new Random(); private int lockCount = 10; private Lock[] locks = new Lock[lockCount]; private Handler() { for (int i = 0; i < lockCount; i++) { locks[i] = new ReentrantLock(); } } public static Handler getInstance() { return self; } public void handle(int id) { // execute sth don't need lock try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } int mod = id % lockCount; try { locks[mod].lock(); // execute sth try { Thread.sleep(random.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } finally { locks[mod].unlock(); } } } } /* Round: 1 ...... Round: 10 Execute summary: Round( 10 ) Thread Per Round( 200 ) Execute Time ( 843 ) ms */ ConcurrentStack和StackBenchmark [java] view plaincopy /** * */ package tune.program.concurrent; import java.util.concurrent.atomic.AtomicReference; /** * 使用Treiber算法實現Stack:基於CAS以及AtomicReference。 * * @author yangwm Aug 25, 2010 10:50:17 AM */ public class ConcurrentStack { AtomicReference<Node> head = new AtomicReference<Node>(); public void push(E item) { Node newHead = new Node(item); Node oldHead; do { oldHead = head.get(); newHead.next = oldHead; } while (!head.compareAndSet(oldHead, newHead)); } public E pop() { Node oldHead; Node newHead; do { oldHead = head.get(); if (oldHead == null) { return null; } newHead = oldHead.next; } while (!head.compareAndSet(oldHead, newHead)); return oldHead.item; } static class Node { final E item; Node next; public Node(E item) { this.item = item; } } } /** * */ package tune.program.concurrent; import java.util.Stack; import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; /** * 基準測試:Treiber算法實現Stack、同步實現的Stack * * @author yangwm Aug 25, 2010 11:36:14 AM */ public class StackBenchmark { public static void main(String[] args) throws Exception { StackBenchmark stackBenchmark = new StackBenchmark(); stackBenchmark.run(); } private Stack stack = new Stack(); private ConcurrentStack concurrentStack = new ConcurrentStack(); private static final int THREAD_COUNT = 300; private CountDownLatch latch = new CountDownLatch(THREAD_COUNT); private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT); public void run() throws Exception { StackTask stackTask = new StackTask(); long beginTime = System.currentTimeMillis(); for (int i = 0; i < THREAD_COUNT; i++) { new Thread(stackTask).start(); } latch.await(); long endTime = System.currentTimeMillis(); System.out.println("Stack consume Time: " + (endTime - beginTime) + " ms"); latch = new CountDownLatch(THREAD_COUNT); barrier = new CyclicBarrier(THREAD_COUNT); ConcurrentStackTask concurrentStackTask = new ConcurrentStackTask(); beginTime = System.currentTimeMillis(); for (int i = 0; i < THREAD_COUNT; i++) { new Thread(concurrentStackTask).start(); } latch.await(); endTime = System.currentTimeMillis(); System.out.println("ConcurrentStack consume Time: " + (endTime - beginTime) + " ms"); } class StackTask implements Runnable { @Override public void run() { try { barrier.await(); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 10; i++) { stack.push(Thread.currentThread().getName()); stack.pop(); } latch.countDown(); } } class ConcurrentStackTask implements Runnable { @Override public void run() { try { barrier.await(); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 10; i++) { concurrentStack.push(Thread.currentThread().getName()); concurrentStack.pop(); } latch.countDown(); } } } /* Stack consume Time: 94 ms ConcurrentStack consume Time: 63 ms Stack consume Time: 78 ms ConcurrentStack consume Time: 62 ms */java