What is Heap Pollution in Java and how to resolve it?
Last Updated :
29 Jan, 2020
What is Heap Pollution?
Heap pollution implies that we have bad data in our heap memory. In Java language, heap pollution is a situation that occurs when a variable of parameterized type points to an object that is not of that parameterized type.
How is Heap Pollution detected?
Usually, the compiler detects the heap pollution situation at the compile-time only and it throws unchecked warning message.
At the run-time, there is a chance of arising heap pollution that will cause ClassCastException. Heap pollution means the bad data in the heap memory. Here bad data is an object of type X but an object of type Y is expected and it will throw ClassCastException at runtime.
Lets understand the heap pollution with program:
import java.util.*;
class Geeks {
public static void main(String[] args)
{
List<String> listOfString = new ArrayList<>();
listOfString.add( "Geeksforgeeks" );
List<Integer> listOfInteger
= (List<Integer>)(Object)listOfString;
Integer firstElement
= listOfInteger.get( 0 );
System.out.println(firstElement);
}
}
|
Compile Time Console:
prog.java:12: warning: [unchecked] unchecked cast
List listOfInteger = (List)(Object)listOfString;
^
required: List
found: Object
1 warning
Output:
Exception in thread “main” java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at Geeks.main(File.java:16)
How to deal with Heap Pollution?
import java.util.*;
class Geeks {
public static void merge(List<String>... stringList)
{
Object[] arr = stringList;
List<Integer> temp = new ArrayList<Integer>();
temp.add( 420 );
arr[ 0 ] = temp;
String firstEle = stringList[ 0 ].get( 0 );
System.out.println(firstEle);
}
public static void main(String args[])
{
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<>();
list1.add( "Geeks" );
list2.add( "for" );
list3.add( "geeks" );
merge(list1, list2, list3);
}
}
|
Compile Time Console:
prog.java:4: warning:
[unchecked] Possible heap pollution from
parameterized vararg type List
public static void merge(List... stringList)
^
prog.java:23: warning:
[unchecked] unchecked generic array creation
for varargs parameter of type List[]
merge(list1, list2, list3);
^
2 warnings
Output:
Exception in thread "main" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.lang.String
at Geeks.merge(File.java:10)
at Geeks.main(File.java:23)
Note: If we don’t want warnings at the compiler time then we can use @SafeVarargs annotation above the method. If we know that the method doesn’t contain any heap pollution situation then you can annotate it with @SafeVarargs to suppress the warning. It does not mean that it will allow our code for heap pollution. It means that if in the code, there is a chance of Heap pollution, it will throw ClassCastException at the run time.
How to prevent Heap pollution situations?
We cant prevent Heap pollution situations, but we can follow a few rules that can help us to prevent heap pollution like:
- Don’t use varargs parameters with generic types or cast an Object array to an array of a generic type.
- Not to expose the varargs parameter or the generic array to any other method.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...