在2GB的zip文件上生成chceksum时,退出OfmemoryError:Java堆空间

时间:2019-09-09 14:41:11

标签: java out-of-memory

我有一个代码来生成校验和,当我尝试在2GB的zip文件上生成校验和时,我遇到了内存不足错误:Java堆空间

2019-09-09:[TIME]10:31:53 ERROR Unhandled exception from message handler method
org.springframework.messaging.MessageHandlingException: Unexpected handler method invocation error; nested exception is java.lang.OutOfMemoryError: Java heap space
        at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:569)
        at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:503)
        at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:93)
        at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:505)
        at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:439)
        at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:144)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space

我在下面显示了校验和的代码-stacktrace指向行

md.update(Files.readAllBytes(Paths.get(sourceFile))); 

我尝试添加更多的堆空间并再次运行该应用程序,但对于大文件仍然遇到相同的错误。档案很小,就没有问题。

public static String getCheckSum(String sourceFile)
     {

  String checksum = null;
   try
   {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(Files.readAllBytes(Paths.get(sourceFile)));
    byte[] digest = md.digest();
    checksum = DatatypeConverter
      .printHexBinary(digest).toUpperCase();
    logger.debug("checksum value " +checksum);
    logger.debug("file " +sourceFile);
   }
   catch (NoSuchAlgorithmException ex)
   {
       ex.printStackTrace();
       logger.error(ex.getMessage());
   } 
   catch (IOException e) 
   {
      e.printStackTrace();
     logger.error(e.getMessage());

   }
   catch (Exception exe)
   {
       exe.printStackTrace();
         logger.error(exe.getMessage()); 
   }
    return checksum;
}

0 个答案:

没有答案