SizeEstimator

  1. import org.apache.spark.util.SizeEstimator
  2. abstract class People {
  3. def name: String
  4. }
  5. class Student extends People {
  6. override val name: String = "lilei"
  7. }
  8. class Teacher extends People {
  9. override def name: String = "lili"
  10. }
  11. object Test {
  12. def main(args: Array[String]): Unit = {
  13. val student = SizeEstimator.estimate(new Student())
  14. println(student) //72
  15. val teacher = SizeEstimator.estimate(new Teacher())
  16. println(teacher) //16
  17. }
  18. }

出处

  1. //org.apache.spark.util.collection.SizeTracker#takeSample
  2. /**
  3. * Take a new sample of the current collection's size.
  4. */
  5. private def takeSample(): Unit = {
  6. samples.enqueue(Sample(SizeEstimator.estimate(this), numUpdates))
  7. // Only use the last two samples to extrapolate
  8. if (samples.size > 2) {
  9. samples.dequeue()
  10. }
  11. val bytesDelta = samples.toList.reverse match {
  12. case latest :: previous :: tail =>
  13. (latest.size - previous.size).toDouble / (latest.numUpdates - previous.numUpdates)
  14. // If fewer than 2 samples, assume no change
  15. case _ => 0
  16. }
  17. bytesPerUpdate = math.max(0, bytesDelta)
  18. nextSampleNum = math.ceil(numUpdates * SAMPLE_GROWTH_RATE).toLong
  19. }