Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the size of an object in scala?

Is there a way to find out the memory/size occupied by an object in scala? Thanks in advance for replying me.

like image 472
Pratap D Avatar asked Jan 02 '18 16:01

Pratap D


People also ask

How do you find the size of an object in Java?

One way to get an estimate of an object's size in Java is to use getObjectSize(Object) method of the Instrumentation interface introduced in Java 5. As we could see in Javadoc documentation, the method provides “implementation-specific approximation” of the specified object's size.

Why there is no sizeof operator in Java?

Fallacy: Sizeof() is not needed because Java basic types' sizes are fixed. Yes, a Java int is 32 bits in all JVMs and on all platforms, but this is only a language specification requirement for the programmer-perceivable width of this data type.

What is the size of a class in Java?

Java Class Any class in Java is nothing but an object with a mix of all mentioned components: header (8 or 12 bytes for 32/64 bit os). primitive (type bytes depending on the primitive type). object/class/array (4 bytes reference size).


1 Answers

I also found a memory consumption estimator for Scala/Java. This library is ported from Spark project. More info here and here.

Add in sbt:

libraryDependencies += "com.madhukaraphatak" %% "java-sizeof" % "0.1"

For any object, estimate object size by calling:

SizeEstimator.estimate(obj)
like image 124
Valentina Avatar answered Sep 24 '22 14:09

Valentina