[concurrency-interest] Lock memory/resource Cost

Doug Lea dl at cs.oswego.edu
Mon Oct 31 08:05:18 EST 2005


Thierry Hanot wrote:

>Hello 
>
>I 'm just wondering what is the memory/system resource cost of
>ReentrantLocks or read write lock.
>  
>
An extension to David's answer: 

Builtin locks take less space (basically just a bit) until they are ever 
used.
When they are contended, the space is probably about the same (for 
builtin locks,
space for queuing etc is allocated internally by the JVM).  When they are
simply locked without contention, builtin locks may use space that may 
or may
not be allocated on heap, but is in any case slightly less than 
ReentrantLock.

You can save a little space by subclassing ReentrantLock instead of
declaring one as a field. This is only reasonable if the class isn't public.

All in all though, if you really must create millions of these, and don't
absolutely require extended functionality (tryLock, etc) of ReentrantLock,
you are probably better off using builtin locking.

-Doug



More information about the Concurrency-interest mailing list