0

I keep getting the following error when I run my program:

java.lang.OutOfMemoryError : Java heap space

I tried fixing this by adding -Xms512M -Xmx1524M to both Program arguments and VM arguments (I use eclipse), but this doesn't seem to prevent the error.

If the solution is adding more memory in eclipse run configuration, the question is > is this going to be exported too? or is it just for me, how can I make sure this doesn't happen on other computers?

Current code

 private static void checkSumGen() throws IOException NoSuchAlgorithmException
    {

    File plFolder = new File(".\\Plugins");
    File[] listOfFiles = plFolder.listFiles();
    List<String> listClone = new ArrayList<String>();
    for (int i = 0; i < listOfFiles.length; i++)
        {
            File file = listOfFiles[i];
            String fileloc = file.getAbsolutePath();
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            FileInputStream fis = new FileInputStream(fileloc);

            byte[] dataBytes = new byte[1024];

            int nread = 0;
            while ((nread = fis.read(dataBytes)) != -1)
            {
                md.update(dataBytes, 0, nread);
            } ;
            byte[] mdbytes = md.digest();
                
            // convert the byte to hex format method 1
            StringBuffer sb = new StringBuffer();
            for (int i1 = 0; i1 < mdbytes.length; i++)
                {
                    sb.append(Integer.toString((mdbytes[i1] & 0xff) + 0x100, 16).substring(1)); 
                }

            System.out.println("Hex format : " + sb.toString());

            // convert the byte to hex format method 2
            StringBuffer hexString = new StringBuffer();
            for (int i2 = 0; i2 < mdbytes.length; i++)
                {
                    hexString.append(Integer.toHexString(0xFF & mdbytes[i2]));
                }
            System.out.println("Hex format : " + hexString.toString());
        }

}
4
  • Don't you want to read the input file not the data bytes you create? Commented Nov 15, 2017 at 20:39
  • @kpie please make the question more clear. Commented Nov 15, 2017 at 20:41
  • what's going on on the line that reads while ((nread = fis.read(dataBytes)) != -1) Commented Nov 15, 2017 at 20:43
  • @kpie See docs.oracle.com/javase/8/docs/api/java/io/… Commented Nov 15, 2017 at 21:13

1 Answer 1

1

You have an infinite loop in your code that causes the OutOfMemoryError.

In:

for (int i1 = 0; i1 < mdbytes.length; i++)

you increment i instead of i1 causing i1 < mdbytes.length to be true forever.

Sign up to request clarification or add additional context in comments.

1 Comment

Works, thanks. Also I want to point out that the same error was made on the last for loop as I used i++ instead of i2++ .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.