.NET中有两个Garbage Collector

一个叫Workstation Garbage Collector(MSCORWKS)、一个叫Server Garbage Collector(MSCORSVR).
Workstation Garbage Collector是为单处理器计算机设计的,而Server Garbage Collector为多处理器计算机设计的。
在ASP.NET中,如果服务器是多处理器,会自动使用Server Garbage Collector。
但正常的windows service 却只使用Workstation Garbage Collector, 即使是运行在多处理器计算机上。
如果解决这个问题呢?
1、新建一个环境变量: COMPLUS_BUILDFLAVOR , 并设置它的值为:SVR
2、在注册表新建这样一个键: HKLM/Software/Microsoft/COMPlus ,值为: “svr” 
也可以在代码中实现:
CComPtr spRuntimeHost;
 HRESULT hr = CorBindToRuntimeEx(NULL, //Retrieve latest version by default
         L"svr", //Request a Server (svr) or WorkStation (wks) build of the CLR
         STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN,
         CLSID_CorRuntimeHost,
         IID_ICorRuntimeHost,
         (void**)&spRuntimeHost);

 //Start the CLR
 hr = spRuntimeHost->Start();

 //Retrieve the IUnknown default AppDomain
 CComPtr pUnk;
 hr = spRuntimeHost->GetDefaultDomain(&pUnk);

 CComPtr<_AppDomain> spDefAppDomain;
 hr = pUnk->QueryInterface(&spDefAppDomain.p);

 // Create a domain setup
 CComPtr pUnkAppDomainSetup;
 hr = spRuntimeHost->CreateDomainSetup(&pUnkAppDomainSetup);

 CComPtr pAppDomainSetup;
 hr = pUnkAppDomainSetup->QueryInterface(__uuidof(IAppDomainSetup), (void**)&pAppDomainSetup);

 // Populate the domain setup
 hr = pAppDomainSetup->put_ApplicationBase(_bstr_t(appBase));

 hr = pAppDomainSetup->put_ConfigurationFile(_bstr_t(config));

 // Create a new domain based on the above setup
 CComPtr pNewAppDomain;
 LPWSTR pszTest = L"Test";
 spRuntimeHost->CreateDomainEx(pszTest, pAppDomainSetup, NULL, &pNewAppDomain);

 CComPtr<_AppDomain> spNewAppDomain;
 hr = pNewAppDomain->QueryInterface(&spNewAppDomain.p);

 CComPtr<_ObjectHandle> objectHandle;
 hr = spNewAppDomain->CreateInstance(_bstr_t(assemblyName), _bstr_t(assemblyType), &objectHandle);

/* IAppLoader is from an internal C# assemble generated via tlbexp*/

 VARIANT v;
 VariantInit(&v);
 hr = objectHandle->Unwrap(&v);

 _ASSERT(v.pdispVal);
 CComPtr pPR;
 hr = v.pdispVal->QueryInterface(__uuidof(IAppLoader), (void**) &pPR);
 _ASSERT(pPR);
 pPR->Run();
参考文章:
http://dotnetjunkies.com/WebLog/cibra/archive/2004/05/17/13785.aspx
http://weblogs.asp.net/mdavey/articles/79467.aspx

     

posted @ 2004-05-18 15:45  dudu  阅读(2127)  评论(1编辑  收藏  举报