官方版

Fiddler throws an out-of-memory exception

Sometimes, Fiddler may show a dialog containing the following text:

  1. Exception of type 'System.OutOfMemoryException' was thrown.
  2. at System.IO.MemoryStream.set_Capacity(Int32 value)
  3. at System.IO.MemoryStream.EnsureCapacity(Int32 value)
  4. at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  5. at Fiddler.Session.Execute(Object objThreadstate)

Fiddler works by storing the entire request and response in memory. If you are performing a huge download (hundreds of megabytes) it’s possible that Fiddler cannot find a free memory block large enough to hold the entire contiguous response, and hence you’ll run into this “out of memory” problem. It’s also possible that if you have thousands of sessions in the Fiddler session list, even a relatively small memory block will not be available to store a response a few megabytes in size. You can reduce the incidence of this problem by clearing the Web Sessions list (CTRL+X) or configuring it to automatically trim to the most recent two hundred sessions (Click the Filters tab, and click the “Keep only the most recent sessions” option at the bottom).
Developers can learn more about this here: https://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx and here https://blogs.msdn.com/b/dotnet/archive/2011/10/04/large-object-heap-improvements-in-net-4-5.aspx.
Update: Fiddler2 now supports running on 64bit computers. If you’re on a 64-bit machine, you’ll never hit a problem.
If you’re on a 32-bit machine, you can avoid out-of-memory errors when downloading huge files by adding the following code inside the OnPeekAtResponseHeaders function inside Rules > Customize Rules. The line in red will cause Fiddler not to keep a copy of the large file:

  1. // This block enables streaming for files larger than 5mb
  2. if (oSession.oResponse.headers.Exists("Content-Length"))
  3. {
  4. var sLen = oSession.oResponse["Content-Length"];
  5. var iLen: Int32 = 0;
  6. if (!isNaN(sLen)){
  7. iLen = parseInt(sLen);
  8. if (iLen > 5120000) {
  9. oSession.bBufferResponse = false;
  10. oSession["ui-color"] = "yellow";
  11. oSession["log-drop-response-body"] = "save memory";
  12. }
  13. }
  14. }

If you’re using FiddlerCore or writing a Fiddler Extension, you can use code like this:

  1. Fiddler.FiddlerApplication.ResponseHeadersAvailable += delegate(Fiddler.Session oS)
  2. {
  3. // This block enables streaming for files larger than 5mb
  4. if (oS.oResponse.headers.Exists("Content-Length"))
  5. {
  6. int iLen = 0;
  7. if (int.TryParse(oS.oResponse["Content-Length"], out iLen))
  8. {
  9. // File larger than 5mb? Don't save its content
  10. if (iLen > 5000000)
  11. {
  12. oS.bBufferResponse = false;
  13. oS["log-drop-response-body"] = "save memory";
  14. }
  15. }
  16. }
  17. };

谷歌翻译版

Fiddler抛出内存不足异常

有时,Fiddler可能会显示一个包含以下文本的对话框:

  1. Exception of type 'System.OutOfMemoryException' was thrown.
  2. at System.IO.MemoryStream.set_Capacity(Int32 value)
  3. at System.IO.MemoryStream.EnsureCapacity(Int32 value)
  4. at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
  5. at Fiddler.Session.Execute(Object objThreadstate)

Fiddler通过将整个请求和响应存储在内存中来工作. 如果您正在执行大量下载(数百兆),则Fiddler可能找不到足够大的可用内存块来容纳整个连续响应,因此您将遇到此“内存不足”问题.如果您在Fiddler会话列表中有数千个会话,那么即使是相对较小的内存块也将无法存储几兆字节的响应,这也是有可能的. 您可以通过清除Web会话列表(CTRL + X)或将其配置为自动调整为最新的200个会话来减少此问题的发生(单击“筛选器”选项卡,然后单击“仅保留最新的会话”选项 在底部)。
开发人员可以在此处了解更多信息: https://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx 或者这里 https://blogs.msdn.com/b/dotnet/archive/2011/10/04/large-object-heap-improvements-in-net-4-5.aspx.
更新:Fiddler2现在支持在64位计算机上运行。 如果您使用的是64位计算机,则永远不会遇到问题。
如果您使用的是32位计算机,则可以通过在Rules > Customize Rules内的OnPeekAtResponseHeaders函数内添加以下代码,来避免下载大文件时出现内存不足错误。 红色的行将导致Fiddler不保留大文件的副本:

  1. // This block enables streaming for files larger than 5mb
  2. if (oSession.oResponse.headers.Exists("Content-Length"))
  3. {
  4. var sLen = oSession.oResponse["Content-Length"];
  5. var iLen: Int32 = 0;
  6. if (!isNaN(sLen)){
  7. iLen = parseInt(sLen);
  8. if (iLen > 5120000) {
  9. oSession.bBufferResponse = false;
  10. oSession["ui-color"] = "yellow";
  11. oSession["log-drop-response-body"] = "save memory";
  12. }
  13. }
  14. }

如果您使用的是FiddlerCore或编写Fiddler Extension,则可以使用如下代码:

  1. Fiddler.FiddlerApplication.ResponseHeadersAvailable += delegate(Fiddler.Session oS)
  2. {
  3. // This block enables streaming for files larger than 5mb
  4. if (oS.oResponse.headers.Exists("Content-Length"))
  5. {
  6. int iLen = 0;
  7. if (int.TryParse(oS.oResponse["Content-Length"], out iLen))
  8. {
  9. // File larger than 5mb? Don't save its content
  10. if (iLen > 5000000)
  11. {
  12. oS.bBufferResponse = false;
  13. oS["log-drop-response-body"] = "save memory";
  14. }
  15. }
  16. }
  17. };