Bukkit/Bukkit — pull request #815 by crast (2013-03-22 00:17:52+00:00)
Title: [B] fix large classloader leak / memory leak in metadata system [BUKKIT-3854]

This is the pull request for [BUKKIT-3854](https://bukkit.atlassian.net/browse/BUKKIT-3854)
This PR also includes commits from #771, that PR should be approved before approving this one.

The Metadata system has all metadata values implement a method getOwningPlugin, which in all MetadataValue implementations keeps a strong reference to a Plugin. Furthermore, there is no clearing of this data when plugins unload.

This effectively leads to a classloader leak, as well as a potentially huge memory leak, metadata values will keep a reference to a plugin which keeps a reference to the classloader as well as a potentially giant object graph. It can also cause NPE's if old versions of the plugin get called into for any reason.

To remedy this I have implemented some changes in the metadata storage to have the owning plugin be a weak reference and changed the internal datastructures to be stored in a canonical form to the concept of having one metadata value per plugin per key.

The advantages:
1. Data from plugins will now be cleared properly when they fall out of scope.
2. setMetadata and removeMetadata are now O(1) operations instead of O(*n*)
3. Less headache overall

The disadvantages:
1. There will likely be a small increase in memory usage of the metadata system. However, if #812 is accepted, then probably 10x as much memory will be saved as this change will add.