Adam Smolnik wrote:An unintended side effect is possible according to me - (redundant) creation of several new FooClass(barCode) instances during simultaneous initial access.
Adam
Yes. But those objects (the redundant copies) will be available for GC just after the method returns. I did not find a solution for that, any ideas?
Jelle Klap wrote:
Aside from the potentially (...)
Thanks for the info. Do you have any ideas how to solve my problem if I am determined to use a cache? I will access this method very frequently, and the pool of the objects can be 8-15 maximum.
Is there a way to implement such valueOf methods? I looked at the JDK sources and those are different case, since I have no idea what barCode can be.
EDIT2:
I am sure that I will have a maximum of about 15 objects I this cache. But the method will be called veery frequently. So I want to use it to optimalise memory usage so that I dont create lots of equal immutable objects.