本篇內(nèi)容主要講解“java是怎么做資源回收補(bǔ)救的”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java是怎么做資源回收補(bǔ)救的”吧!
站在用戶的角度思考問題,與客戶深入溝通,找到曲周網(wǎng)站設(shè)計與曲周網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋曲周地區(qū)。
finalize方式是java對象被回收時觸發(fā)的一個方法。java的很多資源對象,都是在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)用時,會檢測文件描述符是否存在,如果存在的話就調(diào)用close方法。來確保資源的回收。
finalize方法在我們學(xué)習(xí)java的時候都并不推薦進(jìn)行重寫,也不推薦寫復(fù)雜的邏輯在里面,主要是因為gc的時候,都會調(diào)用這個方法,如果執(zhí)行的內(nèi)容太多,就會導(dǎo)致gc被拖長。影響程序的正常運(yùn)行。而且這里也只是做一個簡單的擔(dān)保。大部分希望的還是編寫代碼的人可以調(diào)用close。這樣在做判斷的時候就結(jié)束了,而不用真正的調(diào)用關(guān)閉的代碼。
在DirectByteBuffer中,使用了一個Cleaner對象進(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;
申請完資源后,會創(chuàng)建一個Deallocator對象。
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) {
// Paranoia
return;
}
unsafe.freeMemory(address);
address = 0;
Bits.unreserveMemory(size, capacity);
}
}
Deallocator的run方法中就進(jìn)行了資源的釋放。執(zhí)行的時機(jī)就是靠 Cleaner來觸發(fā)的。Cleaner是PhantomReference的子類,PhantomReference是Reference的子類。在中有一個ReferenceHandler
private static class ReferenceHandler extends Thread {
他的run方法就是調(diào)用cleaner里的clean方法。這個線程是在靜態(tài)塊里啟動起來的。
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);
}
});
于此同時,并且給SharedSecrets設(shè)置了一個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的時候,在沒有回收對象的時候,會進(jìn)入阻塞,然后等ooe。外層是個死循環(huán),就會被再次調(diào)用到,下次進(jìn)來的時候就可以出發(fā)clean了。ReferenceHandler是管理機(jī)制的一種。還有一種就是SharedSecrets調(diào)用tryHandlePending(false)。在另外一個類,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的時候,會從SharedSecrets來調(diào)用tryHandlePending(false)。這里又變相的進(jìn)行了一次回收。
到此,相信大家對“java是怎么做資源回收補(bǔ)救的”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
新聞標(biāo)題:java是怎么做資源回收補(bǔ)救的
鏈接URL:http://muchs.cn/article32/iejcpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、小程序開發(fā)、軟件開發(fā)、營銷型網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)