java軟引用在瀏覽器使用實(shí)例講解
如果一個網(wǎng)頁在瀏覽結(jié)束時回收內(nèi)容,需要按后退查看之前瀏覽過的頁面時重建;
如果將瀏覽過的網(wǎng)頁存儲在內(nèi)存中,會造成大量內(nèi)存浪費(fèi),甚至導(dǎo)致內(nèi)存溢出。
2、實(shí)例// 獲取瀏覽器對象進(jìn)行瀏覽 Browser browser = new Browser(); // 從后臺程序加載瀏覽頁面 BrowserPage page = browser.getPage(); // 將瀏覽完畢的頁面置為軟引用 SoftReference softReference = new SoftReference(page); // 回退或者再次瀏覽此頁面時 if(softReference.get() != null) { // 內(nèi)存充足,還沒有被回收器回收,直接獲取緩存 page = softReference.get(); } else { // 內(nèi)存不足,軟引用的對象已經(jīng)回收 page = browser.getPage(); // 重新構(gòu)建軟引用 softReference = new SoftReference(page); }
實(shí)例擴(kuò)展:
byte[] data = new byte[1*1024*1024];ReferenceQueue referenceQueue = new ReferenceQueue<>();SoftReference softReference = new SoftReference<>(data,referenceQueue);data = null;System.out.println('before:'+softReference.get());try {for (int i = 0; i byte[] temp = new byte[3*1024*1024];System.out.println('processing:'+softReference.get());}} catch (Throwable t) {System.out.println('after:'+softReference.get());t.printStackTrace();}while(referenceQueue.poll()!=null){System.out.println('self:'+softReference);softReference.clear();softReference = null;System.out.println('last:'+softReference);}VM options:-Xms5m -Xmx5m -XX:+PrintGC
到此這篇關(guān)于java軟引用在瀏覽器使用實(shí)例講解的文章就介紹到這了,更多相關(guān)java軟引用如何在瀏覽器使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送3. Python編寫nmap掃描工具4. php5.6不能擴(kuò)展redis.so的解決方法5. python 爬取嗶哩嗶哩up主信息和投稿視頻6. 關(guān)于HTML5的img標(biāo)簽7. python 如何停止一個死循環(huán)的線程8. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效9. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值10. PHP獲取時間戳等相關(guān)函數(shù)匯總
