What is the size of an empty class in scala?
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 am getting is 1368 (bytes) which looks much higher than I expected.
Do you mean file size .class
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 (file size depends on the package name, since the file contains the class and package name).
The tool javap
shows what data is 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
If you mean the size of an empty Scala object on 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 output javap
in my other answer to confirm this).
This is detailed in What is the memory consumption of an object in Java?
Under normal circumstances, assuming a 64-bit JVM, you can expect an empty object to consume 16 bytes.