正确处置 SharePoint 对象?
我希望这里有一位 SharePoint 专家可以帮助解决这个问题。
问题就在这里。 我的 SharePoint 日志多次包含此行:
在此线程结束之前未释放 SPRequest 对象。 为了避免浪费系统资源,请在使用完该对象或其父对象(例如 SPSite 或 SPWeb)后立即对其进行处理。 现在将处理该对象。 分配 ID:{8D090AD2-5D55-42C2-9873-2D5486FE257C} 要确定此对象的分配位置,请在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings 中创建一个注册表项。 然后在该键下创建一个名为 SPRequestStackTrace 的新 DWORD,其值为 1。
我编辑了注册表并添加了该键,但找不到堆栈跟踪。 它不在 SharePoint 日志或事件查看器中。
我确实需要找到这些 SPSite/SPWeb 泄漏的根源并修复它们,但我不能只是开始编辑可能是也可能不是问题根源的代码。 有没有人有什么建议?
I hope there's a SharePoint expert here on SO who can help with this.
Here's the problem. My SharePoint logs contain this line, several times:
An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed. Allocation Id: {8D090AD2-5D55-42C2-9873-2D5486FE257C} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.
I edited the registry and added the key, but the stacktrace is nowhere to be found. It's not in the SharePoint logs or in the Event Viewer.
I really need to find the source of these SPSite/SPWeb leaks and fix them, but I can't just start editing code that may or may not be the source of the problem. Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
到目前为止,检查处置警告的最佳位置是:
http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns -by-example.aspx
在您的情况下,OpenWeb() 需要包装在 using 中。 如果您将其放置在您的 fianlly 块中,那么我建议您显示更多代码以查看您是否正在调用任何其他“陷阱”实例。 此外,如果某些 SPSite 和 SPWeb 对象是从 SPContext 类获取的,则无需处理它们。
如果您想跟踪对象的处置,您可以继承它们并重写 onload 和 ondispose 方法,以详细的消息传递方式记录它们。
阅读您的代码表明 SPWeb 对象是在 RunWithElevatedPrivileges 委托之外声明的。 这可能会影响 SharePoint 处理它们的方式。 通常建议对委托内的对象执行您需要执行的操作。
By far the best location for checking Disposal caveats is:
http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx
In your case OpenWeb() will need to be wrapped in a using. If you are disposing it in your fianlly block then I would suggest showing more code to see if you are calling any other "gotcha" instances. Also, it is unneccessary to dispose of certain SPSite and SPWeb objects if they are obtained from the SPContext class.
If you want to track disposals of the object you could inherit them and override the onload and ondispose methods to log them in a verbose messaging way.
Reading your code suggests that the SPWeb object is declared outside of the RunWithElevatedPriviledges delegate. This may have an effect on the way SharePoint disposes of them. It is generally suggested to do what you need to do to the object inside the delegate.
您需要重新启动受影响的进程(如果是 w3wp.exe 重新启动 IIS)以捕获注册表更改。
You need to restart the affected processes (if it is w3wp.exe restart IIS) to catch the registry change.
来自 http://msdn.microsoft.com/en-us/library/前面提到的aa973248.aspx链接:
鉴于您的示例代码,您仍然可能生成未释放的对象,因为 Dispose() 调用位于 finally 块中。
我的建议是将您的代码重新配置为以下内容:
如果您有多层Using语句,您可以像这样“堆叠”它们并减少代码缩进量(类似于if语句执行下一行或堵塞):
From the http://msdn.microsoft.com/en-us/library/aa973248.aspx link that was previously mentioned:
Given your example code, you could still be generating objects that do not get disposed because the Dispose() call is in the finally block.
My suggestion would be to reconfigure your code to the following:
If you have multiple layers of Using statements, you can 'stack' them like this and reduce the amount the code gets indented (similar to the way an if statment executes the next line or block):
我建议您阅读:
MSDN:最佳实践:使用 SharePoint 对象模型时的常见编码问题MSDN:最佳实践:使用一次性 Windows SharePoint Services 对象Chris O'Brien : 处置 SharePoint 对象 - 他们没有告诉您的内容
如果这些建议都不起作用, 看看 对 WSS v3 和 MOSS 2007 中的 SPSite/SPWeb 泄漏进行故障排除。
编辑:
您应该处置 SPSite 对象。 因为它会自动处理他的所有 SPWeb 对象。
I suggest you to take a read on :
MSDN : Best Practices: Common Coding Issues When Using the SharePoint Object ModelMSDN : Best Practices: Using Disposable Windows SharePoint Services ObjectsChris O'Brien : Disposing SharePoint objects - what they don't tell you
If none of these advice works. Take a look at Troubleshooting SPSite/SPWeb leaks in WSS v3 and MOSS 2007.
Edit :
You should dispose the SPSite object. Since it will dispose all his SPWeb object automatically.