This is using a COM component that I call from my .NET code. I'm having a huge memory leak problem. I've been advised from some helpful folks in these forums that its quite possible using the COM object is causing this.
Is there some relatively simple .NET code that can do the same thing? I think the code is pretty self explanatory. Simple resize, compress, and also I need the option to watermark.
Thanks alot
Mike123
Public Sub ImageCreate_75(ByVal Source As String, ByVal Destination As String)
objManage = New ManageClass()
If objManage.FileExists(Source) Then
objManage.ReadFile(Source)
If objManage.Width > objManage.Height Then
objManage.Resize(75, 0)
Else
objManage.Resize(0, 75)
End If
objManage.JpegQuality = 70
objManage.WriteFile(Destination)
End If
--objManage = Nothing
End SubYou definitely need to UN-Comment out setting it to Nothing - - if it doesn't close, it will cause a memory leak
Setting it to nothing may just set the .NET reference to nothing, but the actual job may happen during the next garbage collection.
To explicitly free a reference to a COM object, use Marshal.ReleaseComObject
I will definately add the set to nothing line, it was overlooked that it was not put in.
A quick lookup on Marshal.ReleaseComObject leads me to this
http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
Sounds kinda scary, this is a highly used com object. Had any bad experiences?
Does anybody have any recommendations on how to do this in pure .NET ? The code just resizes, compresses, and watermarks JPG's and BMP's into JPGs. Maybe a component is out there that can do it as easily?
Thanks ALOT for the help
Mike123
The set=nothing line hasn't really helped.
I'm having major memory problems, my server is crashing daily. I see garbage collection running and it does trim a fair bit of memory off w3wp.exe. At times it will trim it down from 1gig to 800mb ...
However I am still getting these OUTOFMEMORY exceptions.
Whats the best way in finding exactly what it is? I took down the page that uses the COM component for the weekened and it still managed to crash.
Really unsure at this point, seems like a .net memory management issue.
I am thinking of upgrading my server to 4 gigs RAM to delay the problem. Can ASP.NET take full advantage of having this much memory?
Thanks alot guys, your help is REALLY appreciated in this jam.
Mike123
Most of the time, setting to nothing does pretty much nothing in a garbage collected world: as the variable gets out of scope, it will get collected anyways.
The problem here is to explicitly release the underlying COM object immediately, and ReleaseComObject is the way to go for this.
Now, your particular problem can easily be solved in managed code. You need to take a look at System.Drawing. I can give you some pointers if necessary.
I also hope you're not calculating these images on-the-fly every time they are asked for, and that you cache the resulting image to the disk so that next time it can be sent directly without having to be recalculated.
As for the blog entry you're citing, yes, you have to be careful, but it is really relevant when you're doing real multithreading. If you are in an ASP.NET app and use ReleaseComObject in the same scope that you created the object, you should be fine. It's the same as in the old COM days: manage your references, release as many times as you got references, that sort of things. I'm no expert in COM interop, though.
Sometime object=nothing won't help lot, especially for COM objects. I had the similar problem, and used the following code to release the COM object, I did n't have that problem anymore. Try this,
Dim lc As lCPrimary.Calc
Try
lc = New lCPrimary.Calc
-- do something --
Catch ex As Exception
If Not lc Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(lc)
lc = Nothing
End If
Throw ex
End Try
Can you do a .Dispose() on objManage?
Thanks for all your help. I am going to attempt to close the COM object using the suggestions that have been made. I will keep this thread up to date with my progress.
One thing that bothers me is that the page that called this COM object I took offline to prevent server crashes. However it did not stop the server crashes, so I am thinking its possible that its not even related to this object.
Assuming this COM object is not the problem. What other steps can I take? Can asp.net take full advantage of 4GB of Ram on the box? If it could this would probably help it happen less frequently. I am also looking at scheduling IIS to restart every night.
Thanks again everyone for the assitance
Mike123
Just as a side note: I just watched my w3wp.exe go from 1.45 gb to 1.07 gb of memory usage. So its almost like Garbage collection isnt running properly when the server crashes.
Mike123
For what it's worth...
Are you sure that the out of memory error isn't referring to disk space?
We do have log files that run up quite quickly. However I have never noticed a full HD. When the problem occurs I just restart IIS and it is fixed, so I assume its not HD space.
I will keep an eye on HD space however and see how it relates, thanks for the tip.
Mike123
Please consider doing that in managed code. It would be so much easier and faster...
Hi Bleroy,
I will defiantely convert this to managed code as fast as I can to eliminate any chances this could be the problem. I tried awhile back but had problems with a couple things I needed to do. This was about 2 years ago when I was first learning .NET.
Can I do this with .NET
Set the quality of the image being compressed
Merge 2 images together to create a watermark.
If you have any code samples, or links that would be incredible.
Thanks once again,
Mike123
Try this:
http://www.codeproject.com/csharp/watermark.asp?target=watermark
To set the quality of the jpeg, use this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImagingEncoderClassQualityTopic.asp
And of course, cache the resulting files to disk for fast reuse.
0 comments:
Post a Comment