我正在研究一个项目问题,我遇到了OutOfMemoryError
我不明白为什么因为我的代码工作得很漂亮(至少我的新手眼睛:P)
一切正常,直到循环达到113383
如果有人可以帮助我调试这个,我将非常感激,因为我根本不明白它为什么会失败。
我的代码
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
/*
* The following iterative sequence is defined for the set of positive integers:
n = n/2 (n is even)
n = 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 40 20 10 5 16 8 4 2 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains
10 terms. Although it has not been proved yet (Collatz Problem), it is thought
that all starting numbers finish at 1. Which starting number, under one million,
produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
*/
public class Problem14 {
static Map<Integer, Integer> map = new HashMap<Integer, Integer>();
final static int NUMBER = 1000000;
public static void main(String[] args) {
long start = System.currentTimeMillis();
map.put(1, 1);
for (int loop = 2; loop < NUMBER; loop++) {
compute(loop);
//System.out.println(map);
System.out.println(loop);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + ((end - start) / 1000.) + " seconds" );
}
private static void compute(int currentNumber) {
Stack<Integer> temp = new Stack<Integer>();
/**
* checks to see if current value is already
* part of the map. if it isn't, add to temporary
* stack. also if current value exceeds 1 million,
* don't check map or add to stack.
*/
while (!map.containsKey(currentNumber)){
temp.add(currentNumber);
currentNumber = realCompute(currentNumber);
while (currentNumber > NUMBER){
currentNumber = realCompute(currentNumber);
}
}
System.out.println(temp);
/**
* adds members of the temporary stack to the map
* based on when they were placed in the stack
*/
int initial = map.get(currentNumber) + 1;
int size = temp.size();
for (int loop = 1; loop <= size; loop++){
map.put(temp.pop(), initial);
initial++;
//key is the top of stack
//value is initially 1 greater than the current number that was
//found at the map, then incremented by 1 afterwards;
}
}
private static int realCompute(int currentNumber) {
if (currentNumber % 2 == 0) {
currentNumber /= 2;
} else {
currentNumber = (currentNumber * 3) + 1;
}
return currentNumber;
}
}
答案 0 :(得分:2)
对我来说,这就是错误所说的内容:你的内存不足。使用-Xmx
(例如java -Xmx512m
)增加堆大小。
如果您想减少内存占用,请查看GNU Trove以获得更紧凑(和更快)的原始地图实现(而不是使用HashMap<Integer,Integer>
)。
答案 1 :(得分:1)
我遇到了和你一样的问题...... 问题实际上在于“堆栈温度”,将其更改为Long就可以了。 这是一个简单的溢出。
答案 2 :(得分:0)
它出现了哪些OutOfMemory错误?堆,PermGen ..?可能需要增加堆大小或permgen大小。
答案 3 :(得分:0)
你应该像下面这样定义HashMap:map = new HashMap(NUMBER)。
你知道,“Entry []表”是地图数据结构,自动容量。
初始容量的原因:1,不是阵列复制。 2,跑得更快。