title: Getting heap size error while deserializing json respone in apex
body: <p>I am using third party service to get data using rest API. But sometimes it return more than 6 MB data. They don't have API to provide data in chunks. I am already using future method to increase heap size limit to 12 MB.</p>
  
  <p>With below code, I am facing heap size error at the time of de-serialization of data. I have also emphasised that line where I am getting error.</p>
  
  <pre><code>@future (callout=true)
  public static void CreateProjects(set&lt;ID&gt; setOfProjectID)
  {
      HttpRequest request;
      HttpResponse response = new HttpResponse();
      Map&lt;Id,Opportunity&gt; mapOfIdAndProjectToCreate = new Map&lt;Id,Opportunity&gt;([SELECT Id, Name, projectId__c FROM Opportunity 
                                                      WHERE ID IN :setOfProjectID]);
      Integer customerId;
      List&lt;Opportunity&gt; lstOppToUpdate = new List&lt;Opportunity&gt;();
      if(!mapOfIdAndProjectToCreate.isEmpty())
      {
          for(Id OpportunityID : mapOfIdAndProjectToCreate.keySet())
          {
              Opportunity oppCreated = new Opportunity();
              oppCreated = mapOfIdAndProjectToCreate.get(OpportunityID);
              try
              {
                  String projectId;
                  String requestBody = createRequestBodyForProject(mapOfIdAndProjectToCreate.get(OpportunityID));
                  HttpRequest req = CreateHttpRequest(ConstantCls.CreateProjectURL,requestBody,'POST');
                  Http http = new Http();
                  HTTPResponse resp = new HTTPResponse();
                  if(!Test.IsRunningTest())
                  {
                      resp = http.send(req);
                      System.debug('heap size after api call '+Limits.getHeapSize());
                  }
                  if(resp.getStatusCode() == 200)
                  {
                      System.debug('heap size before deserializtion '+Limits.getHeapSize());
  // Till now we have 6 MB data in HTTPResponse resp variable, but when we go for deserializing by fetching its body, it gives Heap size limit error. 
  // It is giving error when we try to fetch its body, because it has already size of response of 6 MB + little bytes are occupied by others variable. and when we fetch its body, it exceeds to more than 12 MB.
  // In execution of below line, I am getting Heap size limit error.
                      *Map&lt;String, Object&gt; mapKeyVal = (Map&lt;String, Object&gt;)System.JSON.deserializeUntyped(resp.getBody());*
  System.debug('heap size after deserialization '+Limits.getHeapSize());
                      Map&lt;String, Object&gt; data = (Map&lt;String, Object&gt;)mapKeyVal.get('data');
                      projectId = String.valueOf(data.get('projectId'));
                      oppCreated.projectId__c = projectId;
                      lstOppToUpdate.add(oppCreated);
                  }
              }
              Catch(Exception ex)
              {
                  System.debug('Error In Sync - CreateProjects method Updated - '+ ex.getMessage() + ex.getStackTraceString());              
              }
          }
      }
      UPDATE lstOppToUpdate;
  }
  public static HttpRequest CreateHttpRequest(String apiUrl, String requestBody, String method)
  {
      Map&lt;String,String&gt; apiConfigs = GetAllConfigSettings();   
      HttpRequest req = new HttpRequest();
      req.setEndpoint(apiUrl);
      req.setMethod(method);  
      req.setTimeout(120000);
      req.setHeader('Content-Type', 'application/json');  
      req.setHeader('Authorization', apiConfigs.get('Access Token'));  
      if(String.isNotEmpty(requestBody))
      {
          req.setBody(requestBody);    
      }
      return req;
  }
  </code></pre>