As mentioned above, it is easy to leak memory when using spsite objects! The reason for Memory leakage is that the spsite object is not properly closed. Please review the code and correct it in time!
In the project, a large amount of exception information occurs during the stress test on the system, similar to the following:
Microsoft. sharepoint. spexception: attempted to make cballs on more than one thread in single threaded mode. (exception from hresult: 0x80010102 (rpc_e_attempted_multithread) ---> system. runtime. interopservices. comexception (0x80010102): attempted to make callon more than one thread in single threaded mode. (exception from hresult: 0x80010102 (rpc_e_attempted_multithread ))
At Microsoft. Sharepoint. Library. sprequestinternalclass. setvar (string bstrurl, string bstrname, string bstrvalue)
At Microsoft. Sharepoint. Library. sprequest. setvar (string bstrurl, string bstrname, string bstrvalue)
--- End of inner exception stack trace ---
At Microsoft. Sharepoint. Library. sprequest. setvar (string bstrurl, string bstrname, string bstrvalue)
At Microsoft. Sharepoint. splistitemcollection. ensurelistitemsdata ()
At Microsoft. Sharepoint. splistitemcollection. undirty ()
At Microsoft. Sharepoint. spbasecollection. system. Collections. ienumerable. getenumerator ()
At oa2.mossaccessblock. Portal. newshelper. getareanewsdom (string subtype, string area, int32 picturenum, int32 newsnum) in D: \ oa2_buildversion \ mossaccessblock \ portal \ newshelper. CS: Line 191
The preceding exception indicates that a multi-threaded call is attempted in single-threaded mode. During stress testing, such as "insufficient Server Memory", "unable to connect to the database server", and "unable to find the website) exceptions that do not occur during use, and within a period of time after these exceptions occur, the server will respond to http500 (the moss server is down ), if you are lucky, the server will automatically survive after the pressure falls for a while, which is amazing. If you are unlucky, the application pool will automatically stop, however, reset IIS is usually the case. This is because, without a lot of stress tests, it is not easy to discover.
After a large amount of exception information comparison, we found that the code in the exception will be associated with the creation of the spsite object. After reviewing the SDK, there is a saying like this:
If you create your own spsite object, you can use the dispose method or the close method to close the object. however, if you have a reference to a shared resource, such as when the object is provided by the spcontrol. getcontextsite method in a Web part, do not use either method to close the object. in scenarios where you have a reference to a shared resource, Instead let Windows SharePoint Services or your portal application manage the object. using either method on a shared resource causes an access violation error to occur.
There are two ways to obtain this object when using spsite: 1. New spsite (siteurl); 2. spcontrol. getcontextsite (this. Context)
The SDK means that when we use the first method to obtain the spsite, we can (it should be said that it is required) use the close () method or the dispose () method to close the object, and use getcontextsite () the obtained spsite object must not be closed using the two methods. Similar methods such as getcontextsite () are used. For example, the spsite object obtained from the current object cannot be closed, because it is a shared object. If it is forced to close, the program will go wrong (if it is verified, it will go wrong. The spsite of all codes is not closed at the beginning, because it will lead to program errors when it is closed, so I didn't close it, but I didn't know which method to get the spsite ).
Obviously, the shared object is theoretically superior to a new independently created object in terms of performance. At least, it will take less time to create an object and other operations, therefore, in a system with only one website set (such as the Baling petrochemical project), use the getcontextsite () method whenever possible to obtain the site set object spsite, in order to reduce the chance of errors and optimize the program performance.
What is the difference between spsite. Close () and spsite. Dispose? I have seen it before. net Book, close () is only to release the resources of the current object, and dispose () will call the dispose () method of its member object when releasing the current object resource, in order to release the resources of the member object, so dispose () is more thorough than close (). When looking for the cause of this thread exception, I also close () the dispose () method and the dispose () method are used for stress testing. After the close () method is used, the number of thrown thread exceptions is significantly lower than that when no method is used () method, the exception is no longer thrown. I think the reason is that the spsite object contains a spwebcollections object that contains all the sub-site point objects in the website set, like spweb and spsite, which are described as spsite In the SDK, so use spsite. the close () method is not thorough enough unless you manually dispose all spweb objects.
To sum up, when using the new method to create a spsite object independently, it must be written in a format similar to the following:
Using (spsite site = new spsite (siteurl or siteguid ))
{
// Your code here
}
Or
Spsite site = NULL;
Try
{
Site = new spsite (siteurl or siteguid );
// Your code here
}
Catch {}
Finially
{
Site. Dispose ();
}
Finally, let's talk about the privileged code in Moss, which is to run a program segment as a system account. Let's look at the following code:
Spsecurity. runwithelevatedprivileges (delegate ()
{
Using (spsite site = new spsite (Web. Site. ID ))
{
// Do things assuming the permission of the "System Account"
}
});
When using privileged code, you must note that the spsite and spweb objects in the privileged code must be independently created and cannot use shared objects. Otherwise, an exception is thrown, that is to say, you cannot write the following code for imaging:
Spsecurity. runwithelevatedprivileges (delegate ()
{
Using (spsite site = spcontrol. getcontextsite (this. Context ))
{
// Do things assuming the permission of the "System Account"
}
});
Please pay attention when developing Moss. If there are any mistakes or omissions, please point out, thank you!