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);
Sagar
  • 399
  • 4
  • 11
  • Have you tried calling `GC.Collect()`? – Max Yankov May 08 '14 at 08:54
  • @golergka It's better to avoid manual garbage collection if possible. @Sagar Does `PrintQueue` implement `IDisposable` ? – Timothy Groote May 08 '14 at 09:02
  • Well, it was just a suggestion to figure out what's happening. – Max Yankov May 08 '14 at 09:04
  • In this case in particular, it would have made no difference, since if the dispose method on the printqueue is never called, its resource will not be released, even when manually forcing garbage collection ;) – Timothy Groote May 08 '14 at 09:07
  • GC.Collect() i tried but its doesn't releases memory – Sagar May 08 '14 at 09:27
  • And i tried calling Dispose after PrintQueue.AddJob() but this also doesnt help. – Sagar May 08 '14 at 09:28
  • Did you call it as `PrintQ.Dispose()`? Also, calling `Dispose()` does not mean you are triggering garbage collection. For as long as there is a viable reference to the object you call `dispose()` on, the garbage collector will still leave alone. That aside, even if there is no reference to the object, you cannot predict when the garbage collector will come to clean it up (this is what Golergka's `GC.Collect()` is for. (i can't tell if this is the case without seeing the rest of your code, but that might be a bit much to post here) – Timothy Groote May 08 '14 at 09:31
  • PrintServer printServer = new PrintServer(@"\\sshinde"); PrintQueue PrintQ = new PrintQueue(printServer, "HP_Printer", PrintSystemDesiredAccess.AdministratePrinter); PrintQ.AddJob("xyz", @"C:\ProgramData\MyComapny\PrintSoftware\Config\Print_Manager\INPUT\d7a8a71b-4e73-43ec-8ee9-bbe24b3ba2cb.xps", false); PrintQ.Dispose(); GC.Collect(); GC.WaitForFullGCComplete(); – Sagar May 08 '14 at 09:36
  • that's the *entire* program? (nothing holds a reference to PrintQ or printServer ?) – Timothy Groote May 08 '14 at 09:38
  • This is just test program i am trying. Here i am getting the same issue. – Sagar May 08 '14 at 09:41
  • I see the `PrintServer` also implements `IDisposable` (since they both inherit from `PrintSystemObject`) updating my example now, try doing that. – Timothy Groote May 08 '14 at 09:45

1 Answers1

0

What is happening is that the Dispose() method of the PrintQueue is never called.

If this doesn't happen, it won't release any unmanaged resources and will not let the garbage collector clean it up.

For more information see :

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

You can call Dispose() manually when you're done addressing the print queue, or encapsulate it with a using statement like this :

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);
  }
}

The Dispose method will then be called once the statements inside the using statement are completed.

The advantage of the using statement, is that it will call the dispose method even if an exception occurs.

Timothy Groote
  • 8,614
  • 26
  • 52