Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 45509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:49:14+00:00 2026-05-10T15:49:14+00:00

I am building a java server that needs to scale. One of the servlets

  • 0

I am building a java server that needs to scale. One of the servlets will be serving images stored in Amazon S3.

Recently under load, I ran out of memory in my VM and it was after I added the code to serve the images so I’m pretty sure that streaming larger servlet responses is causing my troubles.

My question is : is there any best practice in how to code a java servlet to stream a large (>200k) response back to a browser when read from a database or other cloud storage?

I’ve considered writing the file to a local temp drive and then spawning another thread to handle the streaming so that the tomcat servlet thread can be re-used. This seems like it would be io heavy.

Any thoughts would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:49:14+00:00Added an answer on May 10, 2026 at 3:49 pm

    When possible, you should not store the entire contents of a file to be served in memory. Instead, aquire an InputStream for the data, and copy the data to the Servlet OutputStream in pieces. For example:

    ServletOutputStream out = response.getOutputStream(); InputStream in = [ code to get source input stream ]; String mimeType = [ code to get mimetype of data to be served ]; byte[] bytes = new byte[FILEBUFFERSIZE]; int bytesRead;  response.setContentType(mimeType);  while ((bytesRead = in.read(bytes)) != -1) {     out.write(bytes, 0, bytesRead); }  // do the following in a finally block: in.close(); out.close(); 

    I do agree with toby, you should instead ‘point them to the S3 url.’

    As for the OOM exception, are you sure it has to do with serving the image data? Let’s say your JVM has 256MB of ‘extra’ memory to use for serving image data. With Google’s help, ‘256MB / 200KB’ = 1310. For 2GB ‘extra’ memory (these days a very reasonable amount) over 10,000 simultaneous clients could be supported. Even so, 1300 simultaneous clients is a pretty large number. Is this the type of load you experienced? If not, you may need to look elsewhere for the cause of the OOM exception.

    Edit – Regarding:

    In this use case the images can contain sensitive data…

    When I read through the S3 documentation a few weeks ago, I noticed that you can generate time-expiring keys that can be attached to S3 URLs. So, you would not have to open up the files on S3 to the public. My understanding of the technique is:

    1. Initial HTML page has download links to your webapp
    2. User clicks on a download link
    3. Your webapp generates an S3 URL that includes a key that expires in, lets say, 5 minutes.
    4. Send an HTTP redirect to the client with the URL from step 3.
    5. The user downloads the file from S3. This works even if the download takes more than 5 minutes – once a download starts it can continue through completion.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There is a project to help with migrations that I've… May 11, 2026 at 9:55 am
  • added an answer Probably grossly inefficiënt but it seems to work. import java.util.regex.*;… May 11, 2026 at 9:55 am
  • added an answer Looking at my NetBeans-generated build file, I found this snippet… May 11, 2026 at 9:55 am

Related Questions

I am building a java server that needs to scale. One of the servlets
I am looking into building a smallish Java rpg. I haven't found any good
I am looking for an open source library in Java for parsing and building
I am building a quiz and i need to calculate the total time taken
I am building a site that uses a simple AJAX Servlet to talk JMS
I am building a custom win32 control/widget and would like to change the cursor
I am building a project involving natural language processing, since the nlp module currently
I am building a C#/ASP.NET app with an SQL backend. I am on deadline
I am building a blog type page, and I want to load items into
I am building a physics simulation engine and editor in Windows. I want to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.