It is well known that in the .NET world, the programmer is only responsible for the use of NEW to create objects, while the destruction of the object is completely handed over to the garbage collector, and only when garbage is recycled, the type of .NET will be destroyed. This usually doesn't cause anything wrong. However, when using an unmanaged COM object, a special problem will be brought.
COM uses a reference count to determine the living period of the object, when COM customers receive the object each time,
IUNKNOWN-> addRef (), while each time the object is released,
IUNKNOWN-> Release (), once the reference count reaches zero, release the instance.
The problem is produced, let us look at the following code:
This is a C # code that exports the Excel file to the client using the Excel COM component to export the EXCEL file to the client's C # code, and run the wizard to add COM reference before adding this code.
Excel.Application OEXCEL; Excel.Workbook Obook; Object Omiussing
=
System.Reflection.Missing.Value; OEXCEL
=
New
Excel.Application (); OBOOK
=
OEXCEL.WORKBOOKS.ADD (OMISSING);
for
(
int
i
=
1
i
<=
4
i
)
{OEXCEL.CELLS [I, 1] = i.toString (); OEXCEL.CELLS [I, 2] = "'bb2"; OEXCEL.CELLS [I, 3] = "' CCC3"; OEXCEL.CELLS [i, 4] = "'aaa4";
Obook.saved
=
True
OEXCEL.USERCONTROL
=
False
;
String
Filename
=
Datetime.now.ticks.tostring ();
String
mm
=
Server.mappath (
"
.
"
)
"
//
"
Filename
"
.xls
"
;
//
Server saved address
OEXCEL.ActiveWorkbook.savecopyas (mm); OEXCEL.QUIT ();
//
Gc.collect ();
Response.Redirect (filename
"
.xls
"
);
This code can realize the function of exporting files, but if you look at the Windows Task Manager, you will find the wonderful scene below.
So, someone added a "gc.collect ();", very good, Excel.exe is not so much, as shown below. However, how can I completely release it?