The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Problem and Solution: Weird MSMQ memory leak when calling Peek()

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Problem and Solution: Weird MSMQ memory leak when calling Peek() Posted: Apr 29, 2004 5:59 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Problem and Solution: Weird MSMQ memory leak when calling Peek()
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement
I was recently faced with the task of trying to figure out why, sporadically, when Calling an MSMQ's Peek() function, an application would loose memory for no apparent reason. It was pretty weird and hard to even recreate.
Searching google turned up this very helpful news group message which seems to have answered the problem:
 
 
----SNIP------------
If you try to peek or receive messages on a remote queue which does not exist (the queue or the machine) you
will loose memory for every try you do.
The bad part is that there is no direct way to check if a remote queue exists, but there is a workaround.

MessageQueue.Exists(queuename) only works for local queues. If you try to send a message to a queue using this
format name “FormatName:direct=os:<machinename>\private$\myqueue”  you will not get an error if the queue does
not exists. If you try to peek or receive you get an error, but at that point the memory is already lost.



Sample code to reproduce memory leak:

      MessageQueue mq = new MessageQueue("FormatName:direct=OS:dcc-roger-test\\private$\\1234");
      System.Messaging.Message msg = new System.Messaging.Message();                                 

      try                                                                                            
      {                                                                                              
            msg = mq.Peek(TimeSpan.FromTicks(2));                                                 
      }                                                                                              
      catch                                                                                          
      {                                                                                              
      }                                                                                              

      if(msg != null)                                                                                
      {                                                                                              
            msg.Dispose();                                                                           
      }                                                                                              

      mq.Close();                                                                                    
      mq.Dispose();                                                                                  

                                                                                                     
Forcing a GC.Collect / GC.WaitForPendingFinalizers / GC.GetTotalMemory(true) gives back a small portion of the lost memory.
Be aware of the virtual memory usage of your .net applications, in my example I had about 15mb in “mem usage” in Task Manager
and over 1 gb in virtual memory usage.

The workaround is simple, just make sure you can read from the queue before you try to peek / receive from it. CanRead returns
false if the queue does not exist or if you do not have rights to access it.

      if(mq.CanRead)                                                                                 
      {                                                                                              
            msg = mq.Peek(TimeSpan.FromTicks(2));                                                    
      }                                                                                              


Best Regards,
Roger Larsen
Dialogic Communications Corporation
----SNIP------------

Read: Problem and Solution: Weird MSMQ memory leak when calling Peek()

Topic: Got the Tablet Previous Topic   Next Topic Topic: SharePoint Workflow

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use