티스토리 뷰
참조(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;
}
}
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 영역에서 제거되고 메모리가 부족하지 않다면 굳이 제거하지 않습니다.
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 메모리에서 제거됩니다.
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/
neverfear.org/blog/view/150/Strong_Soft_Weak_and_Phantom_References_Java
madplay.github.io/post/java-garbage-collection-and-java-reference
'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
링크
TAG
- 혀가 길지 않은 개발자
- ArrayList
- fragment
- Android
- Architecture Pattern
- Intent
- java
- CoordinatorLayout
- Vue.js #Vue.js + javascript
- coroutine
- 안드로이드 #코틀린 #Android #Kotlin
- recyclerview
- 안드로이드
- 코틀린
- TabLayout
- ViewModel
- XML
- Kotlin
- JSONObject
- handler
- Design Pattern
- activity
- Livedata
- 자바
- JSONArray
- DataBinding
- James Kim
- MVVM
- ViewPager2
- View
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함