1 Answer
1

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.

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 MarksJul 13 '12 at 16:16

2

@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 CJul 20 '12 at 4:31