Memory increase over time

B

Benny

I am having a memory problem with my application. Plain and simple all
the program does is runs through a bunch of images and prints them to a
PCL Printer. The first part of the application that I am noticing the
memory increase is when its scanning the specified directory I get an
error like this:

ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context
0x1b1a58 to COM context 0x1b1bc8 for 60 seconds. The thread that owns
the destination context/apartment is most likely either doing a non
pumping wait or processing a very long running operation without
pumping Windows messages. This situation generally has a negative
performance impact and may even lead to the application becoming non
responsive or memory usage accumulating continually over time. To avoid
this problem, all single threaded apartment (STA) threads should use
pumping wait primitives (such as CoWaitForMultipleHandles) and
routinely pump messages during long running operations.

If I have a smaller amount of images to print the printing *starts*
without any errors. After my application starts printing the memory
eventually gets up to an insane amount (ive seen it up to >300MB) and
then crashes. I am using a helper class that uses the ThreadPool object
to print several documents at a time. It simply acts as a event-driven
WorkQueue with WorkItem objects that contain the information the queue
is to process. I am trying to dispose of objects such as graphics and
PrintDocument but I am still getting this problem. If anyone can help I
would greatly appreciate it.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi , more info is needed, like are you using a third party tool?

what code does the scanning?
Can you post some pieces of the code that you think may be causing the
problem?
 
B

Benny

Sorry...Here's some code:

**NOTE: work is the queue that holds all work items**

This is when the user clicks start
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("root");
foreach (TreeNode node in treeView1.Nodes[0].Nodes)
{
pwi = new PrintWorkItem();
pwi.FileName = dest + node.Text.Replace(".TIF",
".pcl");
dir = new
DirectoryInfo(pwi.FileName.Substring(0,pwi.FileName.LastIndexOf("\\")));
if (!dir.Exists)
dir.Create();
pwi.ImageToPrint = Image.FromFile(root + node.Text);
pwi.PrinterName = txtPrinter.Text;
work.Add(pwi);
}
}

This is when the app looks for all files, builds an XML file for later
use as well as populating a treeview so the user may see what images
will be printed:
private void btnSearch_Click(object sender, EventArgs e)
{
lock (work)
{
xw.WriteStartDocument();
xw.WriteStartElement("FILES");
getDirsFiles(new DirectoryInfo(root));
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
//work.Resume();
}
DataSet ds = new DataSet();
ds.ReadXml("items.xml");
treeView1.Nodes.Add("Files");
foreach (DataRow dr in ds.Tables[0].Rows)
{

treeView1.Nodes[0].Nodes.Add(dr[0].ToString().Replace(root, ""));
}
treeView1.Nodes[0].Expand();
button1.Enabled = true;
}

And this is inside the acutal work item for the PrintDocument.PrintPage
handler:
public void _prntDoc_PrintPage(object sender,
PrintPageEventArgs e)
{
e.Graphics.DrawImage(_img, e.PageBounds);
e.Graphics.Dispose();
}

Thanks in advance!
 
B

Benny

VVV said:
Hi , more info is needed, like are you using a third party tool?
I am using a library created by someone on CodeProject. All the
library does is act the same as a queue but has events to execute a job
everytime a job is queued. A work item is basically a PrintDocuemnt
that prints itself on execution. I posted some of the code that shows
the event that is triggered on PrintPage (_prntDoc_PrintPage)

what code does the scanning?
I simply have a recursive function that uses FileInfo("*.tif") and
every tiff that is found is added to an array of strings.
Can you post some pieces of the code that you think may be causing the
problem?
I think the problem lies in my use of the GC in some way. I am trying
to dispose of some of my objects, mainly my Image, but i dont think are
not being collected because they still are being referenced by
something.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Benny said:
I am using a library created by someone on CodeProject. All the
library does is act the same as a queue but has events to execute a job
everytime a job is queued. A work item is basically a PrintDocuemnt
that prints itself on execution. I posted some of the code that shows
the event that is triggered on PrintPage (_prntDoc_PrintPage)


I simply have a recursive function that uses FileInfo("*.tif") and
every tiff that is found is added to an array of strings.

I think the problem lies in my use of the GC in some way. I am trying
to dispose of some of my objects, mainly my Image, but i dont think are
not being collected because they still are being referenced by
something.

Yes, but what about the COM error?
Unless you are using some unmanaged features you should not get it.

You could use a profiler to see where the memory is allocated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top