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
Varun ChopraVarun Chopra 

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 604.391KB

Hi I am crating a visualforce page showing some data with one lookup field. Page is loading in my developer account (having 100 records).  Even I have used te paginaion i.e. showing only 20 records per page. After creating package when I am installing package in my live accoun t(having 2000+  records) and trying to open that page but  it is giving the following error.
Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 604.391KB

I tried to check the view state in my developer account. Only 6.44 kb is used.  While live account does'nt showing the view state I dont know why ?
Please check my class and visual force page code below :

Class Code
public with sharing class PolicyWrapperClassController {
public List<wPolicy> policyList {get; set;}

//public String accountName {get; set;}
//public Id accountId {get; set;}

   private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 20;

public PolicyWrapperClassController()
{
totalRecs = [select count() from CanaryAMS__Policy__c where CanaryAMS__To_be_Reviewed__c = true];
}
  
    public List<wPolicy> getPolicies() {
        if(policyList == null) {
             policyList = new List<wPolicy>();
           
             Map<Id, Contact> ContactsDetail = new Map<Id, Contact>();
             List<Contact> contacts = [ SELECT Id,Name from Contact];
          
              for(Contact c : contacts){
                  ContactsDetail.put(c.id, c);
              }
            
                Map< Id,String> recordTypeMap = RecordTypeMap();
                 List<CanaryAMS__Policy__c> oldpoliciesList = new List<CanaryAMS__Policy__c>();
            oldpoliciesList = [SELECT Id,CanaryAMS__Effective_Date__c,Name,CanaryAMS__Policy_Number__c,CanaryAMS__Carrier__r.Name,CanaryAMS__PolicyTypeCd__c,CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c,CanaryAMS__Carrier_Product__c from CanaryAMS__Policy__c where  CanaryAMS__To_be_Reviewed__c = false];  
             //system.debug('SELECT Id,Name,CanaryAMS__Policy_Number__c,CanaryAMS__Effective_Date__c,CanaryAMS__Carrier__r.Name,CanaryAMS__PolicyTypeCd__c,CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c,CanaryAMS__Carrier_Product__c from CanaryAMS__Policy__c where  CanaryAMS__To_be_Reviewed__c = true LIMIT'  LIMIT :LimitSize OFFSET :OffsetSize  +LimitSize+ 'OFFSET' +OffsetSize);
             for(CanaryAMS__Policy__c p: Database.Query('SELECT Id,Name,RecordTypeId,CanaryAMS__Policy_Number__c,CanaryAMS__Effective_Date__c,CanaryAMS__Carrier__r.Name,CanaryAMS__PolicyTypeCd__c,CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c,CanaryAMS__Carrier_Product__c from CanaryAMS__Policy__c where CanaryAMS__To_be_Reviewed__c = true LIMIT :LimitSize OFFSET :OffsetSize')) {
                // As each contact is processed we create a new wPolicy object and add it to the policyList
                //system.debug('test=========>>>>' + p);
                  wPolicy policyWrapper = new wPolicy(p);
                
               policyWrapper =  fetchSuggestedPolicies(p,oldpoliciesList,ContactsDetail,policyWrapper);
                  
                  if(policyWrapper.bestMatch != null)
                  {
                   policyWrapper.policyId = policyWrapper.bestMatch.Id;
                   policyWrapper.policyName = policyWrapper.bestMatch.CanaryAMS__Policy_Number__c;
                  }
           /*       
                  try {
                   Contact contact = ContactsDetail.get(p.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c);     
                   policyWrapper.con = contact;
                  }catch(DmlException e) {
               system.debug('info putinto ivansreulsts. test>>>'+p.Name);
            } catch(Exception e) {
                system.debug('info putinto ivansreulsts. test6>>>'+p.Name);
            }
            */
            try {
                   String name = recordTypeMap.get(p.RecordTypeId);     
                   policyWrapper.recordTypeName = name;
                   //policyWrapper.recordTypeId = p.RecordTypeId;
                  }catch(DmlException e) {
            } catch(Exception e) {
            }
           
                
                
                 policyList.add(policyWrapper);
            }
        }
        return policyList;
    }


    public PageReference processSelected() {
     system.debug('coming -----');
                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<wPolicy> selectedPolicies = new List<wPolicy>();
        for(wPolicy wPol: getPolicies()) {
            if(wPol.selected == true) {
             system.debug(wPol);
                selectedPolicies.add(wPol);
            }
          
         }
         system.debug('selectedPolicies -----'+selectedPolicies);
  
  if(!selectedPolicies.isEmpty())
        {
         fetchData(selectedPolicies);
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Policy successfully merged'));
     }
        else
        {
           ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Select atleast one Policy you want to merge'));
        }
 
        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
       /* System.debug('These are the selected Contacts...');
        for(CanaryAMS__Policy__c pol: selectedPolicies) {
            system.debug('selectedPolicies -----'+pol);
        }*/
        policyList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }
    

      public PageReference markAsResolved() {
       system.debug('coming here -----');
                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<wPolicy> selectedPolicies = new List<wPolicy>();
// system.debug('resolve these policies -----'+getPolicies);
        //We will cycle through our list of wPolicys and will check to see if the selected property is set to true, if it is we add the Contact to the selectedPolicies list
        for(wPolicy wPol: getPolicies()) {
          //  system.debug('resolve these policies -----'+wPol);
            if(wPol.selected == true) {
            system.debug(wPol);
            
             //getData(oldPolicyId, newPolicyId);
             //system.debug(wPol.pol.CanaryAMS__Account__r.Primary_Contact__c);
           
                selectedPolicies.add(wPol);
            }
          
         }
          
   system.debug('selectedPolicies -----'+selectedPolicies);
    if(!selectedPolicies.isEmpty())
         {
          resolveSelectedPolicies(selectedPolicies);
       //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Policy successfully merged'));
      }
         else
         {
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Select atleast one Policy you want to merge'));
         }
   policyList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class wPolicy {
        public CanaryAMS__Policy__c pol {get; set;}
        public Boolean selected {get; set;}
       // public Contact con {get; set;}
        public Map<CanaryAMS__Policy__c, Integer> suggestedPolicies {get; set;}
        public CanaryAMS__Policy__c bestMatch {get; set;}
       
        //public CanaryAMS__Policy__c suggestedPol {get; set;}
      //  public String policyName {get; set;}
       // public String recordTypeId {get; set;}
        public String recordTypeName {get; set;}
        public String policyId {get; set;}
        public String policyName {get; set;}
     
        //This is the contructor method. When we create a new wPolicy object we pass a Contact that is set to the con property. We also set the selected value to false
        public wPolicy(CanaryAMS__Policy__c p) {
            pol = p;
            selected = false;
        }
    }
   
   
   public String fetchFields(String objectName)
    {
    
  Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();

  string fieldnames=' ';
  for(String s : objectFields.keySet())
  {
   s = objectFields.get(s).getDescribe().getName() ;
      fieldnames+=s+',';
  }
     fieldnames=fieldnames.substring(0,fieldnames.length()-1);
     return fieldnames;
    
    }
   
    public void fetchData(List<wPolicy> selectedPolicies)
     { 
      System.debug('selectedPolicies3456...'+selectedPolicies);
     
       List<CanaryAMS__Policy__c> policiesToUpdate =  new List<CanaryAMS__Policy__c>();
       List<CanaryAMS__Policy__c> policiesToUpsert =  new List<CanaryAMS__Policy__c>();
       List<CanaryAMS__Policy_Contact_Junction__c> updateInsuredData = new List<CanaryAMS__Policy_Contact_Junction__c>();
       List<CanaryAMS__Vehicle__c> updateVehicleData = new List<CanaryAMS__Vehicle__c>();
       List<CanaryAMS__Claim__c> updateClaimData = new List<CanaryAMS__Claim__c>();
       List<CanaryAMS__Additional_Interests__c> updateAdditionalInterstData = new List<CanaryAMS__Additional_Interests__c>();
       List<CanaryAMS__Form__c> updateFormData = new List<CanaryAMS__Form__c>();
       List<CanaryAMS__Coverage__c> updateCoverageData = new List<CanaryAMS__Coverage__c>();
      
       string fieldnames =  fetchFields('CanaryAMS__Policy__c');
       string query=  'SELECT '+fieldnames +' from CanaryAMS__Policy__c';
           List <CanaryAMS__Policy__c> policiesData = Database.query(query);
           Map<Id, CanaryAMS__Policy__c> policiesMap = new Map<Id, CanaryAMS__Policy__c>();
          for(CanaryAMS__Policy__c pol : policiesData){
              policiesMap.put(pol.Id, pol);
          }
     
       List<CanaryAMS__Vehicle__c> vehicleData =  [select id ,Name, CanaryAMS__Policy__c from CanaryAMS__Vehicle__c];
      List<CanaryAMS__Claim__c> claimData = [select id ,Name, CanaryAMS__Policy__c from CanaryAMS__Claim__c ];
   List<CanaryAMS__Coverage__c> coverageData = [select id ,Name, CanaryAMS__Policy__c from CanaryAMS__Coverage__c];
   List<CanaryAMS__Additional_Interests__c> additionalInterstData = [select id ,Name, CanaryAMS__Policy__c from CanaryAMS__Additional_Interests__c];
   List<CanaryAMS__Form__c> formData  = [select id ,Name, CanaryAMS__Policy__c from CanaryAMS__Form__c ];
   List<CanaryAMS__Policy_Contact_Junction__c> insuredData =  [select id ,Name, CanaryAMS__Policy__c ,CanaryAMS__Contact__c from CanaryAMS__Policy_Contact_Junction__c ];
  
       Map<String, List<CanaryAMS__Vehicle__c>> polVehicleMap = new Map<String, List<CanaryAMS__Vehicle__c>>();     
         Map<String, List<CanaryAMS__Claim__c>> polClaimMap = new Map<String, List<CanaryAMS__Claim__c>>();
         Map<String, List<CanaryAMS__Coverage__c>> polCoverageMap = new Map<String, List<CanaryAMS__Coverage__c>>();
         Map<String, List<CanaryAMS__Additional_Interests__c>> polAddIntMap = new Map<String, List<CanaryAMS__Additional_Interests__c>>();  
         Map<String, List<CanaryAMS__Form__c>> polFormMap = new Map<String, List<CanaryAMS__Form__c>>(); 
         Map<String, List<CanaryAMS__Policy_Contact_Junction__c>> polConJunctionMap = new Map<String, List<CanaryAMS__Policy_Contact_Junction__c>>(); 
     
      if(!insuredData.isEmpty())
      {
        for(CanaryAMS__Policy_Contact_Junction__c insured : insuredData){
                String policyId = insured.CanaryAMS__Policy__c;
                List <CanaryAMS__Policy_Contact_Junction__c> polConJunctionList = polConJunctionMap.get(policyId);
                 if(polConJunctionList == null )
                 {
                   polConJunctionList =  new List<CanaryAMS__Policy_Contact_Junction__c>();
                 }
                 polConJunctionList.add(insured);
                 polConJunctionMap.put(policyId, polConJunctionList);
        }
      }
     
      if(!formData.isEmpty())
      {
        for(CanaryAMS__Form__c form : formData){
                String policyId = form.CanaryAMS__Policy__c;
                List <CanaryAMS__Form__c> polFormList = polFormMap.get(policyId);
                 if(polFormList == null )
                 {
                   polFormList =  new List<CanaryAMS__Form__c>();
                 }
                 polFormList.add(form);
                 polFormMap.put(policyId, polFormList);
        }
      }
     
      if(!additionalInterstData.isEmpty())
      {
        for(CanaryAMS__Additional_Interests__c AI : additionalInterstData){
                String policyId = AI.CanaryAMS__Policy__c;
                List <CanaryAMS__Additional_Interests__c> polAddIntList = polAddIntMap.get(policyId);
                 if(polAddIntList == null )
                 {
                   polAddIntList =  new List<CanaryAMS__Additional_Interests__c>();
                 }
                 polAddIntList.add(AI);
                 polAddIntMap.put(policyId, polAddIntList);
        }
      }
     
     
      if(!coverageData.isEmpty())
      {
        for(CanaryAMS__Coverage__c cov : coverageData){
                String policyId = cov.CanaryAMS__Policy__c;
                List <CanaryAMS__Coverage__c> polCoverageList = polCoverageMap.get(policyId);
                 if(polCoverageList == null )
                 {
                   polCoverageList =  new List<CanaryAMS__Coverage__c>();
                 }
                 polCoverageList.add(cov);
                 polCoverageMap.put(policyId, polCoverageList);
        }
      }
      
      if(!claimData.isEmpty())
      {
        for(CanaryAMS__Claim__c claim : claimData){
                String policyId = claim.CanaryAMS__Policy__c;
                List <CanaryAMS__Claim__c> polClaimList = polClaimMap.get(policyId);
                 if(polClaimList == null )
                 {
                   polClaimList =  new List<CanaryAMS__Claim__c>();
                 }
                 polClaimList.add(claim);
                 polClaimMap.put(policyId, polClaimList);
        }
      }
     
      if(!vehicleData.isEmpty())
      {
        for(CanaryAMS__Vehicle__c veh : vehicleData){
                String policyId = veh.CanaryAMS__Policy__c;
                List <CanaryAMS__Vehicle__c> polVehicleList = polVehicleMap.get(policyId);
                 if(polVehicleList == null )
                 {
                   polVehicleList =  new List<CanaryAMS__Vehicle__c>();
                 }
                 polVehicleList.add(veh);
                 polVehicleMap.put(policyId, polVehicleList);
        }
      }
        Map<Id,String> policyIdsMap = new Map<Id,String>();
      
      for(wPolicy wPols: selectedPolicies) {
               
          //Id oldPolicyId1 = wPols.policyId;
           String oldPolicyId = wPols.policyId;
              // system.debug(newPolicyId);   
       if(oldPolicyId != null && !oldPolicyId.equals('') )
       {
               
       CanaryAMS__Policy__c Policy = new CanaryAMS__Policy__c();
     
         //String oldPolicyId = wPols.policyId;
             Id newPolicyId = wPols.Pol.Id ;
         // Id newPolicyId = wPols.Pol.Id ;
            
             policyIdsMap.put(newPolicyId,oldPolicyId);
       CanaryAMS__Policy__c newPolicyData = policiesMap.get(newPolicyId);   //Database.query(query);;
          CanaryAMS__Policy__c oldPolicyData = policiesMap.get(oldPolicyId);  //[SELECT Id,Name,CanaryAMS__Policy_Number__c from CanaryAMS__Policy__c where Id = : oldPolicyId];
       System.debug('oldPolicyData ==== '+oldPolicyData);
       //CanaryAMS__Policy__c policyUpdate = new CanaryAMS__Policy__c();
       Policy.Id = newPolicyData.Id;
          Policy.CanaryAMS__Policy_Number__c = newPolicyData.CanaryAMS__Policy_Number__c+'_DELETE';
          Policy.CanaryAMS__To_be_Reviewed__c = false ;
       Policy.Name = newPolicyData.Name+'_DELETE';
       Policy.CanaryAMS__Policy_Number_Ex__c = Policy.CanaryAMS__Policy_Number__c;
       policiesToUpdate.add(Policy);
         
       
       
        newPolicyData.Id = oldPolicyData.Id;
           newPolicyData.CanaryAMS__To_be_Reviewed__c = false ;
          //  newPolicyData.CanaryAMS__To_be_Reviewed__c = false ;
           policiesToUpsert.add(newPolicyData);
          
          List<CanaryAMS__Vehicle__c> polVehicleList =   polVehicleMap.get(newPolicyId);
       System.debug('polVehicleList ==== '+polVehicleList);
              if(polVehicleList != null && !polVehicleList.isEmpty())
        { 
            for(CanaryAMS__Vehicle__c Vehicle : polVehicleList )
         {
            CanaryAMS__Vehicle__c updateVehicle = new CanaryAMS__Vehicle__c();
          updateVehicle.CanaryAMS__Policy__c =  oldPolicyId;
          updateVehicle.Id = Vehicle.Id;
          updateVehicleData.add(updateVehicle);
         }
        }
        
          List<CanaryAMS__Coverage__c> polCoverageList =   polCoverageMap.get(newPolicyId);
       System.debug('polCoverageList ==== '+polCoverageList);
          if(polCoverageList != null && !polCoverageList.isEmpty())
        { 
            for(CanaryAMS__Coverage__c Coverage : polCoverageList )
         {
            CanaryAMS__Coverage__c updateCoverage = new CanaryAMS__Coverage__c();
          updateCoverage.CanaryAMS__Policy__c = oldPolicyId;
          updateCoverage.Id = Coverage.Id;
          updateCoverageData.add(updateCoverage);
         }
        }
       
        List<CanaryAMS__Claim__c> polClaimList =   polClaimMap.get(newPolicyId);
       System.debug('polClaimList ==== '+polClaimList);
       
        if(polClaimList != null && !polClaimList.isEmpty())
        { 
            for(CanaryAMS__Claim__c Claim : polClaimList )
         {
            CanaryAMS__Claim__c updateClaim = new CanaryAMS__Claim__c();
          updateClaim.CanaryAMS__Policy__c = oldPolicyId;
          updateClaim.Id = Claim.Id;
          updateClaimData.add(updateClaim);
         }
        }
       
        List<CanaryAMS__Additional_Interests__c> polAddIntList =   polAddIntMap.get(newPolicyId);
       System.debug('polAddIntList ==== '+polAddIntList);
       
        if(polAddIntList != null && !polAddIntList.isEmpty())
        { 
            for(CanaryAMS__Additional_Interests__c AdditionalInterst : polAddIntList )
         {
            CanaryAMS__Additional_Interests__c updateAdditionalInterst = new CanaryAMS__Additional_Interests__c();
          updateAdditionalInterst.CanaryAMS__Policy__c = oldPolicyId;
          updateAdditionalInterst.Id = AdditionalInterst.Id;
          updateAdditionalInterstData.add(updateAdditionalInterst);
         }
        }
       
        List<CanaryAMS__Form__c> polFormList =   polFormMap.get(newPolicyId);
       System.debug('polFormList ==== '+polFormList);
        if(polFormList != null && !polFormList.isEmpty())
        { 
            for(CanaryAMS__Form__c Form : polFormList )
         {
            CanaryAMS__Form__c updateForm = new CanaryAMS__Form__c();
          updateForm.CanaryAMS__Policy__c =oldPolicyId;
          updateForm.Id = Form.Id;
          updateFormData.add(updateForm);
         }
        }
       
        List<CanaryAMS__Policy_Contact_Junction__c> polConJunctionList =   polConJunctionMap.get(newPolicyId);
       System.debug('polConJunctionList ==== '+polConJunctionList);
       
        if(polConJunctionList != null && !polConJunctionList.isEmpty())
        { 
            for(CanaryAMS__Policy_Contact_Junction__c Insured : polConJunctionList )
         {
            CanaryAMS__Policy_Contact_Junction__c insuredForm = new CanaryAMS__Policy_Contact_Junction__c();
          insuredForm.CanaryAMS__Policy__c =oldPolicyId;
          insuredForm.Id = Insured.Id;
          insuredForm.CanaryAMS__Composite_Key__c = ''+insuredForm.CanaryAMS__Contact__c+insuredForm.CanaryAMS__Policy__c;
          updateInsuredData.add(insuredForm);
         }
        }
           }
      }
    
     if(!updateVehicleData.isEmpty())
      {
      update updateVehicleData;
      System.debug('updateVehicleData ==== '+updateVehicleData);
      }
      if(!updateClaimData.isEmpty())
      {
      update updateClaimData;
      System.debug('updateClaimData ==== '+updateClaimData);
      }
     if(!updateCoverageData.isEmpty())
      {
      update updateCoverageData;
      System.debug('updateCoverageData ==== '+updateCoverageData);
      }
       if(!updateAdditionalInterstData.isEmpty())
      {
      update updateAdditionalInterstData;
      System.debug('updateAdditionalInterstData ==== '+updateAdditionalInterstData);
      }
      if(!updateFormData.isEmpty())
      {
      update updateFormData;
      System.debug('updateFormData ==== '+updateFormData);
      }
      if(!updateInsuredData.isEmpty())
      {
      update updateInsuredData;
      System.debug('updateInsuredData ==== '+updateInsuredData);
      }
      if(!policiesToUpdate.isEmpty())
      {
      delete policiesToUpdate ;
      System.debug('policiesToUpdate ==== '+policiesToUpdate);
      }//CanaryAMS__Policy_Number_Ex__c;
     
      if(!policiesToUpsert.isEmpty())
      {
      upsert policiesToUpsert;
      System.debug('policiesToUpsert ==== '+policiesToUpsert);
      }
  
   
    }
   
   
    public wPolicy fetchSuggestedPolicies(CanaryAMS__Policy__c newPolicy,List<CanaryAMS__Policy__c> oldPolicies,Map<Id, Contact> ContactsDetail, wPolicy policyWrapper)
    {
   Map<CanaryAMS__Policy__c, Integer> suggestedPolicies = new Map<CanaryAMS__Policy__c, Integer>();
      CanaryAMS__Policy__c bestMatch ;
      Integer MaxCount = 0;
     
      for(CanaryAMS__Policy__c pol : oldPolicies)
      { 
       Integer value = 0;
     
       if(newPolicy.CanaryAMS__Carrier_Product__c ==  pol.CanaryAMS__Carrier_Product__c)
       {
        value = value + 1;
         }
      
       if(newPolicy.CanaryAMS__Effective_Date__c ==  pol.CanaryAMS__Effective_Date__c)
       {
        value = value + 1;
       }
      
       Contact oldContact ;
       Contact newContact;
      
       oldContact = ContactsDetail.get(pol.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c);
       newContact= ContactsDetail.get(newPolicy.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c);
      
       if(oldContact != null && newContact !=null)
       {
        String oldPolicyContactName= ContactsDetail.get(pol.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c).Name;
        String newPolicyContactName= ContactsDetail.get(newPolicy.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c).Name;
        ContactsDetail.get(pol.CanaryAMS__Account__r.CanaryAMS__Primary_Contact__c);
      
        if(oldPolicyContactName!= null && oldPolicyContactName.equals(newPolicyContactName) )
        {
         value = value + 1;
        }
       }
      
       if(value > 0)
        {
         suggestedPolicies.put(pol,value);
      
        if(value >= MaxCount)  
        {
         bestMatch =  pol;
        }
        }
      }
     
      policyWrapper.suggestedPolicies = suggestedPolicies;
      policyWrapper.bestMatch = bestMatch;
    
      return policyWrapper;
     
    }
   
    public void resolveSelectedPolicies(List<wPolicy> selectedPolicies)
    {
      List<CanaryAMS__Policy__c> policiesToUpdate =  new List<CanaryAMS__Policy__c>();
      for(wPolicy wPols: selectedPolicies) {
        CanaryAMS__Policy__c Policy = new CanaryAMS__Policy__c();
      
       Policy.CanaryAMS__To_be_Reviewed__c = false;
       Policy.Id = wPols.Pol.Id;
      
       policiesToUpdate.add(Policy);
       }
       system.debug('policiesToUpdate -----'+policiesToUpdate);
       update policiesToUpdate;
    }
   
    public void FirstPage()
{
  policyList = null;
  OffsetSize = 0;
  system.debug('FirstPage OffsetSize === >> '+ OffsetSize);
}
public void previous()
{
   policyList = null;
  OffsetSize = OffsetSize - LimitSize;
  system.debug('previous OffsetSize === >> '+ OffsetSize);
}
public void next()
{
  policyList = null;
  OffsetSize = OffsetSize + LimitSize;
  system.debug('next OffsetSize === >> '+ OffsetSize);
}
public void LastPage()
{
  policyList = null;
  OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
  system.debug('LastPage OffsetSize === >> '+ OffsetSize);
}
public boolean getprev()
{
  //policyList = null;
  if(OffsetSize == 0)
  return true;
  else
  return false;
}
public boolean getnxt()
{
  //policyList = null;
  if((OffsetSize + LimitSize) > totalRecs)
  return true;
  else
  return false;
}

  public Map<Id,String> RecordTypeMap(){
        Map<Id,String>recordTypesMap = new map<Id,String>();
        List<RecordType> recordTypes = null;
        try{
            recordTypes = [Select Id,Name From RecordType];
            for(RecordType info: recordTypes) {
                recordTypesMap.put(info.Id,info.Name);
            }
        }catch (DmlException e) {
               system.debug('error'+e);
                return null;
        }
        return recordTypesMap;
    }    
   
}
VisualForce Page Code
<apex:page controller="PolicyWrapperClassController">

<script>
    var newWin=null;
    function openLookupPopup(name, id)
    {
        var url="/apex/LookupExamplePopup?namefield=" + name + "&idfield=" + id;
        newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
        if (window.focus)
        {
            newWin.focus();
        }
          
        return false;
    }
                
    function closeLookupPopup()
    {
       if (null!=newWin)
       {
          newWin.close();
       }
    }
  
     function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");               
            for(var i=0; i<inputCheckBox.length; i++){       
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){                                  
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
  
</script>
<apex:pageMessages />


    <apex:form >
        <apex:pageBlock id="details" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Merge Policies" action="{!processSelected}" rerender="table"/>
                  <apex:commandButton value="Mark As Resolved" action="{!markAsResolved}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!policies}" var="p" id="table">
                <apex:column >
              
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!p.selected}" id="inputId"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
              <apex:column headerValue="Policy Name" >
                <apex:outputLink value="/{!p.pol.Id}" id="eventlink" target="_blank"> {!p.pol.Name}
                    </apex:outputLink>
               </apex:column>
                <apex:column headerValue="Policy Number" value="{!p.pol.Policy_Number__c}" />
               <!--  <apex:column headerValue="Contact" >
                    <apex:outputLink value="/{!p.pol.CanaryAMS__Account__r.Primary_Contact__c}" id="eventlink">{!p.con.Name}
                    </apex:outputLink>
                </apex:column> -->
                <apex:column headerValue="Policy Type" value="{!p.recordTypeName}" />
              
               <apex:column headerValue="Carrier" value="{!p.pol.CanaryAMS__Carrier__r.Name}" />
                <apex:column headerValue="Suggested Policy" >
                    <apex:outputPanel >
                        <apex:inputHidden value="{!p.bestMatch.Id}" id="bestMatchId" />
                         <apex:outputLink value="/{!p.bestMatch.Id}" id="eventlink5" target="_blank"> {!p.bestMatch.Name}
                          </apex:outputLink>
                    </apex:outputPanel>
                </apex:column>
                         
              
                <apex:column headerValue="Policy" >
                    <apex:outputPanel >
                        <apex:inputHidden value="{!p.policyId}" id="policyId" />
                        <apex:inputText value="{!p.policyName}" id="policyName" onFocus="this.blur()" /> <a href="#" onclick="openLookupPopup('{!$Component.policyName}', '{!$Component.policyId}'); return false">Lookup</a>
                    </apex:outputPanel>
                </apex:column>
             </apex:pageBlockTable>
           
             <apex:pageblockButtons >
    <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
    <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
    <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
    <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>
   </apex:pageblockButtons>
           
        </apex:pageBlock>
      
    </apex:form>
 
</apex:page>

Ramesh KallooriRamesh Kalloori
Hi,

Use the Transient Keyword

Try using the "Transient" keyword with variables in your controllers and extensions. The transient keyword is used to declare instance variables that cannot be saved, and shouldn't be transmitted as part of the view state for a Visualforce page.

for any clarifications go through the below link

http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/
Gonzalo AbrunaGonzalo Abruna
Yes, as Ramesh said, transient is your solution. Try to use transient keyword on your controller class as much as possible http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_transient.htm
and if your visualforce is readonly, setting read-only mode for the entire page might help http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_readonly_context_pagelevel.htm

Regards.