Which class is super class of every class

What is weak hashing ?

Answer : Weak hashing or weak reference is a reference object that does not guarantee the referenced object from garbage collection compare to strong reference which protects reference object from garbage collection.

A weak reference object means “every chain of object references at least one weak reference contain as a link” and should be treated as weakly reachable and garbage collected at any time.
Java supports three levels of references:
Strong reference: When we create new objects that would be created on the heap and will be treated as normal reference by JVM will be protected form garbage collector.

Please see example below. This code will throw out of memory exception because its strongly reference and not garbage collected:

package com.javahonk.strongreference;

import java.util.LinkedList;
import java.util.List;

public class StrongReferenceTest {

	private static List<String> strongRef 
	= new LinkedList<String>();

	public static void main(String[] args) {
		//call this method it will throw 
		// out of memory exception 
		// if not add more data to check
		StrongReferenceMethod();

	}

	private static void StrongReferenceMethod() {
	try {
		for (int i = 0; i < 5000000; i++) {
			strongRef.add(
			"Strong reference test: "+i);
		}
	} catch (OutOfMemoryError e) {
		System.out.println("outofmermory: " + e);
	} catch (Throwable e) {
		System.out.println("UnexpectedError" + e);
	}

	}

}

 

Output:
What is weak hashing

 

Soft Reference: This kind of reference object is softly reachable to garbage collector but will be cleared by garbage collector if it need memory.Please see example below this will not throw out of memory error once memory is on the peak it will be garbage collected and memory will be reclaimed.

package com.javahonk.strongreference;

import java.lang.ref.SoftReference;
import java.util.LinkedList;
import java.util.List;

public class SoftReferenceTest {

	private static List<SoftReference<String>> softRef = 
			new LinkedList<SoftReference<String>>();

	public static void main(String[] args) {
		SoftReferenceMethod();

	}

	private static void SoftReferenceMethod() {
	try {
		for (int i = 0; i < 2500000; i++) {
			SoftReference<String> data = 
			new SoftReference<String>(""
			+ "soft ref test: " + i);
            softRef.add(data);
		}
	} catch (OutOfMemoryError oError) {
		System.out.println("Out of memory: " + oError);
	} catch (Throwable e) {
		System.out.println("UnexpectedError" + e);
	}

	}

}

 

Weak reference: Weak reference object is weakly reachable object which can be garbage collected at any point of time. Please see example below this will not throw out of memory error and will be garbage collected :

package com.javahonk.strongreference;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

public class WeakReferenceTest {

	private static List<WeakReference<String>> 
	weakRef = new LinkedList<WeakReference<String>>();

	public static void main(String[] args) {
		weakReferenceMethod();

	}

	private static void weakReferenceMethod() {
	try {
		for (int i = 0; i < 25000000; i++) {
			WeakReference<String> data = 
			new WeakReference<String>(
			"Weak ref test: " + i);
			weakRef.add(data);
		}
	} catch (Throwable e) {
		System.out.println("UnexpectedError" + e);
	}

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *