Java性能調(diào)優(yōu)筆記 調(diào)優(yōu)步驟:衡量系統(tǒng)現(xiàn)狀、設(shè)定調(diào)優(yōu)目標(biāo)、尋找性能瓶頸、性能調(diào)優(yōu)、衡量是否到達(dá)目標(biāo)(如果未到達(dá)目標(biāo),需重新尋找性能瓶頸)、性能調(diào)優(yōu)結(jié)束。 尋找性能瓶頸 性能瓶頸的表象:資源消耗過(guò)多、外部處理系統(tǒng)的性能不足、資源消耗不多但程序的響應(yīng)速度卻仍達(dá)不到要求。 資源消耗:CPU、文件IO、網(wǎng)絡(luò)IO、內(nèi)存。 外部處理系統(tǒng)的性能不足:所調(diào)用的其他系統(tǒng)提供的功能或數(shù)據(jù)庫(kù)操作的響應(yīng)速度不夠。 資源消耗不多但程序的響應(yīng)速度卻仍達(dá)不到要求:程序代碼運(yùn)行效率不夠高、未充分使用資源、程序結(jié)構(gòu)不合理。 CPU消耗分析 CPU主要用于中斷、內(nèi)核、用戶進(jìn)程的任務(wù)處理,優(yōu)先級(jí)為中斷>內(nèi)核>用戶進(jìn)程。 上下文切換: 每個(gè)線程分配一定的執(zhí)行時(shí)間,當(dāng)?shù)竭_(dá)執(zhí)行時(shí)間、線程中有IO阻塞或高優(yōu)先級(jí)線程要執(zhí)行時(shí),將切換執(zhí)行的線程。在切換時(shí)要存儲(chǔ)目前線程的執(zhí)行狀態(tài),并恢復(fù)要執(zhí)行的線程的狀態(tài)。 對(duì)于Java應(yīng)用,典型的是在進(jìn)行文件IO操作、網(wǎng)絡(luò)IO操作、鎖等待、線程Sleep時(shí),當(dāng)前線程會(huì)進(jìn)入阻塞或休眠狀態(tài),從而觸發(fā)上下文切換,上下文切換過(guò)多會(huì)造成內(nèi)核占據(jù)較多的CPU的使用。 運(yùn)行隊(duì)列: 每個(gè)CPU核都維護(hù)一個(gè)可運(yùn)行的線程隊(duì)列。系統(tǒng)的load主要由CPU的運(yùn)行隊(duì)列來(lái)決定。 運(yùn)行隊(duì)列值越大,就意味著線程會(huì)要消耗越長(zhǎng)的時(shí)間才能執(zhí)行完成。 利用率: CPU在用戶進(jìn)程、內(nèi)核、中斷處理、IO等待、空閑,這五個(gè)部分使用百分比。 文件IO消耗分析 linux在操作文件時(shí),將數(shù)據(jù)放入文件緩存區(qū),直到內(nèi)存不夠或系統(tǒng)要釋放內(nèi)存給用戶進(jìn)程使用。所以通常情況下只有寫(xiě)文件和第一次讀取文件時(shí)會(huì)產(chǎn)生真正的文件IO。 對(duì)于Java應(yīng)用,造成文件IO消耗高主要是多個(gè)線程需要進(jìn)行大量?jī)?nèi)容寫(xiě)入(例如頻繁的日志寫(xiě)入)的動(dòng)作、磁盤(pán)設(shè)備本身的處理速度慢、文件系統(tǒng)慢、操作的文件本身已經(jīng)很大。 網(wǎng)絡(luò)IO消耗分析 對(duì)于分布式Java應(yīng)用,網(wǎng)卡中斷是不是均衡分配到各CPU(cat/PRoc/interrupts查看)。 內(nèi)存消耗分析(-Xms和-Xmx設(shè)為相同的值,避免運(yùn)行期JVM堆內(nèi)存要不斷申請(qǐng)內(nèi)存) 對(duì)于Java應(yīng)用,內(nèi)存的消耗主要在Java堆內(nèi)存上,只有創(chuàng)建線程和使用Direct ByteBuffer才會(huì)操作JVM堆外的內(nèi)存。 JVM內(nèi)存消耗過(guò)多會(huì)導(dǎo)致GC執(zhí)行頻繁,CPU消耗增加,應(yīng)用線程的執(zhí)行速度嚴(yán)重下降,甚至造成OutOfMemoryError,最終導(dǎo)致Java進(jìn)程退出。 JVM堆外的內(nèi)存 swap的消耗、物理內(nèi)存的消耗、JVM內(nèi)存的消耗。 程序執(zhí)行慢原因分析 鎖競(jìng)爭(zhēng)激烈:很多線程競(jìng)爭(zhēng)互斥資源,但資源有限, 造成其他線程都處于等待狀態(tài)。 未充分使用硬件資源:線程操作被串行化。 數(shù)據(jù)量增長(zhǎng):?jiǎn)伪頂?shù)據(jù)量太大(如1個(gè)億)造成數(shù)據(jù)庫(kù)讀寫(xiě)速度大幅下降(操作此表)。 調(diào)優(yōu) JVM調(diào)優(yōu)(最關(guān)鍵參數(shù)為:-Xms -Xmx -Xmn -XX:SurvivorRatio -XX:MaxTenuringThreshold) 代大小調(diào)優(yōu):避免新生代大小設(shè)置過(guò)小、避免新生代大小設(shè)置過(guò)大、避免Survivor設(shè)置過(guò)小或過(guò)大、合理設(shè)置新生代存活周期。 -Xmn 調(diào)整新生代大小,新生代越大通常也意味著更多對(duì)象會(huì)在minor GC階段被回收,但可能有可能造成舊生代大小,造成頻繁觸發(fā)Full GC,甚至是OutOfMemoryError。 -XX:SurvivorRatio調(diào)整Eden區(qū)與Survivor區(qū)的大小,Eden 區(qū)越大通常也意味著minor GC發(fā)生頻率越低,但可能有可能造成Survivor區(qū)太小,導(dǎo)致對(duì)象minor GC后就直接進(jìn)入舊生代,從而更頻繁觸發(fā)Full GC。 GC策略的調(diào)優(yōu):CMS GC多數(shù)動(dòng)作是和應(yīng)用并發(fā)進(jìn)行的,確實(shí)可以減小GC動(dòng)作給應(yīng)用造成的暫停時(shí)間。對(duì)于Web應(yīng)用非常需要一個(gè)對(duì)應(yīng)用造成暫停時(shí)間短的GC,再加上Web應(yīng)用 的瓶頸都不在CPU上,在G1還不夠成熟的情況下,CMS GC是不錯(cuò)的選擇。 (如果系統(tǒng)不是CPU密集型,且從新生代進(jìn)入舊生代的大部分對(duì)象是可以回收的,那么采用CMS GC可以更好地在舊生代滿之前完成對(duì)象的回收,更大程度降低Full GC發(fā)生的可能) 在調(diào)整了內(nèi)存管理方面的參數(shù)后應(yīng)通過(guò)-XX:PrintGCDetails、-XX:+PrintGCTimeStamps、 -XX:+PrintGCapplicationStoppedTime以及jstat或visualvm等方式觀察調(diào)整后的GC狀況。 出內(nèi)存管理以外的其他方面的調(diào)優(yōu)參數(shù):-XX:CompileThreshold、-XX:+UseFastaccessorMethods、 -XX:+UseBaiasedLocking。 程序調(diào)優(yōu) CPU消耗嚴(yán)重的解決方法 CPU us高的解決方法: CPU us 高的原因主要是執(zhí)行線程不需要任何掛起動(dòng)作,且一直執(zhí)行,導(dǎo)致CPU 沒(méi)有機(jī)會(huì)去調(diào)度執(zhí)行其他的線程。 調(diào)優(yōu)方案: 增加Thread.sleep,以釋放CPU 的執(zhí)行權(quán),降低CPU 的消耗。以損失單次執(zhí)行性能為代價(jià)的,但由于其降低了CPU 的消耗,對(duì)于多線程的應(yīng)用而言,反而提高了總體的平均性能。 (在實(shí)際的Java應(yīng)用中類似場(chǎng)景, 對(duì)于這種場(chǎng)景最佳方式是改為采用wait/notify機(jī)制) 對(duì)于其他類似循環(huán)次數(shù)過(guò)多、正則、計(jì)算等造成CPU us過(guò)高的狀況, 則需要結(jié)合業(yè)務(wù)調(diào)優(yōu)。 對(duì)于GC頻繁,則需要通過(guò)JVM調(diào)優(yōu)或程序調(diào)優(yōu),降低GC的執(zhí)行次數(shù)。 CPU sy高的解決方法: CPU sy 高的原因主要是線程的運(yùn)行狀態(tài)要經(jīng)常切換,對(duì)于這種情況,常見(jiàn)的一種優(yōu)化方法是減少線程數(shù)。 調(diào)優(yōu)方案: 將線程數(shù)降低 這種調(diào)優(yōu)過(guò)后有可能會(huì)造成CPU us過(guò)高,所以合理設(shè)置線程數(shù)非常關(guān)鍵。 對(duì)于Java分布式應(yīng)用,還有一種典型現(xiàn)象是應(yīng)用中有較多的網(wǎng)絡(luò)IO操作和確實(shí)需要一些鎖競(jìng)爭(zhēng)機(jī)制(如數(shù)據(jù)庫(kù)連接池),但為了能夠支撐搞得并發(fā)量,可采用協(xié)程(Coroutine)來(lái)支撐更高的并發(fā)量,避免并發(fā)量上漲后造成CPU sy消耗嚴(yán)重、系統(tǒng)load迅速上漲和系統(tǒng)性能下降。 在Java中實(shí)現(xiàn)協(xié)程的框架有Kilim,Kilim執(zhí)行一項(xiàng)任務(wù)創(chuàng)建Task,使用Task的暫停機(jī)制,而不是Thread,Kilim承擔(dān)了線程調(diào)度以及上下切換動(dòng)作,Task相對(duì)于原生Thread而言就輕量級(jí)多了,且能更好利用CPU。Kilim帶來(lái)的是線程使用率的提升,但同時(shí)由于要在JVM堆中保存Task上下文信息,因此在采用Kilim的情況下要消耗更多的內(nèi)存。(目前JDK 7中也有一個(gè)支持協(xié)程方式的實(shí)現(xiàn),另外基于JVM的Scala的Actor也可用于在Java使用協(xié)程) 文件IO消耗嚴(yán)重的解決方法 從程序的角度而言,造成文件IO消耗嚴(yán)重的原因主要是多個(gè)線程在寫(xiě)進(jìn)行大量的數(shù)據(jù)到同一文件,導(dǎo)致文件很快變得很大,從而寫(xiě)入速度越來(lái)越慢,并造成各線程激烈爭(zhēng)搶文件鎖。 常用調(diào)優(yōu)方法: 異步寫(xiě)文件 批量讀寫(xiě) 限流 限制文件大小 網(wǎng)絡(luò)IO消耗嚴(yán)重的解決方法 從程序的角度而言,造成網(wǎng)絡(luò)IO消耗嚴(yán)重的原因主要是同時(shí)需要發(fā)送或接收的包太多。 常用調(diào)優(yōu)方法: 限流,限流通常是限制發(fā)送packet的頻率,從而在網(wǎng)絡(luò)IO消耗可接受的情況下來(lái)發(fā)送packget。 內(nèi)存消耗嚴(yán)重的解決方法 釋放不必要的引用:代碼持有了不需要的對(duì)象引用,造成這些對(duì)象無(wú)法被GC,從而占據(jù)了JVM堆內(nèi)存。(使用ThreadLocal:注意在線程內(nèi)動(dòng)作執(zhí)行完畢時(shí),需執(zhí)行ThreadLocal.set把對(duì)象清除,避免持有不必要的對(duì)象引用) 使用對(duì)象緩存池:創(chuàng)建對(duì)象要消耗一定的CPU以及內(nèi)存,使用對(duì)象緩存池一定程度上可降低JVM堆內(nèi)存的使用。 采用合理的緩存失效算法:如果放入太多對(duì)象在緩存池中,反而會(huì)造成內(nèi)存的嚴(yán)重消耗, 同時(shí)由于緩存池一直對(duì)這些對(duì)象持有引用,從而造成Full GC增多,對(duì)于這種狀況要合理控制緩存池的大小,避免緩存池的對(duì)象數(shù)量無(wú)限上漲。(經(jīng)典的緩存失效算法來(lái)清除緩存池中的對(duì)象:FIFO、LRU、LFU等) 合理使用SoftReference和WeekReference:SoftReference的對(duì)象會(huì)在內(nèi)存不夠用的時(shí)候回收,WeekReference的對(duì)象會(huì)在Full GC的時(shí)候回收。 資源消耗不多但程序執(zhí)行慢的情況的解決方法 降低鎖競(jìng)爭(zhēng): 多線多了,鎖競(jìng)爭(zhēng)的狀況會(huì)比較明顯,這時(shí)候線程很容易處于等待鎖的狀況,從而導(dǎo)致性能下降以及CPU sy上升。 使用并發(fā)包中的類:大多數(shù)采用了lock-free、nonblocking算法。 使用Treiber算法:基于CAS以及AtomicReference。 使用Michael-Scott非阻塞隊(duì)列算法:基于CAS以及AtomicReference,典型ConcurrentLindkedQueue。 (基于CAS和AtomicReference來(lái)實(shí)現(xiàn)無(wú)阻塞是不錯(cuò)的選擇,但值得注意的是,lock-free算法需不斷的循環(huán)比較來(lái)保證資源的一致性的,對(duì)于沖突較多的應(yīng)用場(chǎng)景而言,會(huì)帶來(lái)更高的CPU消耗,因此不一定采用CAS實(shí)現(xiàn)無(wú)阻塞的就一定比采用lock方式的性能好。 還有一些無(wú)阻塞算法的改進(jìn):MCAS、WSTM等) 盡可能少用鎖:盡可能只對(duì)需要控制的資源做加鎖操作(通常沒(méi)有必要對(duì)整個(gè)方法加鎖,盡可能讓鎖最小化,只對(duì)互斥及原子操作的地方加鎖,加鎖時(shí)盡可能以保護(hù)資源的最小化粒度為單位--如只對(duì)需要保護(hù)的資源加鎖而不是this)。 拆分鎖:獨(dú)占鎖拆分為多把鎖(讀寫(xiě)鎖拆分、類似ConcurrentHashMap中默認(rèn)拆分為16把鎖),很多程度上能提高讀寫(xiě)的性能,但需要注意在采用拆分鎖后,全局性質(zhì)的操作會(huì)變得比較復(fù)雜(如ConcurrentHashMap中size操作)。(拆分鎖太多也會(huì)造成副作用,如CPU消耗明顯增加) 去除讀寫(xiě)操作的互斥:在修改時(shí)加鎖,并復(fù)制對(duì)象進(jìn)行修改,修改完畢后切換對(duì)象的引用,從而讀取時(shí)則不加鎖。這種稱為CopyOnWrite,CopyOnWriteArrayList是典型實(shí)現(xiàn),好處是可以明顯提升讀的性能,適合讀多寫(xiě)少的場(chǎng)景, 但由于寫(xiě)操作每次都要復(fù)制一份對(duì)象,會(huì)消耗更多的內(nèi)存。 充分利用硬件資源(CPU和內(nèi)存): 充分利用CPU 在能并行處理的場(chǎng)景中未使用足夠的線程(線程增加:CPU資源消耗可接受且不會(huì)帶來(lái)激烈競(jìng)爭(zhēng)鎖的場(chǎng)景下), 例如單線程的計(jì)算,可以拆分為多個(gè)線程分別計(jì)算,最后將結(jié)果合并,JDK 7中的fork-join框架。 Amdahl定律公式:1/(F+(1-F)/N)。 充分利用內(nèi)存 數(shù)據(jù)的緩存、耗時(shí)資源的緩存(數(shù)據(jù)庫(kù)連接創(chuàng)建、網(wǎng)絡(luò)連接的創(chuàng)建等)、頁(yè)面片段的緩存。 畢竟內(nèi)存的讀取肯定遠(yuǎn)快于硬盤(pán)、網(wǎng)絡(luò)的讀取, 在內(nèi)存消耗可接受、GC頻率、以及系統(tǒng)結(jié)構(gòu)(例如集群環(huán)境可能會(huì)帶來(lái)緩存的同步)可接受情況下,應(yīng)充分利用內(nèi)存來(lái)緩存數(shù)據(jù),提升系統(tǒng)的性能。 總結(jié): 好的調(diào)優(yōu)策略是收益比(調(diào)優(yōu)后提升的效果/調(diào)優(yōu)改動(dòng)所需付出的代價(jià))最高的,通常來(lái)說(shuō)簡(jiǎn)單的系統(tǒng)調(diào)優(yōu)比較好做,因此盡量保持單機(jī)上應(yīng)用的純粹性, 這是大型系統(tǒng)的基本架構(gòu)原則。 調(diào)優(yōu)的三大有效原則:充分而不過(guò)分使用硬件資源、合理調(diào)整JVM、合理使用JDK包。 學(xué)習(xí)參考資料: 《分布式Java應(yīng)用:基礎(chǔ)與實(shí)踐》 補(bǔ)充《分布式Java應(yīng)用:基礎(chǔ)與實(shí)踐》一些代碼樣例: cpu----------------------------------- CpuNotUseEffectiveDemo [java] view plaincopy /** * */ package tune.program.cpu; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 未充分利用CPU:在能并行處理的場(chǎng)景中未使用足夠的線程(線程增加:CPU資源消耗可接受且不會(huì)帶來(lái)激烈競(jìng)爭(zhēng)鎖的場(chǎng)景下) * * @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:在能并行處理的場(chǎng)景中使用足夠的線程(線程增加:CPU資源消耗可接受且不會(huì)帶來(lái)激烈競(jìng)爭(zhēng)鎖的場(chǎng)景下) * * @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消耗嚴(yán)重的原因主要是多個(gè)線程在寫(xiě)進(jìn)行大量的數(shù)據(jù)到同一文件, * 導(dǎo)致文件很快變得很大,從而寫(xiě)入速度越來(lái)越慢,并造成各線程激烈爭(zhēng)搶文件鎖。 * * @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; /** * 日志控制:采用簡(jiǎn)單策略為統(tǒng)計(jì)一段時(shí)間內(nèi)日志輸出頻率, 當(dāng)超出這個(gè)頻率時(shí),一段時(shí)間內(nèi)不再寫(xiě)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; // 由于控制不用非常精確, 因此忽略此處的并發(fā)問(wèn)題 public static boolean isLog() { //System.out.println(count.get() + ", " + beginTime + ", " + punishTimeEnd + ", " + System.currentTimeMillis()); // 不寫(xiě)日志階段 if (punishTimeEnd > 0 && punishTimeEnd > System.currentTimeMillis()) { return false; } // 重新計(jì)數(shù) if (count.getAndIncrement() == 0) { beginTime = System.currentTimeMillis(); return true; } else { // 已在計(jì)數(shù) // 超過(guò)閥門(mén)值, 設(shè)置count為0并設(shè)置一段時(shí)間內(nèi)不寫(xiě)日志 if (count.get() > ERROR_THRESHOLD) { count.set(0); punishTimeEnd = PUNISH_TIME + System.currentTimeMillis(); return false; } // 沒(méi)超過(guò)閥門(mén)值, 且當(dāng)前時(shí)間已超過(guò)計(jì)數(shù)周期,則重新計(jì)算 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堆外的內(nèi)存,但同樣是基于GC方式來(lái)釋放的。 * * @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; /** * 使用對(duì)象緩存池:創(chuàng)建對(duì)象要消耗一定的CPU以及內(nèi)存,使用對(duì)象緩存池一定程度上可降低JVM堆內(nèi)存的使用。 * * @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; /** * 釋放不必要的引用:代碼持有了不需要的對(duì)象引用,造成這些對(duì)象無(wú)法被GC,從而占據(jù)了JVM堆內(nèi)存。 * (使用ThreadLocal:注意在線程內(nèi)動(dòng)作執(zhí)行完畢時(shí),需執(zhí)行 ThreadLocal.set把對(duì)象清除,避免持有不必要的對(duì)象引用) * * @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]); // 業(yè)務(wù)邏輯 //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; /** * 鎖競(jìng)爭(zhēng)的狀況會(huì)比較明顯,這時(shí)候線程很容易處于等待鎖的狀況,從而導(dǎo)致性能下降以及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; /** * 盡可能少用鎖:盡可能只對(duì)需要控制的資源做加鎖操作 * * @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; /** * 盡可能少用鎖:盡可能只對(duì)需要控制的資源做加鎖操作 * 拆分鎖:獨(dú)占鎖拆分為多把鎖(讀寫(xiě)鎖拆分、類似ConcurrentHashMap中默認(rèn)拆分為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算法實(shí)現(xiàn)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; /** * 基準(zhǔn)測(cè)試:Treiber算法實(shí)現(xiàn)Stack、同步實(shí)現(xiàn)的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 */
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注