Memory Leaks

  • Thread starter Thread starter Malcolm Klotz
  • Start date Start date
M

Malcolm Klotz

Hello,

I have created a Windows Service application in VB.Net 2005. The service is
pretty basic, it uses the System.Timers.Timer class to poll a database that
checks for jobs to perform. Upon finding a job, the
System.Timers.ElapsedEventHandler creates a new thread to execute the job.

The job is pretty basic, it uses and xml document and applies it to an XSLT
document using the XSLTransform class.

After this services runs a job the memory doubles, and keeps on doubling
until it finally crashes on memory. When I profiled this application, I
noticed the the Byte[] class was holding the most memory, and was surviving
all subsequent garbage collections. I pinned the Byte[] to the following
method:

Private _XSLTDocument As Xml.Xsl.XslTransform

Private Sub InitializeTemplate(ByVal aXSLTLocation As String)

Dim xsltFileSpec As String

If IO.File.Exists(aXSLTLocation) Then
_XSLTDocument = New Xml.Xsl.XslTransform
_XSLTDocument.Load(xsltFileSpec, New Xml.XmlUrlResolver)
Else
Throw New IO.FileNotFoundException(String.Format("No XLS
Transform document was found at: {0}.", xsltFileSpec))
End If

End Sub

It appears (to me) that the XSLTransform class is not being collected or is
not releasing the memory it occupies. I have impliemented the IDosposable
interface for the class that holds this method (although, I do not see why
it was needed, but I was trying to fix the problem). I have made sure that
the thread the this class runs on is indeed at a stopped state when
completed. I have exhausted my potentional, any help here would be
appreciated.

Bottom line: Why is the the Load method on the class XSLTransform taking up
so much memory, and how do I release it?

Thanks.

Malcolm Klotz
 
Strange. The XslTransform class doesn't implement IDisposable, so it
should only contain managed resources, and thus should be able to be
handled by the garbage collector.

The XslTransform class is obsolete in framework 2.0. You should use the
XslCompiledTransform class instead.
 
That is what I thought (re. garbage collection), it also seems as if I
pasted my depreciated code, I am using the XslCompiledTransform class, but I
still see this behaviour. Is there something special you have to do to a
thread to have it cleaned properly?

Thanks.

Malcolm.

Göran Andersson said:
Strange. The XslTransform class doesn't implement IDisposable, so it
should only contain managed resources, and thus should be able to be
handled by the garbage collector.

The XslTransform class is obsolete in framework 2.0. You should use the
XslCompiledTransform class instead.

Malcolm said:
Hello,

I have created a Windows Service application in VB.Net 2005. The service
is pretty basic, it uses the System.Timers.Timer class to poll a database
that checks for jobs to perform. Upon finding a job, the
System.Timers.ElapsedEventHandler creates a new thread to execute the
job.

The job is pretty basic, it uses and xml document and applies it to an
XSLT document using the XSLTransform class.

After this services runs a job the memory doubles, and keeps on doubling
until it finally crashes on memory. When I profiled this application, I
noticed the the Byte[] class was holding the most memory, and was
surviving all subsequent garbage collections. I pinned the Byte[] to the
following method:

Private _XSLTDocument As Xml.Xsl.XslTransform

Private Sub InitializeTemplate(ByVal aXSLTLocation As String)

Dim xsltFileSpec As String

If IO.File.Exists(aXSLTLocation) Then
_XSLTDocument = New Xml.Xsl.XslTransform
_XSLTDocument.Load(xsltFileSpec, New Xml.XmlUrlResolver)
Else
Throw New IO.FileNotFoundException(String.Format("No XLS
Transform document was found at: {0}.", xsltFileSpec))
End If

End Sub

It appears (to me) that the XSLTransform class is not being collected or
is not releasing the memory it occupies. I have impliemented the
IDosposable interface for the class that holds this method (although, I
do not see why it was needed, but I was trying to fix the problem). I
have made sure that the thread the this class runs on is indeed at a
stopped state when completed. I have exhausted my potentional, any help
here would be appreciated.

Bottom line: Why is the the Load method on the class XSLTransform taking
up so much memory, and how do I release it?

Thanks.

Malcolm Klotz
 
Hi Malcolm,

There is nothing special that you need to set to have GC clean the memory.
The only thing that GC checks to see if an object should be finalized is
object reference. If there is no object reference on it, it will be GCed.

Please check your code, if you're holding some reference to that object
which makes it impossible to dispose.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Malcolm,

The object created on one thread can be referenced by object on another
thread. Also the thread stops, the reference is still there and the object
will not be finalized by GC.

There are some tools that can check what is referencing the object, like
WinDbg. You can try to post in the windbg newsgroups to see how to
troubleshoot on this issue.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Kevin,

I found the problem, thought it might be of interest to you and the group.
I was using the class XSLCompiledTransformation, and I had narrowed down the
leak to this class (actually several layers down, but this was the start of
the call), it seemed as if this instance was holding onto large Byte Arrays.
I changed the way I instantiated the class,
initially I had: New Xml.Xsl.XslCompiledTransform(True), however, when you
change it to false, the leak disappears. I had set it to true in development
to get detailed error messages with the XSL document I was using.



Not sure if this behaviour is by design, or whether there is a bug with
setting that flag to true.



(PS: I used ANTS profiler to help me out, pretty easy to use)



Thanks for you help.



Malcolm
 
Hi Malcolm,

I did some research, but didn't find any information related to this issue.
Anyway, it's good to known that you have had a workaround. Thanks for your
feedback. I'll forward this to product team through an appropriate channel.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top