gpt4 book ai didi

java.lang.OutOfMemoryError : Java heap space to find all strings of length n 错误

转载 作者:行者123 更新时间:2023-12-04 20:50:58 25 4
gpt4 key购买 nike

我想用字母 a、b 和 c 生成所有可能的长度为 n 的字符串。我收到此错误 java.lang.OutOfMemoryError:Java 堆空间。我的堆大小是 512m 。你能给我建议替代品吗..

最佳答案

你当前正在计算的字符串个数是

3^30 =                205 891 132 094 649

这是相当多的...

知道每个字符串包含三个字节:

3^30 * 3 =            617 673 396 283 947

加上二维数组的32位或64位指针。

3^30 * (3 + 4) =    1 441 237 924 662 540  // 32-bit Java VM
3^30 * (3 + 8) = 2 264 802 453 041 140 // 64-bit Java VM

这是

2 109 261 GB = 2059 TB     // 64-bit JVM

我想这就是问题所在。


在 500 MB 的限制下,您可以求解这个等式:

  3^x * (3 + 8) = 524 288 000
3^x = 47662545
x = log(47662545) / log(3)
x = 7 / 0.477121254719662
x = 14.67

所以,如果我什么都没忘记,你的测试应该适用于 n <= 14 .当然,除非您删除这段代码,否则它不会起作用:

List<String> result = new ArrayList<String>();
for (char[] permutation : table) {
result.add(new String(permutation));
}

这段代码复制了所有的数据!这意味着您需要两倍的内存。尝试立即打印它。

关于java.lang.OutOfMemoryError : Java heap space to find all strings of length n 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8579575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com