title: Memory limit for running external executables within Asp.net
body: <p>I am using WkhtmltoPdf in my C# web application running in .NET 4.0 to generate PDFs from HTML files. In general everything works fine except when the size of the HTML file is below 250KB. Once the HTML file size increases beyond that, the process which runs the wkhtmltopdf.exe gives an exception as below. On the Task Manager, I have seen that the Memory value for the wkhtmltopdf.exe process does not increase beyond a value of 40,096 K, which I believe is the reason why the process is abandoned in between. </p>
  
  <p>How can we configure such that the memory limit for external exes can be increased? Is there any other way of solving this issue?</p>
  
  <p>More info: <br/>
  When I run the conversion from the command line directly, the PDF is generated fine. So, its unlikely to be a problem with WkhtmlToPdf.</p>
  
  <p>The error is from localhost. I have tried the same on the DEV server, with the same result.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <blockquote>
    <p>More specific exception message: - For the MainModule property of the
    Process object, the error says - {"Only part of a ReadProcessMemory or
    WriteProcessMemory request was completed"}, with the NativeErrorCode
    value - 299.</p>
  </blockquote>
  
  <p>Exception:</p>
  
  <pre><code>&gt; [Exception: Loading pages (1/6) [&gt;                                    
  &gt; ] 0% [======&gt;                                                     ]
  &gt; 10% [======&gt;                                                     ] 11%
  &gt; [=======&gt;                                                    ] 13%
  &gt; [=========&gt;                                                  ] 15%
  &gt; [==========&gt;                                                 ] 18%
  &gt; [============&gt;                                               ] 20%
  &gt; [=============&gt;                                              ] 22%
  &gt; [==============&gt;                                             ] 24%
  &gt; [===============&gt;                                            ] 26%
  &gt; [=================&gt;                                          ] 29%
  &gt; [==================&gt;                                         ] 31%
  &gt; [===================&gt;                                        ] 33%
  &gt; [=====================&gt;                                      ] 35%
  &gt; [======================&gt;                                     ] 37%
  &gt; [========================&gt;                                   ] 40%
  &gt; [=========================&gt;                                  ] 42%
  &gt; [==========================&gt;                                 ] 44%
  &gt; [============================&gt;                               ] 47%
  &gt; [=============================&gt;                              ] 49%
  &gt; [==============================&gt;                             ] 51%
  &gt; [============================================================] 100%
  &gt; Counting pages (2/6)                                               
  &gt; [============================================================] Object
  &gt; 1 of 1 Resolving links (4/6)                                          
  &gt; [============================================================] Object
  &gt; 1 of 1 Loading headers and footers (5/6)                              
  &gt; Printing pages (6/6) [&gt;                                               
  &gt; ] Preparing [=&gt;                                                       
  &gt; ] Page 1 of 49 [==&gt;                                                   
  &gt; ] Page 2 of 49 [===&gt;                                                  
  &gt; ] Page 3 of 49 [====&gt;                                                 
  &gt; ] Page 4 of 49 [======&gt;                                               
  &gt; ] Page 5 of 49 [=======&gt;                                              
  &gt; ] Page 6 of 49 [========&gt;                                             
  &gt; ] Page 7 of 49 [=========&gt;                                            
  &gt; ] Page 8 of 49 [==========&gt;                                           
  &gt; ] Page 9 of 49 [============&gt;                                         
  &gt; ] Page 10 of 49 [=============&gt;                                       
  &gt; ] Page 11 of 49 [==============&gt;                                      
  &gt; ] Page 12 of 49 [===============&gt;                                     
  &gt; ] Page 13 of 49 [================&gt;                                    
  &gt; ] Page 14 of 49 [==================&gt;                                  
  &gt; ] Page 15 of 49 [===================&gt;                                 
  &gt; ] Page 16 of 49 [====================&gt;                                
  &gt; ] Page 17 of 49 [=====================&gt;                               
  &gt; ] Page 18 of 49 [======================&gt;                              
  &gt; ] Page 19 of 49 [========================&gt;                            
  &gt; ] Page 20 of 49 [=========================&gt;                           
  &gt; ] Page 21 of 49 [==========================&gt;                          
  &gt; ] Page 22 of 49 [===========================&gt;                         
  &gt; ] Page 23 of 49 [============================&gt;                        
  &gt; ] Page 24 of 49 [==============================&gt;                      
  &gt; ] Page 25 of 49 [===============================&gt;                     
  &gt; ] Page 26 of 49 [=================================&gt;                   
  &gt; ] Page 27 of 49 [==================================&gt;                  
  &gt; ]
  </code></pre>
  
  <p>Code that I use:</p>
  
  <pre><code>    var fileName = " - ";
      var wkhtmlDir = ConfigurationManager.AppSettings[Constants.AppSettings.ExportToPdfExecutablePath];
      var wkhtml = ConfigurationManager.AppSettings[Constants.AppSettings.ExportToPdfExecutablePath] + "\\wkhtmltopdf.exe";
      var p = new Process();
  
  
      string switches = "";
      switches += "--print-media-type ";
      switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 5mm --margin-left 5mm ";
      switches += "--page-size A4 ";
      switches += "--disable-smart-shrinking ";
  
      var startInfo = new ProcessStartInfo
      {
          CreateNoWindow = true,
          FileName = wkhtml,
          Arguments = switches + " " + url + " " + fileName,
          UseShellExecute = false,
          RedirectStandardOutput = true,
          RedirectStandardError = true,
          RedirectStandardInput=true,
          WorkingDirectory=wkhtmlDir
      };
  
      p.StartInfo = startInfo;
      p.Start();
  </code></pre>
  
  <p>Debugger Screenshot of WkHtmlToPdf.exe Process:</p>
  
  <p><img src="https://i.stack.imgur.com/QNGgL.png" alt="enter image description here"></p>