1, problem description
Recently in the development of C # AE, in the loop to get data and modify encountered two problems "out of the system resources" and "exceeded the maximum number of open cursors"; I read some information on the Internet and found that we didn't release objects that have been used in the loop, but in the loop there are actually null values for COM objects. But it still can't be solved. Later thought to assign the object to null and marshal is not the effect is not the same, deliberately wrote a simple loop to test, the code is as follows (beginner code, more chaotic, please light spray):
1 Public voidtest_ release cursor mode ()2 {3 stringSsname ="ControlPoint";//Layer name (there are more than 4w data)
Test the execution time of two release modes4DateTime Tmstart =DateTime.Now;5DisplayString ("start at:"+tmstart.tolongtimestring () +"\ r \ n");6 DateTime tmend;7 DateTime TmMiddle1;8 DateTime TmMiddle2;9 TimeSpan ts, Tsmax;
Specific execution CodeTenIfeatureworkspace pfeaws = Mc_ws asifeatureworkspace;//I have set up a good Sde Workspace object OneIfeatureclass pfeacls =Pfeaws.openfeatureclass (ssname); A stringStrwhere =string. Format ("Stationserieseventid in (' F9bd16ed-ae2a-454c-9eba-7123dc41af28 ', ' 7e3d0d4a-8c5e-49b5-8977-e060cd4cef6d ', ' A89300a5-3503-4976-b5d2-3d5a712f7b36 ')"); -Ifeaturecursor pcur =NULL; -IFeature Pfea =NULL; theIqueryfilter PFilter =NewQueryfilterclass (); - intNumcurhasbuild =0; - //int counsts = Pfeacls.featurecount (null);//total number of elements - Try + { -Tmstart =DateTime.Now; +DisplayString ("start at:"+ tmstart.tolongtimestring () +"\ r \ n"); ATsmax =Timespan.minvalue;
at intIdxstart =45809; - intIdxend =90416; - intIdxtmpnode = Idxstart + + - for(intIDX = Idxstart; IDX < Idxtmpnode; idx++) - { -TmMiddle1 =DateTime.Now; inStrwhere ="Objectid = '"+ idx. ToString () +"'"; -Pfilter.whereclause =strwhere;
Get Cursor Object toPcur = Pfeacls.search (PFilter,false);//If the cursor object is not disposed, thena cycle cannot exceed 280, or the ' Maximum number of open cursors ' error will explode +numcurhasbuild++;
Loop to get features within a cursor -Pfea =pcur.nextfeature (); the while(Pfea! =NULL) * { $ stringTMP = Pfea.get_value (1). ToString ();Panax NotoginsengPfea =pcur.nextfeature (); - } the //pcur = null;//Imagein this way, the object is actually not released, and will still be in the 283 article when the error + Marshal.ReleaseComObject (pcur);//This method can completely release the object, at this time can completely cycle through 4w data A localreleasecomobj (pcur); the if(Pcur! =NULL) + Localreleasecomobj (pcur);
$TmMiddle2 =DateTime.Now; $TS = tmMiddle2-TmMiddle1; - if(ts >Tsmax) -Tsmax =ts; the } -Tmend =DateTime.Now;WuyiDisplayString ("The most time-consuming times in a cycle are:"+tsmax.totalseconds+"\ r \ n"); theDisplayString ("perform a cycle; The total time consumed is:"+ (Tmend-tmstart). totalseconds+"\ r \ n"); - } Wu Catch(Exception ex) - { AboutDisplayString ("in section"+ numcurhasbuild.tostring () +"An error occurred at the place! \ r \ n"+Ex. Message); $ Throw NewException (ex. Message); - } -}
2. Test process
In the test, a control point layer is opened specifically (there are more than 4w data in it), and the cursor gets the object based on the condition loop;
Situation one: After the cursor is used, it is assigned a value of NULL, when the object is actually not released, so in 283 cycles will be the error ' Ora exceeds the maximum number of open cursors ';
Case TWO: When the cursor is used, at the end of the loop, using the Marshal.ReleaseComObject method to release the COM object, the loop can go through 4w times; I think the object was completely released.
At the same time, when using the Marshal.ReleaseComObject method, no extra time is added, the loop execution time is the same as not releasing the object (small data volume comparison).
3. Conclusion
When using the cursor object in Arcengine, be sure to dispose of the object after use, otherwise the above error will occur, and you need to use the Marshal.ReleaseComObject method to dispose of the object, the assignment of NULL is not up to the purpose;
Also say "Beyond the system resources" This error, this in the initial release of the object, each time when the amount of data more than 200 will be uncertain times this error, has been unable to solve But when I added the Marshal.ReleaseComObject method to the loop to release the object, the problem was somehow gone (and then I tried 4,000 data, and I could pass the test). The equivalent of resolving the "Maximum number of open cursors" issue, let go of another problem (perhaps I do not understand the specific principles of the period to be further).
This question for the time being written so much, and so on in-depth understanding, and then come back to add?!
The release of COM objects in C#+arcengine