1

I am attempting to download over 100mb video mp4 file by using below code. But i am getting "java.lang.OutOfMemoryError: Java heap space" while downloading.

Using jdk1.7 & Spring MVC.

I have refered below SO links but none works for me.

how to download large files without memory issues in java

java.lang.OutOfMemoryError: Java heap space while downloading a large file from an URL

Please help me to resolve this.

OutputStream outStream = null;
FileInputStream inputStream = null;
try {
    ServletContext context =  request.getSession().getServletContext();     
    String fileName = Integer.toString(fileId);     
    filePath = fileName+".mp4";

    File downloadFile = new File(filePath);
    inputStream = new FileInputStream(new File(filePath));
    // get MIME type of the file
    String mimeType = context.getMimeType(filePath);
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }
    response.setContentType(mimeType);
    response.setContentLength((int) downloadFile.length());
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"",downloadFile.getName());
    response.setHeader(headerKey, headerValue);
    outStream = response.getOutputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = 0, bytesBuffered = 0;
    while((bytesRead = inputStream.read(buffer)) > -1) {
        outStream.write(buffer, 0, bytesRead);
        bytesBuffered += bytesRead;
        if (bytesBuffered > 1024 * 1024) { //flush after 1MB
            bytesBuffered = 0;
            outStream.flush();
        }
    }
} catch(Exception e) {
    e.printStackTrace();
} finally {
    if (inputStream != null) {
        inputStream.close();
    }
    if (outStream != null) {
        outStream.flush();
        outStream.close();
    }
}
6
  • What's your -Xmx value? Commented Nov 30, 2017 at 15:06
  • @Stefan.. I am running on eclipse. from eclipse config file - -Xms40m -Xmx1024m. Commented Nov 30, 2017 at 15:10
  • which version of Java you are using? If you are using Java 8+, trying fine tuning MetaSpace. Commented Nov 30, 2017 at 15:27
  • Using Java 1.7.. Commented Nov 30, 2017 at 15:32
  • Can you include the stack trace? Is it the same every time, or does it vary? Commented Nov 30, 2017 at 16:47

2 Answers 2

2

I tried below code with 500 MB file and did not receive out of memory error

OutputStream outStream = response.getOutputStream();

String mimeType = "application/zip";
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
            "abc.zip");
response.setHeader(headerKey, headerValue);
response.setContentType(mimeType);
response.setStatus(HttpServletResponse.SC_OK);

File downloadFile = new File("file path");
InputStream inputStream = new FileInputStream(downloadFile);

int n = 0;
byte[] buf = new byte[1024];

while ((n = inputStream.read(buf)) != -1) {
        outStream.write(buf, 0, n);
}

inputStream.close();
outStream.flush();
outStream.close();

and JVM settings are:

-XX:PermSize=256m
-XX:MaxPermSize=512m
-Xms1024m
-Xmx2048m
-Dosgi.requiredJavaVersion=1.8
-XX:+UseG1GC
-XX:+UseStringDeduplication
Sign up to request clarification or add additional context in comments.

3 Comments

Amit.. You can able to upload video files.. These files will store java memory?
But I think your qs was related to download, so I wrote code per download. If upload needs to be done then need to think of different solution, and where i needs to be uploaded, also can we upload it in chunk?
Amit. Client is downloading a video in browser from server. I think my ram memory is low thats why this problem happen.
1

You need to call HttpServletResponse.flushBuffer(), not just OutputStream.flush(), and rather more frequently.

1 Comment

outStream.flush(); response.flushBuffer(); I have added this line. But still error happens

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.