Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Too much memory for record locking?

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Im using a map based implementation for record locking. Right now, im using a HashMap. But, im concerned that i might have to use a WeakHashMap.
The reason is, if the number of records are more, each creates an object in the map. Even if no thread is waiting/holding the record lock, it is never freed.
Do you think it is necessary for me to use a WeakHashMap? (i will have to take care of holding a strong reference as long as the record is locked)
Dushy
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dushy,
Why can't your unlock() method remove the object from the Map?
Why are you worried about how much memory this is taking up? How many records would you have to have locked in order for the memory to become an issue?
Regards, Andrew
 
Dushy Inguva
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Andrew,
Thanks for your fast reply.
Yes my unlock could see if there are no threads waiting to obtain the lock and remove it. The problem is, I am afraid that might increase the number of objects being created and destroyed.
Well, for the project, this might not be a problem, but since Sun is going to look at the design decisions as well, i am not sure which approach to take.
Here are the possible approaches that i am looking at:
1. Just store them in a HashMap and not do any memory freeing
2. Store them in a HashMap and build intelligent unlock method
3. Store them in a WeakHashMap and leave it on the gc. (Ill have to make my lock restore a strong reference to the object and unlock remove it)
Could you help me choose...
Thanks
Dushy
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dushy,
I just want to check my understanding: you are storing an object in the map for each record thats locked, and any waiting clients will wait on that object that is within the map. Is that correct?
Regarding leaving the lock object in the map: how often is it likely to be called during the record's life cycle? Once you answer that, you might find yourself leaning towards one of those three options you mentioned.
Regards, Andrew
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic