2

Is there any method to compute actual size of LinkedList in bytes?

Suppose I have this List:

final LinkedList<String> strings = new LinkedList<>();
final SecureRandom random = new SecureRandom();
for (int i = 0; i < 600; i++) {
    final String e = new BigInteger(130, random).toString(32);
    strings.add(e);
}

How to calculate how many bytes in heap takes this LinkedList. Not just this 600 Strings, but all links and other meta-data of LinkedList


I ended up with the proposed method using Runtime.getRuntime().

Generating strings with the method mentioned above ( each String with length 32 ) give me this values in bytes:

Length      Size in bytes       L/S
---         ---                 ---
5           0                   0
10          0                   0
25          102.4036458333      0.244131932966                                  
50          102.4036458333      0.488263865931                                  
125         0                   0      
250         102.4036458333      2.44131932966                                  
625         614.41796875        1.01722285445                                    
1250        1843.2291666667     0.678157671659                         
3125        5632.1041666667     0.5548547945                         
6250        11162.2265625       0.55992413028                           
15625       500.9817708333      31.1887595711                          
31250       29318.5390625       1.06587848506                           
78125       30802.51171875      2.53631913895                          
156250      117437.975260417    1.33048955973                           
390625      84691.7486979167    4.61231472966                           
781250      125415.6875         6.22928451435                             
1953125     309488.9921875      6.31080603609 
3906250     509038.018229167    7.67378832251                                      

enter image description here

c0rp
  • 501
  • 1
  • 10
  • 31
  • 3
    (You may have to use java.lang.instrument ) Please check similar size-finding question in stackoverflow: http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – a3.14_Infinity Oct 11 '15 at 06:02

2 Answers2

2

Write a method like :

private long getMemoryUsage(){
    long totalMem= Runtime.getRuntime().totalMemory();
    long freeMem = Runtime.getRuntime().freeMemory();
    return (totalMem- freeMem );
  }

And call this method just before and after creating and populating the list.

long startMemory = getMemoryUsage();
final LinkedList<String> strings = new LinkedList<>();
final SecureRandom random = new SecureRandom();
   for (int i = 0; i < 600; i++) {
       final String e = new BigInteger(130, random).toString(32);
       strings.add(e);
    }
      long endMemory = getMemoryUsage();

      int size = (endMemory - startMemory ) 

But it will never give you a exact result

Rahman
  • 3,755
  • 3
  • 26
  • 43
1

I think you can use Runtime.totalMemory() and Runtime.freeMemory() to calculate it1. Also, I would prefer to program to the interface(s). Something like,

Runtime rt = Runtime.getRuntime();
long heapStart = rt.totalMemory() - rt.freeMemory();
final List<String> strings = new LinkedList<>();
final Random random = new SecureRandom();
for (int i = 0; i < 600; i++) {
    final String e = new BigInteger(130, random).toString(32);
    strings.add(e);
}
long heapEnd = rt.totalMemory() - rt.freeMemory();
System.out.printf("Used %d bytes%n", heapEnd - heapStart);

1I'm not certain how accurate this method will be. You might be better off with a profiler (like visualvm or Chronon)

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249