how to download large files without memory issues in java

后端 未结 4 992
悲哀的现实
悲哀的现实 2020-12-04 18:55

When I am trying to download a large file which is of 260MB from server, I get this error: java.lang.OutOfMemoryError: Java heap space. I am sure my heap size i

相关标签:
4条回答
  • 2020-12-04 19:30

    There are 2 places where I can see you could potentially be building up memory usage:

    1. In the buffer reading your input file.
    2. In the buffer writing to your output stream (HTTPOutputStream?)

    For #1 I would suggest reading directly from the file via FileInputStream without the BufferedInputStream. Try this first and see if it resolves your issue. ie:

    FileInputStream in = new FileInputStream(file);   
    

    instead of:

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));   
    

    If #1 does not resolve the issue, you could try periodically flushing the output stream after so much data is written (decrease chunk size if necessary):

    ie:

    try
    {
        FileInputStream fileInputStream  = new FileInputStream(file);
        byte[] buf=new byte[8192];
        int bytesread = 0, bytesBuffered = 0;
        while( (bytesread = fileInputStream.read( buf )) > -1 ) {
            out.write( buf, 0, bytesread );
            bytesBuffered += bytesread;
            if (bytesBuffered > 1024 * 1024) { //flush after 1MB
                bytesBuffered = 0;
                out.flush();
            }
        }
    }
    finally {
        if (out != null) {
            out.flush();
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:31

    First you can remove the (in != null) from your while statement, it's unnecessary. Second, try removing the BufferedInputStream and just do:

    FileInputStream in = new FileInputStream(file);
    
    0 讨论(0)
  • 2020-12-04 19:34

    Unfortunately you have not mentioned what type out is. If you have memory issues I guess it is ByteArrayOutpoutStream. So, replace it by FileOutputStream and write the byte you are downloading directly to file.

    BTW, do not use read() method that reads byte-by-byte. Use read(byte[] arr) instead. This is much faster.

    0 讨论(0)
  • 2020-12-04 19:35

    There's nothing wrong (in regard to memory usage) with the code you're show. Either the servlet container is configured to buffer the entire response (look at the web.xml configuration), or the memory is being leaked elsewhere.

    0 讨论(0)
提交回复
热议问题