Releasing COM Objects from Memory :
We have created an instance of Excel in VB.NET has a habit of hanging around once we've finished with them. OK
But, Is there any big issue? , YES.
In VB6.0 , Set Obj = NOTHING, Will release the memory. So why doesn't work for .NET ?.
Referencing these objects within our .NET applications is that the actual COM code and its executable is outside of the managed. So, Garbage Collector will not be responsible for this kind of memory managements.
Runtime Callable Wrapper :
When reference the COM objects in our .NET, automatically wraps those references in something called RCW. 'UNMANAGED' objects dealt with within the 'MANAGED' .NET environment thru the RCW(Runtime Callable Wrapper).
The only concession is that we must release each RCW as part of our cleanup process through the method named "Marshal.ReleaseComObject" available in "System.Runtime.InteropServices" namespace.
WE can rely on the garbage collector to automatically perform the necessary memory management tasks. However, unmanaged resources require explicit cleanup.
Code Sample :
Private objEx As Excel.Application
Private objWB As Excel.Workbook
Private objWS As Excel.Worksheet
objEx = New Excel.Application()
objWB = objEx.Workbooks.Add()
objWS = objWB.Worksheets.Add
objEx.Visible = True
Dim intRow As Integer
For intRow = 1 To 7
objWS.Range("A" & intRow).Value = Date.Today.AddDays(intRow).ToString("dddd")
Next
It Release the Objects from the Memory , But not from the Task Manager. (Each time we are closing the application then the following "ReleaseComObject" only enough other wise "Gc.Collect()" is also needed.)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objEx) System.Runtime.InteropServices.Marshal.ReleaseComObject(objWB) System.Runtime.InteropServices.Marshal.ReleaseComObject(objWS)
It Force to Release the Objects from the Task Manager Also. ( It is best to use when the application is open, But we want to release the Object from Memory & Task Manager)
Gc.Collect()
Note : Better to use this method to force the system to attempt to reclaim the maximum amount of available memory.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|