Memory Manipulation

  • Thread starter Thread starter simchajoy2000
  • Start date Start date
S

simchajoy2000

Hi,

I have found that sometimes and usually on an inconsistent basis,
VB.NET projects will hold onto objects after I have disposed of them in
code doing something like:

object.close
object = nothing

A good example of this is an connection object to an Excel spreadsheet
- even after disposing of this object, the project is still holding
onto it and if you try to access the Excel file later, it tells you
that you can only view it in read-only mode because it is being
accessed elsewhere. This is just one example and I have noticed it
periodically with other objects as well. It's beginning to be a
problem and I was wondering if anyone knows how I could go into memory
directly and get rid of the object if it's still hanging out there
after I have disposed of it. Is that possible?

Thanks!

Joy
 
With an office object, use Marshal.ReleaseComObject to release the
reference. Be careful when manipulating COM interfaces to release any
references you generate.
 
From MSDN:

When you assign Nothing to an object variable, it no longer refers to any object
instance. If the variable had previously referred to an instance, setting it to
Nothing does not terminate the instance itself. The instance is terminated, and
the memory and system resources associated with it are released, only after the
garbage collector (GC) detects that there are no active references remaining.

The .NET Framework uses the reference-tracing garbage collection system to
periodically release unused resources. Visual Basic 6.0 and earlier versions
used a different system called reference counting to manage resources. Although
both systems perform the same function automatically, there are a few important
differences.

The CLR periodically destroys objects when the system determines that such
objects are no longer needed. Objects are released more quickly when system
resources are in short supply, and less frequently otherwise. The delay between
when an object loses scope and when the CLR releases it means that, unlike with
objects in Visual Basic 6.0 and earlier versions, you cannot determine exactly
when the object will be destroyed. In such a situation, objects are said to have
non-deterministic lifetime. In most cases, non-deterministic lifetime does not
change how you write applications, as long as you remember that the Finalize
destructor may not immediately execute when an object loses scope.

Another difference between the garbage-collection systems involves the use of
Nothing. To take advantage of reference counting in Visual Basic 6.0 and earlier
versions, programmers sometimes assigned Nothing to object variables to release
the references those variables held. If the variable held the last reference to
the object, the object's resources were released immediately. In Visual Basic
2005, while there may be cases in which this procedure is still valuable,
performing it never causes the referenced object to release its resources
immediately. To release resources immediately, use the object's Dispose method,
if available. The only time you should set a variable to Nothing is when its
lifetime is long relative to the time the garbage collector takes to detect
orphaned objects.
 
Excel is a complex beast. The only way to unload excel is to explicitley
close all workbooks and then issue a quit to excel itself.

dim xl as Excel.Application
xl as new Excel.Application

for each wbk as Excel.Workbook in xl
wbk.close
next
xl.quit
xl = Nothing

Note that I didn't declare xl with the New keyword. This is to prevent
Excel from being reloaded after the xl = Nothing.

Mike.
 
Back
Top