Ali Hasan
Ali Hasan

Reputation: 1075

C# dynamic sitemap memory leak

I have user control for my sitemap which has following in ascx:

<asp:SiteMapPath id="SiteMapPath1"runat="server" RenderCurrentNodeAsLink="true" />

This sitemap control is called on each page of my website.

And the user control code behind:

private void Page_Load(object sender, EventArgs e)
{
    SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ExpandForumPaths);
}
private SiteMapNode ExpandForumPaths(Object sender, SiteMapResolveEventArgs e)
{
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    if (0 != postID)
    {
        tempNode.Url = tempNode.Url + "?PostID=" + postID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumID))
    {
        tempNode.Url = tempNode.Url + "?ForumID=" + forumID.ToString();
    }

    if ((null != (tempNode = tempNode.ParentNode)) &&
        (0 != forumGroupID))
    {
        tempNode.Url = tempNode.Url + "?ForumGroupID=" + forumGroupID.ToString();
    }

    return currentNode;
}

The problem is that it is consuming 1 - 2 mb of ram per page load and is not releasing the memory back. For example, if I refresh the page 2 mb is added to w3wp.exe, it keeps growing.

   SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ExpandForumPaths);

I do believe the line above is causing the problem by creating a new eventhandler every time my user control is used.

Question This method for making dynamic sitemap has failed. Is there any other way to do it without consuming memory, or am I doing something wrong?

Upvotes: 2

Views: 1463

Answers (3)

Kaido
Kaido

Reputation: 3951

You could keep your sitemap in one static cache and only store the current node id(s) against the user. 1-2mb of RAM suggests you are building a sitemap per user (let alone per request) which seems overkill.

Something like

var nodes = Session["ExpandedNodes"] as List<integer>
AddCurrentPageToExpandedNodes(nodes)
var siteMapForUser = StaticSiteMap.Apply(nodes)

Upvotes: 2

MethodMan
MethodMan

Reputation: 18843

have you thought about removing the handler as well with this line of code

SiteMap.SiteMapResolve -= this.ExpandForumPaths

Upvotes: 0

m0s
m0s

Reputation: 4280

http://netpl.blogspot.com/2008/04/sitemapresolve-and-dynamic-site-map.html

According to the article above, on every Page_Load a new SiteMapResolveEventHandler is added because SiteMapResolve is static, seems a good reason for unnecessary excessive memory allocation. Article also has a workaround.

Upvotes: 4

Related Questions