<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: After uploading files we get an OutOfMemoryError: Java heap space</title><link>https://answers.axonivy.com/questions/1302/after-uploading-files-we-get-an-outofmemoryerror-java-heap-space</link><description>&lt;p&gt;We use a the p:fileUpload widget form primefaces with an upload listener like this:
&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;p:fileUpload fileUploadListener="#{logic.uploadFile}&lt;/code&gt;&lt;/pre&gt;
In the listener method we copy the file content to an byte array on a data class to store it later. The script looks like this:&lt;p&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;byte[] content = IOUtils.toByteArray(in.fileUploadEvent.getFile().getInputstream());
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But after uploading multiple files we get an OutOfMemoryError: Java heap space &lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1302/after-uploading-files-we-get-an-outofmemoryerror-java-heap-space" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 06 May 2015 09:54:14 -0400</lastBuildDate><item><title>Answer by Christian Strebel</title><link>https://answers.axonivy.com/questions/1302/after-uploading-files-we-get-an-outofmemoryerror-java-heap-space/1303</link><description>&lt;p&gt;If you store the whole content of a file into a byte array the whole content is held in the RAM.
If the byte array is stored to the Dialog data the data is held until the dialog is closed with the Exit End or the session is destroyed. Normally the session gets only destroyed if the session timeout expires.
So that means it is normally not a good idea to store file contents on byte arrays (in RAM). This will lead later or sooner to memory problems.
You should store the file again as file on the server. As example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FileUploadEvent fileUploadEvent = ... // get it from param
String fileName = fileUploadEvent.getFile().getFileName();
InputStream inputStream = fileUploadEvent.getFile().getInputstream();
// This creates a temporary session file which will be removed if the session terminates
File ivyfile = new ch.ivyteam.ivy.scripting.objects.File(fileName, true);
FileOutputStream fileOutputStream = new FileOutputStream(ivyfile.getJavaFile());
IOUtils.copy(inputStream, fileOutputStream);
    IOUtils.copy(inputStream, fileOutputStream);
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Christian Strebel</dc:creator><pubDate>Wed, 06 May 2015 09:54:14 -0400</pubDate><guid>https://answers.axonivy.com/questions/1302/after-uploading-files-we-get-an-outofmemoryerror-java-heap-space/1303</guid></item></channel></rss>