title: OutOfMemoryError in Scala and Spark
body: <p>I used this code for extracting bigrams from a text file:</p>
  
  <pre><code>import org.apache.spark.{SparkContext, SparkConf}
  object DS_E6 {
  
    def main(args: Array[String]): Unit = {
      case class Bigram(first: String, second: String) {
        def mkReplacement(s: String) = s.replaceAll(first + " " + second, first + "-" + second)
      }
  
      def stringToBigrams(s: String) = {
        val words = s.split(" ")
        if (words.size &gt;= 2) {
          words.sliding(2).map(a =&gt; Bigram(a(0), a(1)))
        } else
          Iterator[Bigram]()
      }
  
      val conf = new SparkConf()
        .setMaster("local")
        .setAppName("bigram")
        .set("spark.executor.memory", "1g")
  
      val sc = new SparkContext(conf)
        val data = sc.textFile("data/file.txt")
        val bigrams = data.flatMap {
          stringToBigrams
        }.collect()
  
        val bigramCounts = bigrams.groupBy(identity).mapValues(_.size)
        val threshold = 100
        val topBigrams = bigramCounts.filter(_._2 &gt;= threshold).map(_._1)
        topBigrams.foreach(println)
        val replaced = data.map(r =&gt; topBigrams.foldLeft(r)((r, b) =&gt; b.mkReplacement(r)))
        val replaced1 = replaced.zipWithIndex()
          .map { case (line, i) =&gt; i.toString + "," + line}
  
        replaced1.coalesce(1).saveAsTextFile("data/output.txt")
      }
    }
  }
  </code></pre>
  
  <p>my input file is 45 MB ,
  when i run this code it shows me below error:
  (i think it is related to Collect())</p>
  
  <pre><code> java.lang.OutOfMemoryError: GC overhead limit exceeded
  
  at org.spark-project.jetty.util.BlockingArrayQueue.poll(BlockingArrayQueue.java:342)
  at org.spark-project.jetty.util.thread.QueuedThreadPool.idleJobPoll(QueuedThreadPool.java:526)
  at org.spark-project.jetty.util.thread.QueuedThreadPool.access$600(QueuedThreadPool.java:44)
  at org.spark-project.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
  </code></pre>
  
  <p>how can i solve this problem?</p>