3

What is the size of an empty class in Scala?
I used SizeEstimator to get the size of an empty class. However the size I get is 1368 (bytes) which looks much higher than I expected.

Rahul
  • 12,886
  • 13
  • 57
  • 62
Dan.D
  • 31
  • 1

2 Answers2

2

You mean the size of a .class file for a class with no members?

package mypackage

class EmptyClass {

}

For me, on Windows with Java 1.8.0_121, using Scala 2.11, it is 527 bytes (the filesize will vary according to the package name, as the file contains the class and package name).

The javap tool shows what data is contained in the file:

$ javap -c -v -cp my-app/target/scala-2.11/classes mypackage.EmptyClass
Classfile /.../mypackage/EmptyClass.class
  Last modified 17-Jul-2017; size 527 bytes
  MD5 checksum d97a3d664755c085417b469bb84f982b
  Compiled from "EmptyClass.scala"
public class mypackage.EmptyClass
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Utf8               mypackage/EmptyClass
   #2 = Class              #1             // mypackage/EmptyClass
   #3 = Utf8               java/lang/Object
   #4 = Class              #3             // java/lang/Object
   #5 = Utf8               EmptyClass.scala
   #6 = Utf8               Lscala/reflect/ScalaSignature;
   #7 = Utf8               bytes
   #8 = Utf8               E1A!
                               \tQQ)9uscm]:
                                           \r\t\"\=qC\mZ3MA\t)i)1oY1mC&1:L(+4\t
                                                                               5\rqJg.?)yC\t
 = Utf8               <init>
  #10 = Utf8               ()V
  #11 = NameAndType        #9:#10         // "<init>":()V
  #12 = Methodref          #4.#11         // java/lang/Object."<init>":()V
  #13 = Utf8               this
  #14 = Utf8               Lmypackage/EmptyClass;
  #15 = Utf8               Code
  #16 = Utf8               LocalVariableTable
  #17 = Utf8               LineNumberTable
  #18 = Utf8               SourceFile
  #19 = Utf8               RuntimeVisibleAnnotations
  #20 = Utf8               ScalaInlineInfo
  #21 = Utf8               ScalaSig
{
  public mypackage.EmptyClass();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #12                 // Method java/lang/Object."<init>":()V
         4: return
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lmypackage/EmptyClass;
      LineNumberTable:
        line 5: 0
}
SourceFile: "EmptyClass.scala"
RuntimeVisibleAnnotations:
  0: #6(#7=s#8)
Error: unknown attribute
  ScalaInlineInfo: length = 0x9
   01 00 00 01 00 09 00 0A 00
Error: unknown attribute
  ScalaSig: length = 0x3
   05 00 00
Rich
  • 15,048
  • 2
  • 66
  • 119
  • Thanks for the answer. I am looking for the size on heap when an instance of the class is created. – Dan.D Jul 19 '17 at 08:11
  • Ah I see, that is a more interesting question. The correct term would be "object" for an instance of a class. – Rich Jul 19 '17 at 08:29
1

If you mean the size of an empty Scala object in the JVM heap, then the answer is the same as for Java.

Scala doesn't add any extra fields to its classes compared to Java (see the javap output in my other answer for evidence of this).

This is answered in detail at What is the memory consumption of an object in Java?

Under normal circumstances, assuming a 64 bit JVM, you might expect an empty object to consume 16 bytes.

Rich
  • 15,048
  • 2
  • 66
  • 119