SizeEstimator
import org.apache.spark.util.SizeEstimatorabstract class People { def name: String}class Student extends People { override val name: String = "lilei"}class Teacher extends People { override def name: String = "lili"}object Test { def main(args: Array[String]): Unit = { val student = SizeEstimator.estimate(new Student()) println(student) //72 val teacher = SizeEstimator.estimate(new Teacher()) println(teacher) //16 }}
出处
//org.apache.spark.util.collection.SizeTracker#takeSample /** * Take a new sample of the current collection's size. */ private def takeSample(): Unit = { samples.enqueue(Sample(SizeEstimator.estimate(this), numUpdates)) // Only use the last two samples to extrapolate if (samples.size > 2) { samples.dequeue() } val bytesDelta = samples.toList.reverse match { case latest :: previous :: tail => (latest.size - previous.size).toDouble / (latest.numUpdates - previous.numUpdates) // If fewer than 2 samples, assume no change case _ => 0 } bytesPerUpdate = math.max(0, bytesDelta) nextSampleNum = math.ceil(numUpdates * SAMPLE_GROWTH_RATE).toLong }