How to stay within the bounds of memory

时间:2015-07-28 15:46:24

标签: multithreading scala out-of-memory

I am trying to run a program to calculate the missing 4th value in an equation and I keep running out of memory. Is there a way to stay in memory, or do I need to format my code differently?

import scala.util.control.Breaks._

val p1=1<<56
val p2=1<<52 
val p3=0<<32
(1L to 100000000).par.foreach( (x: Long) =>{
    if(((p1|p2)|(p3|x)).toLong==76561198036298569L)
    {
        println("FOUND: "+x)
        break
    }
})

1 个答案:

答案 0 :(得分:0)

You have many options (and similar ones have already been suggested); you don't have to necessarily store an integer number in an integer object; you could rather store them as sequence of bits or even in a container e.g. a List (slow due to the linked list nature of scala lists, I would suggest BitSet or HashSet (for the constant and eC time complexity).

TLDR; you can represent unimaginably large numbers by just storing them differently for the cost of execution speed without changing the code much.