spark学习2之OutOfMemoryError错误的解决办法

时间:2021-11-07 14:48:14

更多代码请见:https://github.com/xubo245/SparkLearning

spark之OutOfMemoryError错误的解决办法:

xubo@xubo:~/cloud/spark-1.4.1$ spark-submit --master local examples/src/main/python/pi.py 1000
Traceback (most recent call last):
File "/home/xubo/cloud/spark-1.4.1/examples/src/main/python/pi.py", line 39, in <module>
count = sc.parallelize(range(1, n + 1), partitions).map(f).reduce(add)
File "/home/xubo/cloud/spark-1.4.1/python/lib/pyspark.zip/pyspark/context.py", line 396, in parallelize
File "/home/xubo/cloud/spark-1.4.1/python/lib/py4j-0.8.2.1-src.zip/py4j/java_gateway.py", line 538, in __call__
File "/home/xubo/cloud/spark-1.4.1/python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py", line 300, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.readRDDFromFile.
: java.lang.OutOfMemoryError: Java heap space
at org.apache.spark.api.python.PythonRDD$.readRDDFromFile(PythonRDD.scala:389)
at org.apache.spark.api.python.PythonRDD.readRDDFromFile(PythonRDD.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:379)
at py4j.Gateway.invoke(Gateway.java:259)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:207)
at java.lang.Thread.run(Thread.java:745)



Killed

解决办法:
在spark-env.sh中加入:

xubo@xubo:~/cloud/spark-1.4.1/conf$ vi spark-env.sh
export SPARK_DRIVER_MEMORY=1000M

在运行就没问题了:

xubo@xubo:~/cloud/spark-1.4.1$ spark-submit --master local examples/src/main/python/pi.py 1000
Pi is roughly 3.141683

单节点运行这个感觉好慢。