티스토리 뷰

IT 지식

참조(Reference)와 GC(Garbage Collection)의 관계

혀가 길지 않은 개발자 2020. 10. 23. 00:06

참조(Reference)GC(Garbage Collection)의 관계


1. Strong Reference

public class JavaTest {
    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj = null;
        // "obj" object is no longer referencing to the instance.
        // So the "MyClass" type object is now available for garbage collection.
    }
}

class MyClass {
}
// Java program to illustrate Strong reference 
class MyClass {
    //Code..
}

public class JavaTest {
    public static void main(String[] args) {
        //Strong Reference - by default
        MyClass obj = new MyClass();

        //Now, object to which 'obj' was pointing earlier is
        //eligible for garbage collection.
        obj = null;
    }
} 

Strong Reference

 

 

 

 

2. Soft Reference

//Code to illustrate Soft reference
import java.lang.ref.SoftReference;

class MyClass {
    //code..
    public void print()
    {
        System.out.println("James Kim");
    }
}

public class JavaTest {
    public static void main(String[] args) {
        // Strong Reference
        MyClass obj = new MyClass();
        obj.print();

        // Creating Soft Reference to MyClass-type object to which 'obj'
        // is also pointing.
        SoftReference<MyClass> softRef = new SoftReference<MyClass>(obj);

        // Now, MyClass-type object to which 'obj' was pointing earlier
        // is available for garbage collection.
        // But, it will be garbage collected only when JVM needs memory.
        obj = null;

        // You can retrieve back the object which
        // has been weakly referenced.
        // It successfully calls the method.
        obj = softRef.get();

        obj.print();
    }
} 

대상 객체를 참조하는 경우가 SoftReference 객체만 존재하는 경우 GC의 대상이 됩니다.
단, JVM의 메모리가 부족한 경우에만 Heap 영역에서 제거되고 메모리가 부족하지 않다면 굳이 제거하지 않습니다.

 

실행 결과
Soft Reference

 

 

 

 

3. Weak Reference

// Java Code to illustrate Weak reference
import java.lang.ref.WeakReference;

class MyClass {
    // code
    public void print() {
        System.out.println("James Kim");
    }
}

public class JavaTest {
    public static void main(String[] args) {
        // Strong Reference
        MyClass obj = new MyClass();
        obj.print();

        // Creating Weak Reference to MyClass-type object to which 'obj'
        // is also pointing.
        WeakReference<MyClass> weakRef = new WeakReference<MyClass>(obj);

        // Now, MyClass-type object to which 'obj' was pointing earlier
        // is available for garbage collection.
        obj = null;

        // You can retrieve back the object which
        // has been weakly referenced.
        // It successfully calls the method.
        obj = weakRef.get();

        obj.print();
    }
}

대상 객체를 참조하는 경우가 WeakReferences 객체만 존재하는 경우 GC의 대상이 됩니다.
다음 GC 실행 시 무조건 Heap 메모리에서 제거됩니다.

 

실행 결과
Weak Reference

 

 

 

 

 

4. Phantom Reference

//Code to illustrate Phantom reference
import java.lang.ref.*;

class MyClass {
    // code
    public void print() {
        System.out.println("James Kim");
    }
}

public class JavaTest {
    public static void main(String[] args) {
        // Strong Reference
        MyClass obj = new MyClass();
        obj.print();

        // Creating reference queue
        ReferenceQueue<MyClass> refQueue = new ReferenceQueue<MyClass>();

        // Creating Phantom Reference to MyClass-type object to which 'obj'
        // is also pointing.
        PhantomReference<MyClass> phantomRef = new PhantomReference<MyClass>(obj, refQueue);

        // Now, MyClass-type object to which 'obj' was pointing
        // earlier is available for garbage collection.
        // But, this object is kept in 'refQueue' before
        // removing it from the memory.
        obj = null;

        // It always returns null.
        obj = phantomRef.get();

        // It shows NullPointerException.
        obj.print();
    }
}

실행 결과

 

 

 

 

 

 

참고.

www.geeksforgeeks.org/types-references-java/

 

Types of References in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

lion-king.tistory.com/entry/Java-참조-유형-Strong-Reference-Soft-Reference-Weak-Reference-Phantom-References

 

(Java) 참조 유형 (Strong Reference/ Soft Reference/ Weak Reference/ Phantom References)

Java Strong Reference/ Soft Reference/ Weak Reference/ Phantom References Java의 참조 유형에는 크게 4가지가 있습니다. 참조 유형에 따라 GC 실행 대상여부, 시점이 달라집니다. 1. Strong References (강한..

lion-king.tistory.com

 

neverfear.org/blog/view/150/Strong_Soft_Weak_and_Phantom_References_Java

 

NEVERFEAR.org - Strong, Soft, Weak and Phantom References (Java)

There are four distinct forms of references in the JVM, and indeed many of these apply to other garbage collected languages. Strong references Soft references Weak references Phantom references It's important to know the differences, what affect they have

neverfear.org

 

madplay.github.io/post/java-garbage-collection-and-java-reference

 

자바 레퍼런스와 가비지 컬렉션(Java Reference & Garbage Collection)

가비지 컬렉션에 이어서 자바 레퍼런스의 객체 참조에 대해서 알아보자.

madplay.github.io

 

 

 

 

'IT 지식' 카테고리의 다른 글

SOLID 원칙  (0) 2020.10.27
참조(Reference)의 종류  (0) 2020.10.22
GC(Garbage Collection)에 대한 고찰  (0) 2020.10.22
Java에서의 직렬화와 역직렬화  (0) 2020.10.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함