2

When looking in the Memory tab, I have a class which have 8.96 GiB Max Live Size while the Max Live Count it 401,000,000.

This a a Scala Cons class which is an element in a LinkedList of type Byte. By this logic, 401M bytes is 401 MiB (maybe times some constant), so I don't understand why the live size is 8.96 GiB, which is orders of magnitude larger.

enter image description here

Can someone please help me make sense of this?

Kayaman
  • 72,141
  • 5
  • 83
  • 121
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79

1 Answers1

4

According to Java Object Layout tool instance size of :: or

scala.collection.immutable.$colon$colon

is 24 bytes

➜ java -cp scala-library-2.13.3.jar:jol-cli.jar org.openjdk.jol.Main internals 'scala.collection.immutable.$colon$colon'
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

Instantiated the sample instance via public scala.collection.immutable.$colon$colon(java.lang.Object,scala.collection.immutable.List)

scala.collection.immutable.$colon$colon object internals:
 OFFSET  SIZE                              TYPE DESCRIPTION                               VALUE
      0     4                                   (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4                                   (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                                   (object header)                           77 0c 02 f8 (01110111 00001100 00000010 11111000) (-134083465)
     12     4                  java.lang.Object $colon$colon.head                         null
     16     4   scala.collection.immutable.List $colon$colon.next                         null
     20     4                                   (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

hence

401,000,000 * 24 byte = 9.62 Gigabtye = 8.96 Gigibyte

which makes sense because :: stores at least references to a head and a tail, in addition to standard object headers

case class::[+A](head: A, next: List[A])
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • This is a very good reason to prefer `Seq`'s which aren't `List` if there's a reasonable chance that they'll be large and outlive a method invocation. – Levi Ramsey Jul 05 '20 at 00:02
  • Weirdly enough, with `Vector` I'm hitting this weird exception: https://github.com/NetLogo/NetLogo/issues/1830 – Ori Popowski Jul 05 '20 at 12:53