Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala collection memory footprint characteristics

There is a handy page about performance characteristics of the Scala collection classes. Is there similar data on memory footprint?

I have a situation where I'm concerned about memory use and would like to factor this in my choice of collection to use. For instance, between Array[Array[T]] and Vector[Vector[T]].

like image 717
huynhjl Avatar asked Jun 05 '11 13:06

huynhjl


People also ask

What are different collection data types in Scala?

There are two types of collections in Scala – mutable and immutable.

Which is the topmost collection type in the Scala collections hierarchy?

Traversable is the top of the Collection hierarchy. It defines a lot of the great operations that the Scala Collections has to offer.

What are Scala collections?

Scala has a rich set of collection library. Collections are containers of things. Those containers can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may have an arbitrary number of elements or be bounded to zero or one element (e.g., Option). Collections may be strict or lazy.


2 Answers

Here is what I've found out by filling up those respective immutable sequences with 1,000,000 objects on 2.9.0. I had them all point to the same object to factor out the size of the content.

  • Array: 1x (baseline 4,000,016 bytes on 32 bits; 8,000,024 on 64 bits)
  • Vector: 1.17x
  • List, Queue, Stack: 4x
  • evaluated Stream: 10x

System.gc was called then triggered heap dump then opened in Eclipse MAT.

Based on that Array and Vector are pretty closed.

like image 143
huynhjl Avatar answered Oct 05 '22 17:10

huynhjl


You could start with a simple generation of multiple, multidimensional Vectors and Arrays of different size:

val vMin = Vector.fill (10  , 10)(9)
val vMed = Vector.fill (1000, 10)(9)
val aMed =  Array.fill (1000, 10)(9)

10 Arrays of 10 Arrays of Ints with Value 9, 1000 such Arrays, 1000 such Vectors ...

To measure the size, you could use

$JAVA_HOME/bin/jvisualvm 
like image 37
user unknown Avatar answered Oct 05 '22 18:10

user unknown