+ Start a Discussion
New_DeveloperNew_Developer 

Apex heap size too large:

Hi,

I have a batch class which runs and attached like hundreds of text files to the Document object and i have a vf page and controller to get all the data in that documents into one big text file. The size of the file would be around 9MB. But iam getting the Apex heap sze too large error. Is it possible to generate a 9 MB text file from a vf page??

Here is my code

<apex:page standardController="Child__c" extensions="DocumentController" contentType="text/plain/#testfile" cache="false" >
<apex:repeat value="{!lstStringOutput}" var="str">
{!str}
</apex:repeat>
</apex:page>

Conrtoller:
public with sharing class DocumentController {
public List<String> lstStringOutput {get;set;}
Public Static String text;
    public DocumentController(ApexPages.StandardController controller) {

         lstStringOutput = new List<String>();
          Folder folder = [select id from Folder where name='Test file' LIMIT 1];
        
        for(Document doc : [Select id, Description , Body , folderId from Document where folderId = :folder.id ]){
          text= doc.Body.toString();
             lstStringOutput.add(text+'\r');
           } 
}
}
Thanks

ShashForceShashForce
Hi,

Heap size limit is 6MB for synchronous requests like displaying on a visualforce page. You might want to consider using the "transient" keyword to reduce heap size. Something like:

public with sharing class DocumentController {
public transient List<String> lstStringOutput {get;set;}
Public transient Static String text;
    public DocumentController(ApexPages.StandardController controller) {

         lstStringOutput = new List<String>();
          Folder folder = [select id from Folder where name='Test file' LIMIT 1];
        
        for(Document doc : [Select id, Description , Body , folderId from Document where folderId = :folder.id ]){
          text= doc.Body.toString();
             lstStringOutput.add(text+'\r');
           } 
}
}

For more suggestions, please see these:
http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/
http://www.interactiveties.com/b_visualforce_heap_size.php#.U3w9NfmSyt0
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_transient.htm

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
New_DeveloperNew_Developer
Hi Shashank,

Thanks for the reply. The above code did not work. Its just giving me the same error. Is there any way we can attach a file with size of 10 MB in salesforce. The files with size of 2GB ca be attached to files tab , but  is there a way to attach the file to teh files tab using apex??

Actually i have a batch Apex class which generates the string and iam trying to attach that text file which is the output of that batch class to documents object as a attachment. Since the file size is 10 MB , i could not attach it . So i made the code in batch apex to attach the chunks of documents in the documents object and wrote the above Vf page and controller to generate one big document. So if there is a way to attach 10 MB document in salesforce , then i would not need the VF page and controller .

Thanks