1

I am Using System.Printing library in my application with .net framework 3.5. When i Print job on any printer using PrintQueue.AddJob memory gets increased and doesnt releases it. Memory is released only if the application is closed. If i print more than 10 jobs then my application uses all the memory of my computer and finally everything gets slows down. I obeserved the memory usage goes to 2GB, which is not acceptable. After investigation i found that Memory gets increased when PrintqQueue.AddJob method is invoked.

Here is my sample Code for printing:

PrintServer printServer = newPrintServer(@"\\sshinde");
PrintQueue PrintQ = newPrintQueue(printServer, "HP_Printer", PrintSystemDesiredAccess.AdministratePrinter);
PrintQ.AddJob("xyz", @"C:\ProgramData\MyComapny\PrintSoftware\Config\Print_Manager\INPUT\d7a8a71b-4e73-43ec-8ee9-bbe24b3ba2cb.xps", false);
4

1 に答える 1

0

何が起こっているかというと、 のDispose()メソッドがPrintQueue呼び出されないということです。

これが発生しない場合、管理されていないリソースは解放されず、ガベージ コレクターによってクリーンアップされません。

詳細については、次を参照してください。

http://msdn.microsoft.com/en-us/library/ms584331(v=vs.110).aspx

Dispose()印刷キューのアドレス指定が完了したら手動で呼び出すか、次のusingようなステートメントでカプセル化できます。

using(PrintServer printServer = newPrintServer(@"\\sshinde"))
{
  using(PrintQueue PrintQ = newPrintQueue(printServer, "HP_Printer", PrintSystemDesiredAccess.AdministratePrinter))
  {
    PrintQ.AddJob("xyz", @"C:\ProgramData\MyComapny\PrintSoftware\Config\Print_Manager\INPUT\d7a8a71b-4e73-43ec-8ee9-bbe24b3ba2cb.xps", false);
  }
}

ステートメント内のステートメントが完了すると、Disposeメソッドが呼び出されます。using

このステートメントの利点は、例外が発生した場合でもusingdispose メソッドを呼び出すことです。

于 2014-05-08T09:09:40.913 に答える