out of memory exception in C# Outlook add-in

R

rchf

I am getting an out of memory exception in an Outlook add-in written in C#.
The problem is, there is over 700 MB of memory left to be used. Is there a
setting I need to change or something like that? I'm stumped.
 
K

Ken Slovak - [MVP - Outlook]

It's hard to say what's happening without knowing what your code is doing.

You could be running out of the RPC channel limit if you're not releasing
objects in a loop or if you're using lots of dot operators and forcing
Outlook to create internal variables for every dot operator position. You
might need to release your objects explicitly or even call
Marshal.ReleaseComObject on them and then wait for the GC to finalize.
 
R

rchf

Ken,

Thank you for your advice. A few more questions...

We are relying heavily on GC as we overwrite ArrayList objects multiple
times. I gather we should be explicitly
Finalizing/Disposing/Marshal.ReleaseCOMObject-ing these structures because
the GC does not actually recover the COM objects?

Its this true in all cases where we overwrite a managed object or have it go
out of scope?

Any guidance as to which cases deallocating Managed objects results in the
most corruption of memory?

Could it be we exhaust memory (1Gb), when the Task Manager shows memory in
the area of 50Mb for the Outlook Addin (factor of 20x)? Does this C#
unmanged object problem NOT show up in the Task Manager report of memory used
by Outlook+Addin?

Looking up "RPC Channel Limit" it appears to be associated with
Outlook-Exchange messaging. Is this still a factor when we are connecting to
a non-Exchange SMTP email server?

Lastly and most importantly, is there some well below maximum RAM on the
system for Outlook VSTO Addins?

Thank you
 
K

Ken Slovak - [MVP - Outlook]

Almost every developer follows their own rules for when to just release
objects by setting them to null and when to call GC on them.

Since calling GC and waiting for finalization is a costly call in terms of
performance I usually follow a rule of calling GC only when the objects are
being released in a time sensitive context.

If it's say an Inspector that's closing I probably would only set any class
level objects to null. At the end of each procedure I explicitly set each
local object to null. That's a hangover from earlier versions of Outlook
that are more sensitive to non-released objects than Outlook 2007 is.

In my shutdown code that might get called when the last Explorer is being
closed or when OnBeginShutdown or OnDisconnection is called I call
Marshal.ReleaseComObject on each object and then GC.Collect() followed by
GC.WaitForPendingFinalizers().

Other developers follow their own rules and there's debate about whether or
not to call Marshal.ReleaseComObject at all in some cases.

This rule of thumb only applies to unmanaged objects such as Outlook
objects, it does not apply to managed objects. If your ArrayList objects are
being set to null or being disposed of properly they are managed objects and
you shouldn't call to release them from COM since they aren't COM objects.

The RPC limit is for Exchange.
 

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