Reputation: 321
Is there a way to find out the memory/size occupied by an object in scala? Thanks in advance for replying me.
Upvotes: 10
Views: 11806
Reputation: 534
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)
Upvotes: 3
Reputation: 15455
Scala runs on the JVM, the runtime system which backs Java. A Scala object is compiled into a JVM object at runtime, and is indistinguishable from a Java object at runtime. See for example the "Scala in a Nutshell" intro at https://www.scala-lang.org/ :
SEAMLESS JAVA INTEROP
Scala runs on the JVM, so Java and Scala stacks can be freely mixed for totally seamless integration.
...
Scala classes are ultimately JVM classes. You can create Java objects, call their methods and inherit from Java classes transparently from Scala. Similarly, Java code can reference Scala classes and objects.
So for the purposes of this question, any Java answers are equally applicable to Scala.
Please refer to
(Unless you are referring to non-standard Scala runtimes like Scala.js? In that case, you should look for answers that apply to the underlying runtime, like How to get the size of a JavaScript object? )
Upvotes: 3