惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - Jason Li 2011

Maven - import a web project into eclipse [分享] 兰迪·波许教授的最后一课[PDF/PPT/AVI] 转:Controlling Access to Members of a Class 转:退火算法 Simulate Anneal Arithmetic (SAA,模拟退火算法) Scrum in practise 转:MOSS 2007 and Code Access Security 转:CollaDec FriendlyQuery类库(beta)说明 转:MOSS站点的迁移(备份还原) 转:MOSS漫游(3):说说MOSS中的母版页 转:关于MOSS 2007的Content Types 转:Best Practices: Common Coding Issues When Using the SharePoint Object Model 转:Best Practices: Using Disposable Windows SharePoint Services Objects 转:将你的Asp.NET应用程序嵌入到SharePoint Duff International Sites 项目总结 转:Code-blocks are not allowed in this file: Using Server-Side Code with SharePoint 转:ASP.NET Web Services or .NET Remoting: How to Choose 转:类与结构的差别 转:SharePoint Server 2007 页面模型 转:MOSS 2007基础:WSS 3.0 中的母版页(Master Pages)和内容页(Content Pages) 转:How to: Create a Minimal Master Page
转:Free the SPWeb
Jason Li 2011 · 2010-12-29 · via 博客园 - Jason Li 2011

Free the SPWeb!

29 Jun 2007 10:30 AM

  • Comments 7

Every SharePoint developer knows that you need to free resources that you have used... like SPWeb (and others too!). Most often freeing up the resources are easily managed with the nice using -statement like this:

1
2
3
4
5
6
7
using (SPSite site = new SPSite("http://localhost/"))
{
using (SPWeb web = site.OpenWeb("/"))
{
// TODO: Do something funny
}
}

But you can do that stuff manually with try-finally + <object>.Displose();. This is common .NET knowledge. But what happens if you don't do that? How much are we wasting resources if we just fail/forget to free objects? Well I tested this stuff a little bit and it was pretty suprising for me too... but before I'm going to give the results I'll show the examples I used:

1
2
3
4
5
6
7
8
9
10
using (SPSite site = new SPSite("http://localhost/"))
{
for (int i = 0; i < COUNT; i++)
{
using (SPWeb web = site.OpenWeb("/"))
{
// TODO: Do something funny
}
}
}

This one is the "normal" way to use SPWeb and SPSite. I'll call this  code 1.

1
2
3
4
5
6
7
8
9
using (SPSite site = new SPSite("http://localhost/"))
{
for (int i = 0; i < COUNT; i++)
{
SPWeb web = site.OpenWeb("/");
// TODO: Do something funny
// Missing: web.Dispose();
}
}

This one is _really_ bad example. We're not disposing SPWeb at all! I call this code 2 (cool and creative naming right!)

If you run those examples in loop we can get following results (Time is measured in seconds and memory usage is in kilos):

  Code 1 (good) Code 2 (bad)
Count Time Mem Usage Peak Mem Usage VM Size Time Mem Usage Peak Mem Usage VM Size
10 1,42 37 512 37 512 36 792 1,53 42 220 42 220 44 132
100 1,67 37 456 37 456 36 808 4,41 71 932 87 656 91 028
500 2,90 37 532 37 532 36 880 * * * *
1 000 5,00 37 612 37 612 36 888 * * * *
10 000 33,38 35 400 37 524 35 724 * * * *

Note: Memory consumption is just taken from Task Manager just after test code has finished. I haven't removed the test applications "base memory usage" (=usage before calling test code) but it was 8580K.

Note: The * indicates: Unhandled Exception: OutOfMemoryException.

So from numbers after the test run we can clearly see that code 2 is just slow and terrible memory slob! It can barely run few hundred loops before my server is "running low on memory".  And if you look at the memory usage from task manager you'll see something that can't be seen from the table:

Upper image is code 2 and lower one is code 1. So after startup code 1 stays steady even if you're running loop > 1000. And in upper image you can see increasing memory usage. If the loop count is 100 or less (=No OutOfMemoryException is happening on my system) then memory decreases since all of that memory isn't used anymore and this cannot be seenfrom the table above. So the spike usage of code 2 is huge for even in < 100 loops. And if you're doing  the same stuff over 1000 times it would suck up all memory you have in your system...

But if I have managed to scare you a little bit... that's good! But still don't start making too many disposes.... don't free up stuff that you haven't allocated yourself... like SPContext.Current.Web. It's allocated by SharePoint and if you dispose it you'll get exception. Here is example web part that will dispose if there is url parameter demanding it:

1
2
3
4
5
6
7
protected override void Render(HtmlTextWriter writer)
{
if (this.Page.Request["Dispose"] != null)
{
SPContext.Current.Web.Dispose();
}
}

And if I add that web part to page and use it normally it works fine. But when I add "Dispose=true" to the url, it will give you:

Microsoft.SharePoint.SPException: Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

So don't just free all disposable objects... think first but act second since if you don't act your application will definitely get those "Unhandled Exception: OutOfMemoryException" errors.

When you next time get OufOfMemoryException then check out your code before blaming the system... you might have some bugs in there.

Anyways... Happy hacking!

posted on 2010-12-29 22:41  Jason Li 2011  阅读(265)  评论()    收藏  举报