Sharepoint习题——内存对象的释放

Question 4
You have a Web Part that contains the following code segment. (Line numbers are included for reference only.)
01 protected void Page_Load(object sender, EventArgs e)
02 {
03   SPSite site = new SPSite("
http://www.contoso.com/default.aspx");
04   {
05     SPWeb web = site.OpenWeb();
06    
07   }
08 }

You deploy the Web Part to a SharePoint site.
After you deploy the Web Part, users report that the site loads slowly. You need to modify the Web Part to prevent the site from loading slowly.
What should you do?
A. Add the following line of code at line 06:
web.Close();
B. Add the following line of code at line 06:
web.Dispose();
C. Add the following line of code at line 06:
site.Close();
D. Change line 03 to the following code segment:
using (SPSite site = new SPSite("http://www.contoso.com/default.aspx"))

解析:
 很明显,此题还是考的是SPSite,SPWeb对象内存的释放。
  由前面Question2,3的分析我们已经知道,使用Close方法与使用Dispose方法释放内存的区别。
  显然答案A,B,C只”分别” 释放了SPWeb对象或SPSite对象,而且释放的时机不对。
  我们通常是通过两种方式来处理此类代码
1. Try….Catch….Finally代码结构来实现内存的释放处理,
2. 也可以通过using()语句来自动实现内存的释放(事实上,系统在运行时会自动把Using代码块处理为Try….Catch…Finally代码块)。
http://msdn.microsoft.com/en-us/library/ee557362.aspx
所以本题目正确选项应该是D


Question 5
You create an event receiver.

The ItemAdded method for the event receiver contains the following code segment. (Line numbers are included for reference only.)

01 SPWeb recWeb = properties.Web;
02 using (SPSite siteCollection = new SPSite("http://site1/hr"))
03 {
04   using (SPWeb web = siteCollection.OpenWeb())
05   {
06     PublishingWeb oWeb = PublishingWeb.GetPublishingWeb(web);
07     PublishingWebCollection pubWebs = oWeb.GetPublishingWebs();
08     foreach (PublishingWeb iWeb in pubWebs)
09     {
10       try
11       {
12         SPFile page = web.GetFile("/Pages/default.aspx");
13         SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
14       }
15       finally
16       {
17         if (iWeb != null)
18         {
19           iWeb.Close();
20         }
21       }
22     }
23   }
24 }

You need to prevent the event receiver from causing memory leaks.
Which object should you dispose of?

A. oWeb at line 06
B. recWeb at line 01
C. wpManager at line 13
D. wpManager.Web at line 13

解析:
 对于选项A. oWeb对象是通过PublishingWeb.GetPublishingWeb方法获取的,这个对象并不是SPWeb对象,它是一个包裹了SPWeb对象集的对象实例,我们可以通过此实例的GetPublishingWebs方法进一步获取这包裹于其内的SPWeb对象集。并且通过遍历这个对象集来分别释放其内的SPWeb对象,如下面代码:

复制代码
using(SPWeb web = site.OpenWeb())
{
  PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
  PublishingWebCollection pubWebs = pubWeb.GetPublishingWebs());
  foreach(PublishingWeb innerPubWeb in pubWebs)
  {
    try
    {
       Process innerPubWeb
    }
    finally
    {
      innerPubWeb.Web.Dispose();
    }
  }
}
复制代码

而本题目也正是通过上述方式来实现的,所以选项A不会造成内存泄漏。
对于选项B. recWeb是通过properties对象的Web属性获取的, properties是Evenreceiver的传递参数。如下例:

private static void ItemAdded(SPItemEventProperties properties)       
{
    SPWeb web = properties.Web; 
}   

由此获取的SPWeb对象并不会造成内存的泄漏,因为它是由SPSite对象创建的,而此SPSite对象会在Event事件结束后自动被释放回收。

对于选项C,它并不是SPWeb对象,因此不存在要去释放它。
选项D为什么为造成内存泄漏问题呢,因为wpManager.Web是由page.GetLimitedWebPartManager方法返回的SPLimitedWebPartManager对象来获取的,这个SPLimitedWebPartManager对象实例包含了对SPWeb对象的内部引用, 并且在引用SPWeb对象之后并不会自动释放它,所以,wpManager.Web对象是会造成内存泄漏的。换句话说:当我们使用GetLimitedWebPartManager相关方法来获取SPWeb对象时,一定要注意内存泄漏问题。
因此,本题应该选 D
 
参考 :
http://msdn.microsoft.com/zh-tw/library/ms497306.aspx
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/8999f361-6e8e-452a-a42e-c8bf323c106e


Question 6
You create a console application to manage Personal Sites.
The application contains the following code segment. (Line numbers are included for reference only.)

01 SPSite siteCollection = new SPSite("http://moss");
02 UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));
03 UserProfile profile = profileManager.GetUserProfile("domain\\username");
04 SPSite personalSite = profile.PersonalSite;
05
06 siteCollection.Dispose();

You deploy the application to a SharePoint site.
After deploying the application, users report that the site loads slowly. You need to modify the application to prevent the site from loading slowly.
What should you do?
 
A. Remove line 06.
B. Add the following line of code at line 05:
personalSite.close();
C. Add the following line of code at line 05:
personalSite.Dispose();
D. Change line 06 to the following code segment:
siteCollection.close();

解析:
  选项A, 显然是错上加错,已经是由于SPSite造成的内存泄漏问题了,还在进一步扩大这个问题。
  选项 D. 显示无此必要,因为代码中已经有关于siteCollection的内存释放了,即:siteCollection.Dispose();
  选项 B与C:Close方法在这里并不是真正的完成了释放,因为personalSite并不是你新New的一个SPSite对象实例,见Question2,3解析,所以只能通过 Dispose方法才能真正释放SPSite的内存。
所以本题目正确选项应该是C

 
Question 7
You are creating a Web Part for SharePoint Server 2010.

The Web Part contains the following code segment. (Line numbers are included for reference only.)

01 protected override void CreateChildControls()
02 {
03   base.CreateChildControls();
04   SPSecurity.RunWithElevatedPrivileges(
05     delegate()
06     {
07       Label ListCount = new Label();
08       ListCount.Text = String.Format("There are {0} Lists", SPContext.Current.Web.Lists.Count);
09       Controls.Add(ListCount);
10     });
11 }

You need to identify which line of code prevents the Web Part from being deployed as a sandboxed solution.
Which line of code should you identify?
A.03
B. 04
C. 08
D. 09

解析
本题实质是考的关于Sandbox Solution的限制问题,Sandbox之所以安全,正是因为受限,所以它不支持通过RunWithElevatedPrivileges手段来提升代码的访问权限,因为如此一来就破坏了它的设计初衷。
Sandboxed solutions也不支持操作诸如下面的元素:
• Application Pages
• Custom Action Group
• Farm-scoped features
• HideCustomAction element
• Web Application-scoped features
• Workflows with code

所以本题目正确选项应该是B

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值