Skip to content

Commit 4dbd2da

Browse files
committed
Update README.md
1 parent 0887649 commit 4dbd2da

File tree

1 file changed

+56
-87
lines changed

1 file changed

+56
-87
lines changed

README.md

Lines changed: 56 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,101 +2378,70 @@ Parallel garbage collector is also called as throughput collector. It is the def
23782378
23792379
## Q. What is difference between WeakReference and SoftReference in Java?
23802380
2381-
In Java there are four types of references differentiated on the way by which they are garbage collected.
2381+
**1. Weak References:**
23822382
2383-
* Strong References
2384-
* Weak References
2385-
* Soft References
2386-
* Phantom References
2383+
Weak Reference Objects are not the default type/class of Reference Object and they should be explicitly specified while using them.
23872384
2388-
**Strong References**: This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null.
23892385
```java
2390-
MyClass obj = new MyClass();
2391-
```
2386+
/**
2387+
* Weak Reference
2388+
*/
2389+
import java.lang.ref.WeakReference;
23922390
2393-
**Weak References**: Weak Reference Objects are not the default type/class of Reference Object and they should be explicitly specified while using them.
2394-
```java
2395-
//Java Code to illustrate Weak reference
2396-
import java.lang.ref.WeakReference;
2397-
class MainClass
2398-
{
2399-
public void message() {
2400-
System.out.println("Weak References Example");
2401-
}
2402-
}
2403-
2404-
public class Example
2405-
{
2406-
public static void main(String[] args) {
2407-
// Strong Reference
2408-
MainClass g = new MainClass();
2409-
g.message();
2410-
2411-
// Creating Weak Reference to MainClass-type object to which 'g'
2412-
// is also pointing.
2413-
WeakReference<MainClass> weakref = new WeakReference<MainClass>(g);
2414-
g = null;
2415-
g = weakref.get();
2416-
g.message();
2417-
}
2418-
}
2419-
```
2391+
class MainClass {
2392+
public void message() {
2393+
System.out.println("Weak References Example");
2394+
}
2395+
}
24202396
2421-
**Soft References**: In Soft reference, even if the object is free for garbage collection then also its not garbage collected, until JVM is in need of memory badly.The objects gets cleared from the memory when JVM runs out of memory.To create such references java.lang.ref.SoftReference class is used.
2422-
```java
2423-
//Java Code to illustrate Weak reference
2424-
import java.lang.ref.SoftReference;
2425-
class MainClass
2426-
{
2427-
public void message() {
2428-
System.out.println("Weak References Example");
2429-
}
2430-
}
2431-
2432-
public class Example
2433-
{
2434-
public static void main(String[] args) {
2435-
// Strong Reference
2436-
MainClass g = new MainClass();
2437-
g.message();
2438-
2439-
// Creating Weak Reference to MainClass-type object to which 'g'
2440-
// is also pointing.
2441-
SoftReference<MainClass> softref = new SoftReference<MainClass>(g);
2442-
g = null;
2443-
g = softref.get();
2444-
g.message();
2445-
}
2446-
}
2397+
public class Example {
2398+
public static void main(String[] args) {
2399+
// Strong Reference
2400+
MainClass g = new MainClass();
2401+
g.message();
2402+
2403+
// Creating Weak Reference to MainClass-type object to which 'g'
2404+
// is also pointing.
2405+
WeakReference<MainClass> weakref = new WeakReference<MainClass>(g);
2406+
g = null;
2407+
g = weakref.get();
2408+
g.message();
2409+
}
2410+
}
24472411
```
2448-
**Phantom References**: The objects which are being referenced by phantom references are eligible for garbage collection. But, before removing them from the memory, JVM puts them in a queue called ‘reference queue’ . They are put in a reference queue after calling finalize() method on them.To create such references java.lang.ref.PhantomReference class is used.
2412+
2413+
**2. Soft References:**
2414+
2415+
In Soft reference, even if the object is free for garbage collection then also its not garbage collected, until JVM is in need of memory badly.The objects gets cleared from the memory when JVM runs out of memory.To create such references java.lang.ref.SoftReference class is used.
2416+
24492417
```java
2450-
//Java Code to illustrate Weak reference
2451-
import java.lang.ref.*;
2452-
class MainClass
2453-
{
2454-
public void message() {
2455-
System.out.println("Phantom References Example");
2456-
}
2457-
}
2458-
2459-
public class Example
2460-
{
2461-
public static void main(String[] args) {
2462-
// Strong Reference
2463-
MainClass g = new MainClass();
2464-
g.message();
2465-
2466-
// Creating Phantom Reference to MainClass-type object to which 'g'
2467-
// is also pointing.
2468-
PhantomReference<MainClass> phantomRef = null;
2469-
phantomRef = new PhantomReference<MainClass>(g,refQueue);
2470-
g = null;
2471-
g = phantomRef.get();
2472-
g.message();
2473-
}
2474-
}
2418+
/**
2419+
* Soft Reference
2420+
*/
2421+
import java.lang.ref.SoftReference;
2422+
2423+
class MainClass {
2424+
public void message() {
2425+
System.out.println("Weak References Example");
2426+
}
2427+
}
2428+
2429+
public class Example {
2430+
public static void main(String[] args) {
2431+
// Strong Reference
2432+
MainClass g = new MainClass();
2433+
g.message();
2434+
2435+
// Creating Weak Reference to MainClass-type object to which 'g'
2436+
// is also pointing.
2437+
SoftReference<MainClass> softref = new SoftReference<MainClass>(g);
2438+
g = null;
2439+
g = softref.get();
2440+
g.message();
2441+
}
2442+
}
24752443
```
2444+
24762445
<div align="right">
24772446
<b><a href="#related-interview-questions">↥ back to top</a></b>
24782447
</div>

0 commit comments

Comments
 (0)