亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術(shù)文章
文章詳情頁

淺談java是如何做資源回收補(bǔ)救的

【字号: 作者:豬豬瀏覽:5日期:2022-08-30 10:28:55

學(xué)習(xí)java的過程,我們經(jīng)常談?wù)撘粋€(gè)對(duì)象的回收,尤其是資源類型,如果沒有顯示的關(guān)閉,對(duì)象就被回收了,說明出現(xiàn)了資源泄漏。java本身為了防止這種情況,做了一些擔(dān)保的方式,確保可以讓未關(guān)閉的資源合理回收掉。

finalize回收

finalize方式是java對(duì)象被回收時(shí)觸發(fā)的一個(gè)方法。java的很多資源對(duì)象,都是在finalize中寫了擔(dān)保的方法。

/** * Ensures that the <code>close</code> method of this file input stream is * called when there are no more references to it. * * @exception IOException if an I/O error occurs. * @see java.io.FileInputStream#close() */ protected void finalize() throws IOException { if ((fd != null) && (fd != FileDescriptor.in)) { /* if fd is shared, the references in FileDescriptor * will ensure that finalizer is only called when * safe to do so. All references using the fd have * become unreachable. We can call close() */ close(); } }

上面是FileInputStream的finalize方法,在方法被調(diào)用時(shí),會(huì)檢測(cè)文件描述符是否存在,如果存在的話就調(diào)用close方法。來確保資源的回收。

finalize方法在我們學(xué)習(xí)java的時(shí)候都并不推薦進(jìn)行重寫,也不推薦寫復(fù)雜的邏輯在里面,主要是因?yàn)間c的時(shí)候,都會(huì)調(diào)用這個(gè)方法,如果執(zhí)行的內(nèi)容太多,就會(huì)導(dǎo)致gc被拖長(zhǎng)。影響程序的正常運(yùn)行。而且這里也只是做一個(gè)簡(jiǎn)單的擔(dān)保。大部分希望的還是編寫代碼的人可以調(diào)用close。這樣在做判斷的時(shí)候就結(jié)束了,而不用真正的調(diào)用關(guān)閉的代碼。

Cleaner回收

在DirectByteBuffer中,使用了一個(gè)Cleaner對(duì)象進(jìn)行補(bǔ)救的。

unsafe.setMemory(base, size, (byte) 0); if (pa && (base % ps != 0)) { // Round up to page boundary address = base + ps - (base & (ps - 1)); } else { address = base; } cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); att = null;

申請(qǐng)完資源后,會(huì)創(chuàng)建一個(gè)Deallocator對(duì)象。

private static class Deallocator implements Runnable { private static Unsafe unsafe = Unsafe.getUnsafe(); private long address; private long size; private int capacity; private Deallocator(long address, long size, int capacity) { assert (address != 0); this.address = address; this.size = size; this.capacity = capacity; } public void run() { if (address == 0) {// Paranoiareturn; } unsafe.freeMemory(address); address = 0; Bits.unreserveMemory(size, capacity); } }

Deallocator的run方法中就進(jìn)行了資源的釋放。執(zhí)行的時(shí)機(jī)就是靠 Cleaner來觸發(fā)的。

Cleaner是PhantomReference的子類,PhantomReference是Reference的子類。

在中有一個(gè)ReferenceHandler

private static class ReferenceHandler extends Thread {

他的run方法就是調(diào)用cleaner里的clean方法。這個(gè)線程是在靜態(tài)塊里啟動(dòng)起來的。

Thread handler = new ReferenceHandler(tg, 'Reference Handler'); /* If there were a special system-only priority greater than * MAX_PRIORITY, it would be used here */ handler.setPriority(Thread.MAX_PRIORITY); handler.setDaemon(true); handler.start(); SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() { @Override public boolean tryHandlePendingReference() {return tryHandlePending(false); } });

于此同時(shí),并且給SharedSecrets設(shè)置了一個(gè)JavaLangRefAccess。

調(diào)用clean方法的過程在tryHandlePending里,這里的參數(shù)很重要。

static boolean tryHandlePending(boolean waitForNotify) { Reference<Object> r; Cleaner c; try { synchronized (lock) {if (pending != null) { r = pending; // ’instanceof’ might throw OutOfMemoryError sometimes // so do this before un-linking ’r’ from the ’pending’ chain... c = r instanceof Cleaner ? (Cleaner) r : null; // unlink ’r’ from ’pending’ chain pending = r.discovered; r.discovered = null;} else { // The waiting on the lock may cause an OutOfMemoryError // because it may try to allocate exception objects. if (waitForNotify) { lock.wait(); } // retry if waited return waitForNotify;} } } catch (OutOfMemoryError x) { // Give other threads CPU time so they hopefully drop some live references // and GC reclaims some space. // Also prevent CPU intensive spinning in case ’r instanceof Cleaner’ above // persistently throws OOME for some time... Thread.yield(); // retry return true; } catch (InterruptedException x) { // retry return true; }

waitForNotify是true的時(shí)候,在沒有回收對(duì)象的時(shí)候,會(huì)進(jìn)入阻塞,然后等ooe。外層是個(gè)死循環(huán),就會(huì)被再次調(diào)用到,下次進(jìn)來的時(shí)候就可以出發(fā)clean了。

ReferenceHandler是管理機(jī)制的一種。

還有一種就是SharedSecrets調(diào)用tryHandlePending(false)。

在另外一個(gè)類,bits里

final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess(); // retry while helping enqueue pending Reference objects // which includes executing pending Cleaner(s) which includes // Cleaner(s) that free direct buffer memory while (jlra.tryHandlePendingReference()) { if (tryReserveMemory(size, cap)) {return; } }

在做reserveMemory的時(shí)候,會(huì)從SharedSecrets來調(diào)用tryHandlePending(false)。這里又變相的進(jìn)行了一次回收。

小結(jié)

java回收利用兩種機(jī)制。一種是finalize,一種是Cleaner。其中Cleaner一部分依賴oome觸發(fā)一次回收,一部分利用reserveMemory中做一次回收。

到此這篇關(guān)于淺談java是如何做資源回收補(bǔ)救的的文章就介紹到這了,更多相關(guān)java 資源回收補(bǔ)救內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 免费看黄在线观看 | 一区二区三区四区视频在线观看 | 国产成人综合亚洲 | 亚洲色图综合区 | 野外三级国产在线观看 | 国产午夜久久影院 | 国产成人综合网亚洲欧美在线 | 国产1024一区二区你懂的 | 国产一区二区三区丶四区 | 555夜色555亚洲夜色 | 国偷盗摄自产福利一区在线 | 免费观看影院 | 男女做a一级视频免费观看 男女喷水视频 | 青青草国产精品欧美成人 | 精品福利视频在线观看 | 免费一级黄色录像影片 | 黄色免费一级播放片 | 亚洲国产成人久久三区 | 麻豆视频在线播放 | 开心午夜婷婷色婷在线 | 91精品国产亚洲爽啪在线观看 | 在线播放黄 | 草草线禁成18年在线视频 | 国产精品视频免费播放 | 欧美色爱综合网 | 亚洲国产欧美在线 | 日本xxx片免费高清在线 | 永久免费观看黄网站 | 久久综合精品国产一区二区三区无 | 日韩 欧美 国产 亚洲 中文 | 一区二区三区福利视频 | 国产青榴社区91精品 | 好硬好湿好爽再深一点h视频 | julia一区福利视频在线观看 | 日韩一区二区三区在线视频 | 国产伦精品一区二区三区女 | 九九热在线免费视频 | 国产毛片一级aaaaa片 | 三级国产精品一区二区 | 天干天干夜天干天天爽 | 伊人影院99|