14

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]].

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • Don't you know how to initialize a big Array of Arrays or Vector of Vector? Or don't you know how to measure the memory footprint? I would expect a linear relationship, and try to disprove or prove this idea for some forms of collection. Then measure for some sample sizes up to the limit of my machine. – user unknown Jun 08 '11 at 02:49
  • It's more that I haven't performed those measurements and was wondering if anybody knew... – huynhjl Jun 12 '11 at 04:07

2 Answers2

17

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.

huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • Vectors can't hold primitives (because they are not specialized (yet?)) but Arrays can. So an Array[Int] would take much less memory than Vector[Int]. – Display Name Apr 24 '14 at 22:44
1

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 
user unknown
  • 35,537
  • 11
  • 75
  • 121