Memory usage by collection objects

Sometimes, there are several benchmarking questions arrived for example: A Lists faster than Vectors for what you're doing or are Vectors faster than Lists? How much memory can you save using unboxed Arrays to store primitives? When you do performance tricks such as pre-allocating arrays or using a while loop instead of a foreach call, how much does it really matter? var l: List or val b: mutable.Buffer? Memory usage can be estimated using different Scala benchmark codes, for example, refer to https://github.com/lihaoyi/scala-bench.

Table 6 here shows the estimated size (bytes) of the various immutable collections of 0-elements, 1- element, 4- elements, and powers of four all the way up to 1,048,576 elements. Although mostse are deterministic, these might be changed depending on your platform:

Size 0 1 4 16 64 256 1,024 4,069 16,192 65,536 262,144 1,048,576
Vector 56 216 264 456 1,512 5,448 21,192 84,312 334,440 1,353,192 5,412,168 21,648,072
Array[Object] 16 40 96 336 1,296 5,136 20,496 81,400 323,856 1,310,736 5,242,896 20,971,536
List 16 56 176 656 2,576 10,256 40,976 162,776 647,696 2,621,456 10,485,776 41,943,056
Stream (unforced) 16 160 160 160 160 160 160 160 160 160 160 160
Stream (forced) 16 56 176 656 2,576 10,256 40,976 162,776 647,696 2,621,456 10,485,776 41,943,056
Set 16 32 96 880 3,720 14,248 59,288 234,648 895,000 3,904,144 14,361,000 60,858,616
Map 16 56 176 1,648 6,800 26,208 109,112 428,592 1,674,568 7,055,272 26,947,840 111,209,368
SortedSet 40 104 248 824 3,128 12,344 49,208 195,368 777,272 3,145,784 12,582,968 50,331,704
Queue 40 80 200 680 2,600 10,280 41,000 162,800 647,720 2,621,480 10,485,800 41,943,080
String 40 48 48 72 168 552 2,088 8,184 32,424 131,112 524,328 2,097,192
Table 6: The estimated size (bytes) of the various collections

The following table shows the estimated size (bytes) of the array used in Scala with 0-elements, 1- element, 4- elements, and powers of four all the way up to 1,048,576 elements. Although mostse are deterministic, these might change depending on your platform:

Size 0 1 4 16 64 256 1,024 4,069 16,192 65,536 262,144 1,048,576
Array[Object] 16 40 96 336 1,296 5,136 20,496 81,400 323,856 1,310,736 5,242,896 20,971,536
Size 0 1 4 16 64 256 1,024 4,069 16,192 65,536 262,144 1,048,576
Array[Boolean] 16 24 24 32 80 272 1,040 4,088 16,208 65,552 262,160 1,048,592
Array[Byte] 16 24 24 32 80 272 1,040 4,088 16,208 65,552 262,160 1,048,592
Array[Short] 16 24 24 48 144 528 2,064 8,160 32,400 131,088 524,304 2,097,168
Array[Int] 16 24 32 80 272 1,040 4,112 16,296 64,784 262,160 1,048,592 4,194,320
Array[Long] 16 24 48 144 528 2,064 8,208 32,568 129,552 524,304 2,097,168 8,388,624
Boxed Array[Boolean] 16 40 64 112 304 1,072 4,144 16,328 64,816 262,192 1,048,624 4,194,352
Boxed Array[Byte] 16 40 96 336 1,296 5,136 8,208 20,392 68,880 266,256 1,052,688 4,198,416
Boxed Array[Short] 16 40 96 336 1,296 5,136 20,496 81,400 323,856 1,310,736 5,230,608 20,910,096
Boxed Array[Int] 16 40 96 336 1,296 5,136 20,496 81,400 323,856 1,310,736 5,242,896 20,971,536
Boxed Array[Long] 16 48 128 464 1,808 7,184 28,688 113,952 453,392 1,835,024 7,340,048 29,360,144
Table 7: The estimated size (bytes) of the arrays in Scala

However, this book does not set out to distinguish them in a broad way and hence we will omit any discussion on these topics. Refer to the following information box for further guidelines on these:

For very detailed benchmarking for Scala Collections with timed code, refer to this link on GitHub (https://github.com/lihaoyi/scala-bench/tree/master/bench/src/main/scala/bench).

As we mentioned in Chapter 1, Introduction to Scala, Scala has a very rich Collection API. The same applies for Java but there are lots of differences between the two Collection APIs. In the next section, we will see some examples on Java interoperability.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.10.69