Reputation: 7632
So I'm not really sure what actually happens deep-down.
Let's say we are in a web application and user request to download a dynamically generated file which can be several mb in size, possibly even 100 mb or more. The application does this
String disposition = "attachment; fileName="myFile.txt";
response.setHeader("Content-Disposition", disposition);
ServletOutputStream output = response.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
service.exportFile(ids, writer, properties);
Am I right that the whole file is never completely in memory? eg. whatever data was generated is sent to the user and then discarded on the server (assuming everything went well, no packet loss)?
I ask this because I need to change the library that generates the files (3rd party) and the new one doesn't not use standard Java IO stuff probably because it is just an API and the actual library is in C. Anyway to get a buffers data the documentation says to call
String data = buffer.toString();
(the files are ASCII)
So is my assumption correct that memory consumption will be affected especially when multiple users download large files at the same time?
Upvotes: 0
Views: 1963
Reputation: 7632
the 3rd party lib offers a second solution and that is writing to a file. however that requires extra hassle to create unique file names, then read them in and delete them afterwards. But I implemented it anyway.
In
service.exportFile(ids, writer, properties)
ids
is a collection of database identifiers of records to be exported and hence ids.size()
gives a rough estimate of the resulting files size. So as long as it is small I use the buffer version and if it is large the file one.
Upvotes: 0
Reputation: 11327
Yes in you're first code snippet data is streamed directly to client, assuming, that implementation of service.exportFile(ids, writer, properties)
itself never holds the generated data in memory but really streames it directly to writter.
With String data = buffer.toString();
you'll definitively place the entire data in heap space, at the latest when buffer.toString()
is called, may be earlier depending on exact implementation.
In conclusion, you have in my opinion to be aware of two things: - never alocate data to a variable in you're codem but diretly write it to output stream - insure that implmentation also never holds the whole data in memory whike generating it
Upvotes: 1