Skip to content

Commit 4222da6

Browse files
sryzasrowen
authored andcommitted
[SPARK-5112] Expose SizeEstimator as a developer api
"The best way to size the amount of memory consumption your dataset will require is to create an RDD, put it into cache, and look at the SparkContext logs on your driver program. The logs will tell you how much memory each partition is consuming, which you can aggregate to get the total size of the RDD." -the Tuning Spark page This is a pain. It would be much nicer to expose simply functionality for understanding the memory footprint of a Java object. Author: Sandy Ryza <[email protected]> Closes #3913 from sryza/sandy-spark-5112 and squashes the following commits: 8d9e082 [Sandy Ryza] Add SizeEstimator in org.apache.spark 2e1a906 [Sandy Ryza] Revert "Move SizeEstimator out of util" 93f4cd0 [Sandy Ryza] Move SizeEstimator out of util e21c1f4 [Sandy Ryza] Remove unused import 798ab88 [Sandy Ryza] Update documentation and add to SparkContext 34c523c [Sandy Ryza] SPARK-5112. Expose SizeEstimator as a developer api
1 parent fc8feaa commit 4222da6

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark
19+
20+
import org.apache.spark.annotation.DeveloperApi
21+
22+
/**
23+
* Estimates the sizes of Java objects (number of bytes of memory they occupy), for use in
24+
* memory-aware caches.
25+
*
26+
* Based on the following JavaWorld article:
27+
* http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html
28+
*/
29+
@DeveloperApi
30+
object SizeEstimator {
31+
/**
32+
* :: DeveloperApi ::
33+
* Estimate the number of bytes that the given object takes up on the JVM heap. The estimate
34+
* includes space taken up by objects referenced by the given object, their references, and so on
35+
* and so forth.
36+
*
37+
* This is useful for determining the amount of heap space a broadcast variable will occupy on
38+
* each executor or the amount of space each object will take when caching objects in
39+
* deserialized form. This is not the same as the serialized size of the object, which will
40+
* typically be much smaller.
41+
*/
42+
@DeveloperApi
43+
def estimate(obj: AnyRef): Long = org.apache.spark.util.SizeEstimator.estimate(obj)
44+
}

docs/tuning.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ We will then cover tuning Spark's cache size and the Java garbage collector.
9494

9595
## Determining Memory Consumption
9696

97-
The best way to size the amount of memory consumption your dataset will require is to create an RDD, put it into cache, and look at the SparkContext logs on your driver program. The logs will tell you how much memory each partition is consuming, which you can aggregate to get the total size of the RDD. You will see messages like this:
97+
The best way to size the amount of memory consumption a dataset will require is to create an RDD, put it
98+
into cache, and look at the "Storage" page in the web UI. The page will tell you how much memory the RDD
99+
is occupying.
98100

99-
INFO BlockManagerMasterActor: Added rdd_0_1 in memory on mbk.local:50311 (size: 717.5 KB, free: 332.3 MB)
100-
101-
This means that partition 1 of RDD 0 consumed 717.5 KB.
101+
To estimate the memory consumption of a particular object, use `SizeEstimator`'s `estimate` method
102+
This is useful for experimenting with different data layouts to trim memory usage, as well as
103+
determining the amount of space a broadcast variable will occupy on each executor heap.
102104

103105
## Tuning Data Structures
104106

0 commit comments

Comments
 (0)