Heap Pollution in Java

Today, we’ll learn about Heap Pollution in Java.

It is also called bad data in memory. In Java, it is a kind of situation, which occurs whenever a parameterized variable points to an object which is not of that type.

Detecting Heap Pollution in Java

Mostly, it is the compiler that detects this situation during compile time itself and throws an unchecked warning.

During run time, there’s a fair chance of this situation showing up which causes ClassCastException. Bad data – is an object of type A but, here we expect an object of type B and so at runtime, it throws ClassCastException.

The following code is a demonstration of this:

import java.util.*; 
class HPoll { 
    public static void main(String[] args) 
        // making a String type list
        List<String> lOS = new ArrayList<>(); 
        lOS.add("HeapPollution found here"); 
// making an integer list which carries 
// a reference of the String list
// Now our compiler detects that 
// there's a probable Heap pollution 
// so, the Compiler throws an unchecked warning 
// during the compile time itself 
        List<Integer> loI 
            = (List<Integer>)(Object)lOS; 
// accessing the first element of list Of Integer(loI)  holding reference of a List of type String 
// Also storing it into an integer
        Integer f_Ele 
            = lOI.get(0); 

Class HPoll encloses the main function. lOI- list of integer, lOS- list of string.


During compile time:

Heap Pollution in Java

Run time output:

Error: Could not find or load main class HPoll
Caused by: java.lang.ClassNotFoundException: HPoll

Deal with it

Once again, we have class HPoll having the following methods:

  • void merge(): here an array object of type list<string> is made. Declaring a temporary variable of list<integer> and assigning the value of Ary[0] to it. ClassCastException is thrown in here.
  • main(): making instances of list<string> namely l_1, l_2 and l_3. Adding their values and merging them.
import java.util.*; 
class HPoll { 
    public static void merge(List<String>... stringList) 
        // Array 'Ary' of type Object has the reference of array of the type List<String> 
        Object[] Ary = stringList; 
        List<Integer> temporary_var = new ArrayList<Integer>(); 
        // assigning temporary_var of type List<Integer> into Ary[0] 
        // its type is List<String> 
        // ClassCastException will be thrown here
        Ary[0] = temporary_var; 
        String f_element = stringList[0].get(0); 
    // Main section 
    public static void main(String args[]) 
        List<String> l_1 = new ArrayList<>(); 
        List<String> l_2 = new ArrayList<>(); 
        List<String> l_3 = new ArrayList<>(); 
        merge(l_1, l_2, l_3); 


Compile time output of this code is as follows:

Detecting Heap Pollution in Java

During run time:
java HPoll

Thus, we saw how to detect and deal with such a situation in Java.

Though we cannot prevent this situation, yet, some norms can be followed:

  • Restrict using varargs parameters  along with generic types
  • Or restrict casting an Object array onto another array of the generic type.
  • Avoid exposing varargs parameter / generic array to other methods.

Eliminating bad data in memory isn’t completely impossible! Prior knowledge on Java syntaxes and methods would come in handy, to understand code snippets better.

Leave a Reply

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