aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorYin Huai <yhuai@databricks.com>2015-11-18 00:42:52 -0800
committerReynold Xin <rxin@databricks.com>2015-11-18 00:42:52 -0800
commit1714350bddd78cd1398e1a816f675ab729001081 (patch)
treef567319f64994f8492c8d69c8aacea83d87b799a /core
parent5e2b44474c2b838bebeffe5ba5cd72961b0cd31e (diff)
downloadspark-1714350bddd78cd1398e1a816f675ab729001081.tar.gz
spark-1714350bddd78cd1398e1a816f675ab729001081.tar.bz2
spark-1714350bddd78cd1398e1a816f675ab729001081.zip
[SPARK-11792][SQL] SizeEstimator cannot provide a good size estimation of UnsafeHashedRelations
https://issues.apache.org/jira/browse/SPARK-11792 Right now, SizeEstimator will "think" a small UnsafeHashedRelation is several GBs. Author: Yin Huai <yhuai@databricks.com> Closes #9788 from yhuai/SPARK-11792.
Diffstat (limited to 'core')
-rw-r--r--core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java3
-rw-r--r--core/src/main/scala/org/apache/spark/util/SizeEstimator.scala26
-rw-r--r--core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala22
3 files changed, 47 insertions, 4 deletions
diff --git a/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java b/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
index 5f743b2885..d31eb449eb 100644
--- a/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
+++ b/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
@@ -215,6 +215,9 @@ public class TaskMemoryManager {
logger.info(
"{} bytes of memory were used by task {} but are not associated with specific consumers",
memoryNotAccountedFor, taskAttemptId);
+ logger.info(
+ "{} bytes of memory are used for execution and {} bytes of memory are used for storage",
+ memoryManager.executionMemoryUsed(), memoryManager.storageMemoryUsed());
}
}
diff --git a/core/src/main/scala/org/apache/spark/util/SizeEstimator.scala b/core/src/main/scala/org/apache/spark/util/SizeEstimator.scala
index 23ee4eff08..c3a2675ee5 100644
--- a/core/src/main/scala/org/apache/spark/util/SizeEstimator.scala
+++ b/core/src/main/scala/org/apache/spark/util/SizeEstimator.scala
@@ -32,6 +32,16 @@ import org.apache.spark.annotation.DeveloperApi
import org.apache.spark.util.collection.OpenHashSet
/**
+ * A trait that allows a class to give [[SizeEstimator]] more accurate size estimation.
+ * When a class extends it, [[SizeEstimator]] will query the `estimatedSize` first.
+ * If `estimatedSize` does not return [[None]], [[SizeEstimator]] will use the returned size
+ * as the size of the object. Otherwise, [[SizeEstimator]] will do the estimation work.
+ */
+private[spark] trait SizeEstimation {
+ def estimatedSize: Option[Long]
+}
+
+/**
* :: DeveloperApi ::
* Estimates the sizes of Java objects (number of bytes of memory they occupy), for use in
* memory-aware caches.
@@ -199,10 +209,18 @@ object SizeEstimator extends Logging {
// the size estimator since it references the whole REPL. Do nothing in this case. In
// general all ClassLoaders and Classes will be shared between objects anyway.
} else {
- val classInfo = getClassInfo(cls)
- state.size += alignSize(classInfo.shellSize)
- for (field <- classInfo.pointerFields) {
- state.enqueue(field.get(obj))
+ val estimatedSize = obj match {
+ case s: SizeEstimation => s.estimatedSize
+ case _ => None
+ }
+ if (estimatedSize.isDefined) {
+ state.size += estimatedSize.get
+ } else {
+ val classInfo = getClassInfo(cls)
+ state.size += alignSize(classInfo.shellSize)
+ for (field <- classInfo.pointerFields) {
+ state.enqueue(field.get(obj))
+ }
}
}
}
diff --git a/core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala b/core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala
index 20550178fb..9b6261af12 100644
--- a/core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala
+++ b/core/src/test/scala/org/apache/spark/util/SizeEstimatorSuite.scala
@@ -60,6 +60,18 @@ class DummyString(val arr: Array[Char]) {
@transient val hash32: Int = 0
}
+class DummyClass8 extends SizeEstimation {
+ val x: Int = 0
+
+ override def estimatedSize: Option[Long] = Some(2015)
+}
+
+class DummyClass9 extends SizeEstimation {
+ val x: Int = 0
+
+ override def estimatedSize: Option[Long] = None
+}
+
class SizeEstimatorSuite
extends SparkFunSuite
with BeforeAndAfterEach
@@ -214,4 +226,14 @@ class SizeEstimatorSuite
// Class should be 32 bytes on s390x if recognised as 64 bit platform
assertResult(32)(SizeEstimator.estimate(new DummyClass7))
}
+
+ test("SizeEstimation can provide the estimated size") {
+ // DummyClass8 provides its size estimation.
+ assertResult(2015)(SizeEstimator.estimate(new DummyClass8))
+ assertResult(20206)(SizeEstimator.estimate(Array.fill(10)(new DummyClass8)))
+
+ // DummyClass9 does not provide its size estimation.
+ assertResult(16)(SizeEstimator.estimate(new DummyClass9))
+ assertResult(216)(SizeEstimator.estimate(Array.fill(10)(new DummyClass9)))
+ }
}