function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kanth kKanth k 

how to solve Apex heap size too large error?

Hi,

 I am  getting Apex heap size is too large error message when i am uploading large file (10MB Size and data size is 65000records) in visualforce page using batch class. but if file size is 5MB(26000 records)  data is inserted without any error.

Can any baody help me where i am make mistake in
<apex:inputFile value="{!csvfile}" fileName="{!fname}" id="fileinput"/> 
                <center><apex:commandButton value="Upload File" id="theButton" style="width:88px;" action="{!uploadfile}"/></center>
            </apex:pageBlock>

batch class code.
public class uploadFilebatch_testing
{
    PUBLIC transient  blob csvfile{get;set;}
    public String fname{get;set;}
public void uploadfile()
    {
         Email_Service_Logs__c servicestatus=new Email_Service_Logs__c();
        Attachment attch = new Attachment();
        if(csvfile!=null)
        {
           count= database.countQuery('select count() from Email_Service_Logs__c');
            count=count+1;
            system.debug('noof records'+count);
            
             //servicestatus.name=fname;
            servicestatus.Date__c =system.today();
            servicestatus.Subject__c   =fname+'_'+count;
            if(fname!=null)
            insert servicestatus;
            
            attch.name = fname;
            attch.body = csvfile;
            attch.parentID =servicestatus.Id; 
            if(fname!=null)
            insert attch;
            
            
            
            dataBatch_testing cuatch=new dataBatch_testing(attch.name,servicestatus.id);
            //cuatch.teoost(attch.name,,servicestatus.id);
            String strBatchId = Database.executeBatch(cuatch, 300);
            
        }
______________________

Batch class code:
global with sharing class dataBatch_testing  implements Database.batchable<String>
{
 private  String m_csvFile;
   private Integer m_startRow;
   global boolean headervaleus=true;
   public string filename;
   public id statusid;
   public dataBatch_testing (string filename,string emailstatusid)
   {
 
 this.filename=filename;
statusid=emailstatusid;
}
 global Iterable<String> start(Database.batchableContext batchableContext)
   { 
      transient string mcsvfile;
       for(Attachment att:[select id,name,body from Attachment where name=:filename and parentid=:statusid])
       {
           //m_csvFile=att.body.tostring();
           HttpRequest tmp = new HttpRequest();
           tmp.setBodyAsBlob(att.body);
           //m_csvFile= tmp.getBody();
           mcsvfile=tmp.getBody();

       }
       
       
       return new CSVIterator(mcsvfile, '\n');
       //return new CSVIterator(m_csvFile, parser.crlf);
   }
 global void execute(Database.BatchableContext batchableContext, List<String> scope)  
   {
       system.debug('inside execute method'+scope.size());
      
       String csvFile = '';
       for(String row : scope)
       {
           csvFile += row + parser.crlf;
    
       }
       UploadDta_test.InsertValues_test(batchableContext.getJobId(),csvFile,headervaleus,statusid);
       
       if(headervaleus==true)
           headervaleus=false;
     
   }
   global void finish(Database.BatchableContext batchableContext)
   {

}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Kanth,

Salesforce enforces an Apex Heap Size Limit of 6MB for synchronous transactions and 12MB for asynchronous transactions.

The "Apex heap size too large" error occurs when too much data is being stored in memory during processing. The limit depends on the type of execution (E.g. synchronous vs asynchronous calls)

for more information please go through the given links as it may help you in solving your problem.

-https://help.salesforce.com/articleView?id=000321537&language=en_US&type=1
-https://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/ 


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ravi Dutt SharmaRavi Dutt Sharma
Instead of storing the file body in apex variable, try using attachment object to hold the file. You can find more details in below post:

https://sagarsindhiblog.wordpress.com/2017/09/30/how-to-tackle-view-state-issue-during-upload-file-as-a-attachment-through-visualforce-page/