Silas Davis created AVRO-1640:
---------------------------------

             Summary: Does SpecificData.schemaCache actually do anything?
                 Key: AVRO-1640
                 URL: https://issues.apache.org/jira/browse/AVRO-1640
             Project: Avro
          Issue Type: Bug
          Components: java
    Affects Versions: 1.7.7
            Reporter: Silas Davis
            Priority: Minor


I've been looking at the source code for SpecificData. On line 182 a 
schemaCache is defined as a WeakHashMap.

Having looked into how WeakHashMap works - it stores a WeakReference for the 
keys of the map but not the values, I can't see how the schemaCache actually 
provides a cache. I think it just stores all Schema objects it has ever seen, 
and never expires any. This is because it stores keys of type 
java.lang.reflect.Type key, typlically a Class<?>, which typically never fall 
out of reference. The only way the Class (a static singleton object) will be 
garbage collected is if the ClassLoader that holds a reference to it is garbage 
collected. This mostly doesn't happen, so the schemaCache is really just 
behaving as a HashMap.

It seems like WeakHashMap is commonly mistakenly used as a cache:

- 
http://stackoverflow.com/questions/1802809/javas-weakhashmap-and-caching-why-is-it-referencing-the-keys-not-the-values
- 
http://www.codeinstructions.com/2008/09/weakhashmap-is-not-cache-understanding.html

Perhaps I am missing something, like Avro's use of ClassLoaders meaning that 
Class objects do get collected, apologies if so.





--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to