如何避免java.lang.OutOfMemoryError? [英] How to avoid java.lang.OutOfMemoryError?

查看:118
本文介绍了如何避免java.lang.OutOfMemoryError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个简单的java代码。第一个定义恒定功率为power = a.pow(b);

I have two simple java codes.The first one defines constant power as power = a.pow(b);

import java.math.BigInteger;    
public class FermatOne    
{    
    public static void main(String[] args)    
    {    
         BigInteger a = new BigInteger ("2");    
         BigInteger k = new BigInteger ("15");    
         BigInteger c = new BigInteger ("1");    
         int b = 332192810;    
         BigInteger n = new BigInteger ("2");    
         BigInteger power;    
         power = a.pow(b);    
         BigInteger exponent;    
         exponent = k.multiply(power);    
         BigInteger mod;    
         mod = exponent.add(c);    
         BigInteger result = n.modPow(exponent,mod);    
         System.out.println("Result is  ==> " + result);    
     }    
}

第二个定义恒定功率为power = BigInteger。 ONE.shiftLeft(b)

The second one defines constant power as power = BigInteger.ONE.shiftLeft(b)

import java.math.BigInteger;    
public class FermatOne    
{    
    public static void main(String[] args)    
    {    

         BigInteger k = new BigInteger ("15");    
         BigInteger c = new BigInteger ("1");    
         int b = 332192810;    
         BigInteger n = new BigInteger ("2");    
         BigInteger power;    
         power = BigInteger.ONE.shiftLeft(b);    
         BigInteger exponent;    
         exponent = k.multiply(power);    
         BigInteger mod;    
         mod = exponent.add(c);    
         BigInteger result = n.modPow(exponent,mod);    
         System.out.println("Result is  ==> " + result);    
     }    
}

在命令行中设置内存标志-Xmx1024m第一个代码工作正常,但对于第二个代码我收到错误:java.lang.OutOfMemoryError:Java堆空间

Setting the memory flag -Xmx1024m in the command line the first code works fine , but for the second code I am getting error : java.lang.OutOfMemoryError :Java heap space

我的问题:我应该在第二个代码中更改什么避免java.lang.OutOfMemoryError?

My question : What should I change in the second code to avoid java.lang.OutOfMemoryError ?

推荐答案

你试图计算一个像这样的数字^ ^ (15 * 2 ^ 332192809)。我不知道你是否能在宇宙中适应这样的数字!!或者,答案很简单...... 42 ? ; - )

You're trying to calculate a number like 2 ^ (15 * 2 ^ 332192809). I don't know if you could even fit such a number in the universe!! Or maybe, the answer is simply... 42 ? ;-)

更严重的是,你真的遇到麻烦,计算这个数字。以位编码, 15 * 2 ^ 332192810 本身需要几乎一千兆字节。然后再次将 2 提升到那个权力,我不想知道......

On a more serious note, you'll really have trouble, calculating this number. Encoded in bits, 15 * 2 ^ 332192810 would require almost a gigabyte by itself. Then raising 2 to that power again, I don't want to know...

更严重的是,当你深入研究 java.math.BigInteger 的实现时,我认为你只是遇到了这样的错误更快左移,因为它比电力方法更有效地实施。话虽如此,您是否尝试使用 System.gc()强制进行代码中的垃圾收集?

On a more serious note, when you dig into the implementation of java.math.BigInteger, I think that you just run into such an error faster with the left shift, as that is implemented much more efficiently, than the power method. Having said this, have you tried to force garbage collection in your code, using System.gc()?

更新:我原来的推理可能是错的。 2 ^ 332192809 可以用1GB计算。整个结果可能会被 java.math.BigInteger 有效地修改,虽然我相信这个计算可能需要一段时间......

UPDATE: My original reasoning might've been wrong. 2 ^ 332192809 can be calculated with 1GB. And the overall result might be "modded" efficiently by java.math.BigInteger, although I believe that this calculation might take a while...

这篇关于如何避免java.lang.OutOfMemoryError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆