jvm:java中的引用(強引用、軟引用、虛引用、弱引用)


1、分類

強引用、軟引用、弱引用、虛引用、終結器引用

 

強引用:只要能夠通過GC Root的引用鏈找到就不會被垃圾回收,也就是說只有所有的GC Roots對象都不通過強引用引用該對象的時候,該對象才能被垃圾回收

弱引用:如果某個對象與弱引用關聯,那么當JVM在進行垃圾回收時,無論內存是否充足,都會回收此類對象。

軟引用:java中使用SoftRefence來表示軟引用,如果某個對象與軟引用關聯,那么JVM只會在內存不足的情況下回收該對象

虛引用:創建的時候會關聯一個引用隊列

例如:創建ByteBuffer的時候會創建一個名為Cleaner的虛引用對象,當ByteBuffer沒有被強引用所引用就會被jvm垃圾回收,虛引用Cleaner就會進入引用隊列,會有專門的線程掃描引用隊列,被發現后會調用直接內存地址的方法將直接內存釋放掉,保證直接內存不會導致內存泄漏

是所有引用類型中最弱的,一個對象是否有虛引用的存在,完全不會對其生命周期構成影響,也無法通過虛引用獲得一個對象實例。

終結器引用:創建的時候會關聯一個引用隊列,當A4對象沒有被強引用所引用時,A4被垃圾回收的時候,會將終結器引用放入到一個引用隊列(被引用對象暫時還沒有被垃圾回收),有專門的線程(優先級較低,可能會造成對象遲遲不被回收)掃描引用隊列並調用finallize()方法,第二次GC的時候才能回收掉被引用對象

 

2、強引用

(1)強引用的使用:

設置jvm的內存為20M

public class Test2 {
    public static final int _4MB=4*1024*1024;
    public static void main(String[] args) throws IOException {
        List<byte[]> list =new ArrayList<>();
        for(int i=0;i<5;i++){
            list.add(new byte[_4MB]);
        }
        System.in.read();
    }
}

運行上面的程序會出現內存溢出問題,也就是說有的內存即使不太重要依舊會被占用:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at pers.zhb.test.Test2.main(Test2.java:12)

 

3、軟引用

(1)軟引用的場景:

輸入jvm指令:

public class Test2 {
    private static final int _4MB=4*1024*1024;
    public static void main(String[] args) throws IOException {
        soft();
    }
    public static void soft(){
        List<SoftReference<byte[]>> list=new ArrayList<>();
        for(int i=0;i<5;i++){
            SoftReference<byte[]> reference=new SoftReference<>(new byte[_4MB]);
            System.out.println(reference.get());
            list.add(reference);
            System.out.println(list.size());
        }
        System.out.println("循環結束:"+list.size());
        for(SoftReference<byte[]> reference:list){
            System.out.println(reference.get());
        }
    }
}

運行結果:

[B@1b6d3586
1
[B@4554617c
2
[B@74a14482
3
[GC (Allocation Failure) [PSYoungGen: 1744K->488K(6144K)] 14032K->12932K(19968K), 0.0427814 secs] [Times: user=0.00 sys=0.00, real=0.05 secs] 
[B@1540e19d
4
[GC (Allocation Failure) --[PSYoungGen: 4696K->4696K(6144K)] 17140K->17140K(19968K), 0.0030222 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 4696K->4574K(6144K)] [ParOldGen: 12444K->12414K(13824K)] 17140K->16988K(19968K), [Metaspace: 3118K->3118K(1056768K)], 0.0095409 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) --[PSYoungGen: 4574K->4574K(6144K)] 16988K->16996K(19968K), 0.0010492 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 4574K->0K(6144K)] [ParOldGen: 12422K->587K(8704K)] 16996K->587K(14848K), [Metaspace: 3118K->3118K(1056768K)], 0.0076471 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 
[B@677327b6
5
循環結束:5
null
null
null
null
[B@677327b6
Heap
 PSYoungGen      total 6144K, used 4546K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 5632K, 80% used [0x00000000ff980000,0x00000000ffdf0940,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 8704K, used 587K [0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000)
  object space 8704K, 6% used [0x00000000fec00000,0x00000000fec92d20,0x00000000ff480000)
 Metaspace       used 3224K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 349K, capacity 388K, committed 512K, reserved 1048576K

可以看出,在第四次和第五次的時候就觸發了垃圾回收,在對集合進行遍歷后只有第五個軟引用對象存在,也就是說在內存不足的時候將前面的虛引用的對象進行了垃圾回收

(2)軟引用與引用隊列:

public class Test2 {
    private static final int _4MB=4*1024*1024;
    public static void main(String[] args) throws IOException {
        List<SoftReference<byte[]>> list=new ArrayList<>();
        //引用隊列
        ReferenceQueue<byte[]> queue=new ReferenceQueue<>();
        for(int i=0;i<5;i++){
            //關聯了引用隊列,當軟引用所關聯的byte數組被回收時,軟引用自己會加入到引用隊列中
            SoftReference<byte[]> reference=new SoftReference<>(new byte[_4MB],queue);
            System.out.println(reference.get());
            list.add(reference);
            System.out.println(list.size());
        }
        //從隊列獲取無用的軟引用對象並移除
        Reference<? extends byte[]> poll=queue.poll();
        while(poll!=null){
            list.remove(poll);
            poll=queue.poll();
        }
        System.out.println("循環結束");
        for(SoftReference<byte[]> reference:list){
            System.out.println(reference.get());
        }
    }
}

測試:

[B@1b6d3586
1
[B@4554617c
2
[B@74a14482
3
[GC (Allocation Failure) [PSYoungGen: 1866K->488K(6144K)] 14154K->12964K(19968K), 0.0010895 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1540e19d
4
[GC (Allocation Failure) --[PSYoungGen: 4696K->4696K(6144K)] 17172K->17180K(19968K), 0.0011250 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 4696K->4564K(6144K)] [ParOldGen: 12484K->12458K(13824K)] 17180K->17022K(19968K), [Metaspace: 3228K->3228K(1056768K)], 0.0055610 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) --[PSYoungGen: 4564K->4564K(6144K)] 17022K->17030K(19968K), 0.0009186 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 4564K->0K(6144K)] [ParOldGen: 12466K->620K(8704K)] 17030K->620K(14848K), [Metaspace: 3228K->3228K(1056768K)], 0.0065061 secs] [Times: user=0.00 sys=0.01, real=0.01 secs] 
[B@677327b6
5
循環結束
[B@677327b6
Heap
 PSYoungGen      total 6144K, used 4546K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 5632K, 80% used [0x00000000ff980000,0x00000000ffdf0918,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 8704K, used 620K [0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000)
  object space 8704K, 7% used [0x00000000fec00000,0x00000000fec9b160,0x00000000ff480000)
 Metaspace       used 3237K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 351K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

 

4、弱引用

(1)弱引用舉例

public class Test2 {
    private static final int _4MB = 4 * 1024 * 1024;

    public static void main(String[] args) throws IOException {
        List<WeakReference<byte[]>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            WeakReference<byte[]> reference = new WeakReference<>(new byte[_4MB]);
            list.add(reference);
            for (WeakReference<byte[]> w : list) {
                System.out.println(w.get());
            }
            System.out.println();
        }
        System.out.println("循環結束:" + list.size());
    }
}

(2)測試:

[B@1b6d3586

[B@1b6d3586
[B@4554617c

[B@1b6d3586
[B@4554617c
[B@74a14482

[GC (Allocation Failure) [PSYoungGen: 1865K->488K(6144K)] 14153K->12992K(19968K), 0.0014680 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
[B@1540e19d

[GC (Allocation Failure) [PSYoungGen: 4809K->496K(6144K)] 17313K->13044K(19968K), 0.0013554 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
null
[B@677327b6

[GC (Allocation Failure) [PSYoungGen: 4702K->472K(6144K)] 17250K->13020K(19968K), 0.0017348 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
null
null
[B@14ae5a5

[GC (Allocation Failure) [PSYoungGen: 4678K->488K(6144K)] 17226K->13036K(19968K), 0.0016909 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
null
null
null
[B@7f31245a

[GC (Allocation Failure) [PSYoungGen: 4694K->488K(6144K)] 17242K->13044K(19968K), 0.0008993 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
null
null
null
null
[B@6d6f6e28

[GC (Allocation Failure) [PSYoungGen: 4694K->472K(5120K)] 17250K->13028K(18944K), 0.0011246 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@1b6d3586
[B@4554617c
[B@74a14482
null
null
null
null
null
[B@135fbaa4

[GC (Allocation Failure) [PSYoungGen: 4657K->32K(5632K)] 17214K->13012K(19456K), 0.0012595 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 32K->0K(5632K)] [ParOldGen: 12980K->638K(7680K)] 13012K->638K(13312K), [Metaspace: 3232K->3232K(1056768K)], 0.0084781 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
null
null
null
null
null
null
null
null
null
[B@45ee12a7

循環結束:10
Heap
 PSYoungGen      total 5632K, used 4371K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 4608K, 94% used [0x00000000ff980000,0x00000000ffdc4e70,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 7680K, used 638K [0x00000000fec00000, 0x00000000ff380000, 0x00000000ff980000)
  object space 7680K, 8% used [0x00000000fec00000,0x00000000fec9fa38,0x00000000ff380000)
 Metaspace       used 3239K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 351K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM