Showing posts with label memory. Show all posts
Showing posts with label memory. Show all posts

Thursday, March 22, 2012

System.OutOfMemoryException

I just got this error on my server even though I have lots of available memory. I did notice that my aspnet_wp.exe was at 648MB. How can I get the bugger back down to a lower MB usage without restarting the asp worker processor?Not sure why you are getting the error when you have available memory, maybe you incorrectly customised the garbage collection?

As far as reducing memory usage, well there could be multiple efficiencies possible in your code, there is no magic bullet :(.

Cheers
Martin
No, I didn't customize the garbage collection. I wouldn't even knwo where to begin on how to do such a thing. I am looking into my code trying to see where I might have efficiency problems. Mainly looking at if I dispose of all my object correctly. If I dispose of my objects in the finally block will that get remove them from memory?

Thanks.
Garbage collection usually manages the clean up of .Net objects. Although it is possible, MS recommends that you do not modify the behavior of the garbage collector so don't let me put bad ideas into your head :).

As far as disposing of objects, you usually only need to woory about that for unmanaged objects (legacy). How large is your code, can you put up a sample?

Cheers
Martin
Are you storing a lot of stuff in cache or session variables?
Rethink your design or let somebody do a code review ...
I would like to do a code review. What is the best way to do this?

System.OutOfMemoryException

What should I do to combat this error? I am doing fairly heavy dataset caching on my webapp. I have 2 gigs memory, and aspwp.exe is using up to 1.3gigs. This caching is essential to my application as it saves ALOT of database queries.

I thought asp.net would recycle what it needed to. Is the best solution to just thro more RAM at it? Thats what I plan on doing, but its going to take a few days to get the server up and running and need a solution in the meantime. Also I am worried what is going to happen when I have my server maxed out on ram usage and I get this error

thanks for any help

mike123Are you setting any priority on the cache items when adding them? Reason I ask is that if everything is being set to "high", it may not be recycling anything w/ the garbage collection.

Also, when you add things to cache, are you checking to see if they already exist or possibly even deleting the old cache before adding it again?
Are any of your caches set to time out (I would think so).

If your cache is too broad, I can see this being a problem. If you are caching a search page, it would be very possible for you to hit the limit of your RAM.

Charley.
I am not setting priority on anything, not high or low. Here is an example of how I cache my dataset. I'm not sure about deleting the old cache before adding it again, because I dont think that happens since I check before I add, please correct me if im wrong on that.

Also, Charley I believe you may be exactly correct on the searches taking all of my memory, I am looking at making them expire quicker and setting their priority low. Is this what you would recommend? How would you modify this function?

Thanks alot
mike123

Public Function QuestionsGet(ByVal userID As Integer) As DataSet

Dim myDataSet As New DataSet()

If HttpContext.Current.Cache.Get("select_questions" & CStr(userID)) Is Nothing Then

' Create Instance of Connection and Command Object
Dim objConn As New SqlConnection("uid=")
Dim objCommand As New SqlDataAdapter("select_questions", objConn)

' Mark the Command as a SPROC
objCommand.SelectCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim parameterUserID As New SqlParameter("@.userID", SqlDbType.Int, 4)
parameterUserID.Value = userID
objCommand.SelectCommand.Parameters.Add(parameterUserID)

'Fill the DataSet
objCommand.Fill(myDataSet, "select_questions")
HttpContext.Current.Cache.Item("select_questions" & CStr(userID)) = myDataSet

Else
myDataSet = CType(HttpContext.Current.Cache.Get("select_questions" & CStr(userID)), DataSet)
End If

Return myDataSet

End Function
Tell us a bit about what you are trying to accomplish.

What type of data are you returning?

I see 'questions' based by the user Id.

Are these very static?

If so, a different way to approch this might be to generate an XML file durning a nightly batch, and instead of reading the DB, read that file instead...
They are fairly static I have probably 20k cached, but not static enough to do a nightly update I believe. I have at least a few hundred changing daily. They are directly tied to users info, I have cached each users info. The data is about 8 text strings shorter than 500 chars.

Thanks for the suggestion, but I dont think it will work in this case. What do you think?

Also, I am attempting to add ", Caching.CacheItemPriority.Low" to my cache statement, but its not accepting the final argument. Am I approaching this wrong?

Thanks again
mike123
anyone? I'm still getting this error and have no idea what to do! :(

thanks again
mike123
Mike.

I'm just throwing out ideas here...

Instead of caching the datasets, have you tried caching the page itself?

Some other options are (Not saying these are the BEST options, but options nonetheless):
- Disable caching until you get more RAM
- Reevaluate queries and see if you can combine anything
- Reevaluate your architecture so that you aren't as database intensive on any given page

Are your pages hitting directly against the database?
Charley,

Unforunately I can't output cache the page, because I need to manually expire it when certain occurances happen. One page in particular has 4 queries, and is being hit about 1 million times per day, so if I lose the caching my database server is going to have soem seroius extra load on it. I would love to get more RAM, however I need a scalable solution and I dont want the same thing to happen when I max out on my new server with 4gigs instead of 2.

Thanks for the input

mike123
4 million queries a day... yowsers...

What the heck kind of app is this?

Have you done performance monitoring on the SQL server?

If you have caching off, does it peg that server?
It's a web app where users have their own personal page, that is database driven. Yes, it sure does peg the server if I turn caching off so I really appreciate the caching. It just really sucks to have it crap out when it runs out of memory, I thought asp.net would be a little better at handling it.

I can upgrade to 4 gigs, but thats just a temporary solution and its goign to cost alot of time / money.

Thanks

System.OutOfMemoryException: Out of memory.

Hello all

I have a ASPX file called scaleimage.aspx that scales my images down
to a given width. This page is used very much in web project I am
working on.

But due to the large size of the images I randomly get [X] images
(error) in Internet Explorer. When I open one individual image at that
point I read this exception:

[OutOfMemoryException: Out of memory.]
System.Drawing.Bitmap..ctor(String filename) +197
tekno.scaleimage.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

Any suggestions are more than welcome. I hope you can help me.
Thank you in advance.

Regards,

Freek Versteijn

See here my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim img As Image = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
img = resizeImage(img, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

img.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
img.Dispose()
End Sub

Public Function ResizeImage(ByRef image As Image, ByVal maxWidth
As Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double

Dim bm As Bitmap = New Bitmap(image)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h,
Nothing, inp)

Return bmp
End FunctionHi, Versteijn,

Did you try using the DrawImage method on the Graphics class instead? You
will have to change the following lines:

> Dim inp As IntPtr = New IntPtr
> Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h, Nothing, inp)

to:

Dim bmp As New Bitmap(w, h)
Graphics.FromImage(bmp).DrawImage(bm, w, h)

Hope this helps
Martin
Sorry, it should be another overload:

Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)

It is so easy with the IntelliSence...

Greetings
Martin
"Martin Dechev" <detcheff_@.hotmail.com> wrote in message news:<#uSXmc3$DHA.2660@.TK2MSFTNGP10.phx.gbl>...
> Sorry, it should be another overload:
> Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)
> It is so easy with the IntelliSence...
> Greetings
> Martin

Hi Martin,

Thanks, I'll try it! Why do you think this will spare memory?

Thank you.

Freek Versteijn
Hi, Freek Versteijn,

I don't know from which call the OutOfMemory originates, but I tested with a
2 MB bitmap scaling it to ~75% and it was working with the both methods. As
it is stated in the documentation for the GetThumbnailImage method it gives
good quality for 120x120, but lacks in quality for higher resolutions.

http://msdn.microsoft.com/library/e...mageTopic .asp

Greetings
Martin
"Versteijn" <versteijn@.538mail.nl> wrote in message
news:4d19834f.0403010822.54274e09@.posting.google.c om...
> "Martin Dechev" <detcheff_@.hotmail.com> wrote in message
news:<#uSXmc3$DHA.2660@.TK2MSFTNGP10.phx.gbl>...
> > Sorry, it should be another overload:
> > Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)
> > It is so easy with the IntelliSence...
> > Greetings
> > Martin
> Hi Martin,
> Thanks, I'll try it! Why do you think this will spare memory?
> Thank you.
> Freek Versteijn
> > Hi Martin,
> > Thanks, I'll try it! Why do you think this will spare memory?
> > Thank you.
> > Freek Versteijn

Hi Martin,

Changed my code a bit (see under), but ever call to the page uses
about 10-50 mb RAM, which is not freed after the response has been
send. After a while the process can't address any more memory.

The cause of the high memory use is the GetThumbnailImage function.

Regards,

Freek Versteijn

Here is my current code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim bmp As Bitmap = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
ResizeImage(bmp, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
bmp.Dispose()
End Sub

Public Sub ResizeImage(ByRef bmp As Bitmap, ByVal maxWidth As
Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double
Dim bm As Bitmap = New Bitmap(bmp)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
bmp = bm.GetThumbnailImage(w, h, Nothing, inp)
End Sub