忽然遇到报错:ERROR spark.SparkContext: Error initializing SparkContext.


java.lang.IllegalArgumentException: System memory 100663296 must be at least 4.718592E8. Please use a larger heap size.

在Eclipse里开发Spark项目,尝试直接在spark里运行程序的时候,遇到下面这个报错:


很明显,这是JVM申请的memory不够导致无法启动SparkContext。但是该怎么设呢?

但是检查了一下启动脚本

#!/bin/bash
/usr/local/spark-1.6.0/bin/spark-submit  \
--class cn.spark.study.Opt17_WordCount \
--num-executors 3 \
--driver-memory 100m \ 
--executor-memory 100m \
--executor-cores 3 \
/root/sparkstudy/Java/spark-study-java-0.0.1-SNAPSHOT-jar-with-dependencies.jar \
--master  spark://yun01:7077



看来是driver内存不足,当给了 driver的内存尝试着增大到400M 时候

仍旧是爆出如下错

Exception in thread "main" java.lang.IllegalArgumentException: System memory 402128896 must be at least 4.718592E8. Please use a larger heap size.

此时就可以再次调大一些 给了1g(应该是从spark升级1.5或者1.6之后才出现这样的问题,)

然后再次运行之后正常得出结果

还可以指定在代码中 :

 val conf = new SparkConf().setAppName("word count")
 conf.set("spark.testing.memory", "1g")//后面的值大于512m即可

[java]  view plain  copy
 
  1. /** 
  2.    * Return the total amount of memory shared between execution and storage, in bytes. 
  3.    */  
  4.   private def getMaxMemory(conf: SparkConf): Long = {  
  5.     val systemMemory = conf.getLong("spark.testing.memory", Runtime.getRuntime.maxMemory)  
  6.     val reservedMemory = conf.getLong("spark.testing.reservedMemory",  
  7.       if (conf.contains("spark.testing")) 0 else RESERVED_SYSTEM_MEMORY_BYTES)  
  8.     val minSystemMemory = reservedMemory * 1.5  
  9.     if (systemMemory < minSystemMemory) {  
  10.       throw new IllegalArgumentException(s"System memory $systemMemory must " +  
  11.         s"be at least $minSystemMemory. Please use a larger heap size.")  
  12.     }  
  13.     val usableMemory = systemMemory - reservedMemory  
  14.     val memoryFraction = conf.getDouble("spark.memory.fraction"0.75)  
  15.     (usableMemory * memoryFraction).toLong  
  16.   }  

所以,这里主要是val systemMemory = conf.getLong("spark.testing.memory", Runtime.getRuntime.maxMemory)。

conf.getLong()的定义和解释是

[java]  view plain  copy
 
  1. getLong(key: String, defaultValue: Long): Long  
  2. Get a parameter as a long, falling back to a default if not set  

 


你可能感兴趣的:(spark)