mikemccand commented on PR #13581:
URL: https://github.com/apache/lucene/pull/13581#issuecomment-2260766983

   I was curious about the HEAP cost of `Lock` and wrote a silly little program 
that keeps making and saving `Lock` into an `ArrayList` and catches OOME and 
prints how far it got:
   
   ```java
   import java.util.concurrent.locks.ReentrantReadWriteLock;
   import java.util.concurrent.locks.Lock;
   import java.util.ArrayList;
   import java.util.List;
   import java.util.Locale;
   import java.util.Random;
   
   public class LockHeap {
   
     public static void main(String[] args) {
       final List<ReentrantReadWriteLock> locks = new ArrayList<>();
       Thread[] threads = new Thread[16];
       //Thread[] threads = new Thread[0];                                      
                                                                                
                                                                                
              
       for(int i=0;i<threads.length;i++) {
         threads[i] = new Thread() {
             @Override
             public void run() {
               Random random = new Random();
               while (true) {
                 int size = locks.size();
                 if (size == 0) {
                   continue;
                 }
                 ReentrantReadWriteLock lock = locks.get(random.nextInt(size));
                 Lock lock2;
                 if (random.nextBoolean()) {
                   lock2 = lock.readLock();
                 } else {
                   lock2 = lock.writeLock();
                 }
                 lock2.lock();
                 lock2.unlock();
               }
             }
           };
         threads[i].start();
       }
       int count = 0;
       while (true) {
         try {
           ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
           //lock.readLock();                                                   
                                                                                
                                                                                
              
           //lock.writeLock();                                                  
                                                                                
                                                                                
              
           locks.add(lock);
           count++;
           if (count % 10000 == 0) {
             System.out.println(String.format(Locale.ROOT, "%.1fM locks", count 
/ 1_000_000.));
           }
         } catch (OutOfMemoryError oome) {
           locks.clear();
           System.out.println(String.format(Locale.ROOT, "OOME at %.1fM locks", 
count / 1_000_000.));
           throw oome;
         }
       }
     }
   }
   ```
   
   When I run with `-Xmx2g` I get:
   ```
   17.2M locks
   17.2M locks
   OOME at 17.2M locks
   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:241)
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:231)
           at LockHeap.main(LockHeap.java:41)
   ```
   
   and `-Xmx4g`:
   
   ```
   34.2M locks
   OOME at 34.2M locks
   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:241)
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:231)
           at LockHeap.main(LockHeap.java:41)
   ```
   
   and `-Xmx6g`:
   
   ```
   51.3M locks
   51.3M locks
   OOME at 51.3M locks
   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock$Sync.<init>(ReentrantReadWriteLock.java:339)
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync.<init>(ReentrantReadWriteLock.java:680)
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:241)
           at 
java.base/java.util.concurrent.locks.ReentrantReadWriteLock.<init>(ReentrantReadWriteLock.java:231)
           at LockHeap.main(LockHeap.java:41)
   ```
   
   So it seems to be a linear function :)  And it works out to ~125 bytes per 
`Lock`:
   
   ```
   raptorlake:~$ python3
   Python 3.12.4 (main, Jun  7 2024, 06:33:07) [GCC 14.1.1 20240522] on linux
   Type "help", "copyright", "credits" or "license" for more information.
   >>> (6 * 1024 * 1024 * 1024) / 51300000
   125.58383906432749
   ```
   
   [Turning on the threads randomly acquiring/releasing the read and write lock 
didn't seem to change the heap usage]


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to