Is there a way to find out the memory/size occupied by an object in scala? Thanks in advance for replying me.
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.
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.
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With