Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A "Heap Pollution" as in Non-Reifiable Types (The Java™ Tutorials > Learning the Java Language > Generics (Updated))

Why is it called that way?

share|improve this question
A good reason for not having any warnings in your project. – user1249 Jul 8 '12 at 8:00
@ThorbjørnRavnAndersen Good one. But still - there should be a reason / mnemonic device behind that name. It is funny but even google does not seem to know why. – user18404 Jul 8 '12 at 8:07
up vote 38 down vote accepted

Heap pollution simply implies that you have "bad stuff" in your heap. It is an analogy to (for example) water pollution which is where you have "bad stuff" in the water.

Specifically, the bad stuff here is objects of type A where you ought to have objects of type B ... according to the static typing. Some hole in the static typing is allowing the bad stuff to leak into the heap ... where it is liable to cause damage (e.g. unexpected ClassCastExceptions) at runtime.

It is not a particularly good analogy, but it is the terminology that the Java folks coined ... and they have (in effect) defined it in the page that you linked to, and probably other places.

But still - there should be a reason / mnemonic device behind that name.

Why should there be? There are lots of words and phrases in use in English where nobody really knows the origin or the reasoning. (In this case, the source is known, and the reasoning are self evident ...)

It is funny but even google does not seem to know why.

Not really. It just means that nobody had previously thought to ask for a definition in a place that Google indexes. 1) The usage is restricted to the Java programming AFAIK, and 2) the analogy is obvious ... to most people who would use the term.

share|improve this answer
More precisely, heap pollution can only occur when using generics. If you have code such as: A someVar = ...; The variable someVar can never refer to an object of type B (assuming that A and B are unrelated types). However, if you have List<A> someList = ...; It is possible for someList to contain references to objects of type B, and if this occurs, the heap is said to be polluted. This can occur when code contains unchecked casts (which generate warnings), or mixes raw types and generics, which also generate such warnings. – Stuart Marks Jul 13 '12 at 16:16
@StuartMarks - this is true, but the OP was looking for the reasons why this is called "heap pollution", not a technical explanation. (Indeed, he already has the technical explanation in the page he linked to!) – Stephen C Jul 20 '12 at 4:31
@StephenC That's why it's a comment rather than an answer. – MC Emperor Sep 10 '15 at 14:35
@MCEmperor - And that is why my comment was a comment too. What actually is your point? – Stephen C Sep 12 '15 at 4:54
@StephenC That it's not meant to be an answer to the question; instead, it's meant to be a clarification or continuation of the post that the comment is on. Therefore, it's not subject to things like "this doesn't answer OP's question". – QPaysTaxes May 14 at 23:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.