C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Releasing COM Objects from Memory


Posted Date: 09 Feb 2007    Resource Type: Articles    Category: .NET Framework
Author: Pandian SMember Level: Gold    
Rating: 1 out of 5Points: 10



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.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Releasing  .  Memory  .  COM Objects  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: OOPs In JavaScript
Previous Resource: Scrolling DataGrid Programatically
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use