473,466 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

garbage collection of open resources

Everything I have read suggests that if I open files or database connections
or the like, that I should explicitly close those said resources before
making the object subject to GC. This is obviously good programming
technique.

However what happens if this practice is not followed. e.g.
that I have a base object that creates an instance of a filestream and for
arguements sake an instance of a oledb connection.
Both sub objects are opened (one to a file and one to a db) and used to
read/ write data.

Say for instance that I implement the IDisposible interface for the parent
class definition (that instanciates both objects) and within the Dispose()
method I set both object variable references to null (without closing them).
I am under the impression that they will be made available to the GC.

My question is - could data corruption occur to any open data sources that
have been made available to garbage collection OR does (as I suspect) it
simply means that those open resources (open files) will simply be locked
out until the GC destroys them?

Obviously I know that there are several reasons for closing the resources
but I would like to know that corruption is not one of them.
--

Br,
Mark Broadbent
mcdba , mcse+i
=============
Nov 16 '05 #1
6 1361
Eventually you might run out of resources. In this case two things could
happen:
1. When you next go to use that file, it may say that it's locked or already
open.
2. Opening a new file will might throw an exception stating that there are
too many open files.

The GC does dispose of all objects eventually, and even if you set the
reference to null and forget to close the file handle, the file stream
itself implements IDisposable and will close the file for you. Just that it
won't happen until potentially much later, leaving the option for open
handles to accumulate.

"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Everything I have read suggests that if I open files or database connections or the like, that I should explicitly close those said resources before
making the object subject to GC. This is obviously good programming
technique.

However what happens if this practice is not followed. e.g.
that I have a base object that creates an instance of a filestream and for
arguements sake an instance of a oledb connection.
Both sub objects are opened (one to a file and one to a db) and used to
read/ write data.

Say for instance that I implement the IDisposible interface for the parent
class definition (that instanciates both objects) and within the Dispose()
method I set both object variable references to null (without closing them). I am under the impression that they will be made available to the GC.

My question is - could data corruption occur to any open data sources that
have been made available to garbage collection OR does (as I suspect) it
simply means that those open resources (open files) will simply be locked
out until the GC destroys them?

Obviously I know that there are several reasons for closing the resources
but I would like to know that corruption is not one of them.
--

Br,
Mark Broadbent
mcdba , mcse+i
=============

Nov 16 '05 #2
John Wood wrote:
Eventually you might run out of resources. In this case two things could
happen:
1. When you next go to use that file, it may say that it's locked or already
open.
2. Opening a new file will might throw an exception stating that there are
too many open files.

The GC does dispose of all objects eventually, and even if you set the
reference to null and forget to close the file handle, the file stream
itself implements IDisposable and will close the file for you. Just that it
won't happen until potentially much later, leaving the option for open
handles to accumulate.
Another thing to be aware of is that on application shutdown finalizers
are not necessarily called. So, the file close will occur (as the OS
does this on process shutdown) but any buffered data might not be
flushed to the file.

Compile and run the following with no command line arguments to see this
behavior (run with "dispose" or "close" on the command line to get the
data flushed):

//================================================== ========
using System;
using System.IO;
using System.Text;

public class MyClass
{
public static void Main(string[] args)
{
string arg = null;

if (args.Length > 0) {
arg = args[0].ToLower();
}

StreamWriter sw = new StreamWriter( @"c:\temp\swtest.out");

sw.WriteLine( "Does this data actually make it into the file?");
sw.Write( "It depends...");

if (arg != null) {
switch (arg) {
case "dispose":
((IDisposable) sw).Dispose();
break;
case "close":
sw.Close();
break;
default:
// do nothing
break;
}
}
}
}
//================================================== ========

"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Everything I have read suggests that if I open files or database


connections
or the like, that I should explicitly close those said resources before
making the object subject to GC. This is obviously good programming
technique.

However what happens if this practice is not followed. e.g.
that I have a base object that creates an instance of a filestream and for
arguements sake an instance of a oledb connection.
Both sub objects are opened (one to a file and one to a db) and used to
read/ write data.

Say for instance that I implement the IDisposible interface for the parent
class definition (that instanciates both objects) and within the Dispose()
method I set both object variable references to null (without closing


them).
I am under the impression that they will be made available to the GC.

My question is - could data corruption occur to any open data sources that
have been made available to garbage collection OR does (as I suspect) it
simply means that those open resources (open files) will simply be locked
out until the GC destroys them?

Obviously I know that there are several reasons for closing the resources
but I would like to know that corruption is not one of them.
--

Br,
Mark Broadbent
mcdba , mcse+i
=============


--
mikeb
Nov 16 '05 #3
That's actually very interesting -- you'd think that the CLR would ask the
GC to finalize everything before it terminates... that's kind-of a bug don't
you think?

"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:eE*************@TK2MSFTNGP11.phx.gbl...
John Wood wrote:
Eventually you might run out of resources. In this case two things could
happen:
1. When you next go to use that file, it may say that it's locked or already open.
2. Opening a new file will might throw an exception stating that there are too many open files.

The GC does dispose of all objects eventually, and even if you set the
reference to null and forget to close the file handle, the file stream
itself implements IDisposable and will close the file for you. Just that it won't happen until potentially much later, leaving the option for open
handles to accumulate.


Another thing to be aware of is that on application shutdown finalizers
are not necessarily called. So, the file close will occur (as the OS
does this on process shutdown) but any buffered data might not be
flushed to the file.

Compile and run the following with no command line arguments to see this
behavior (run with "dispose" or "close" on the command line to get the
data flushed):

//================================================== ========
using System;
using System.IO;
using System.Text;

public class MyClass
{
public static void Main(string[] args)
{
string arg = null;

if (args.Length > 0) {
arg = args[0].ToLower();
}

StreamWriter sw = new StreamWriter( @"c:\temp\swtest.out");

sw.WriteLine( "Does this data actually make it into the file?");
sw.Write( "It depends...");

if (arg != null) {
switch (arg) {
case "dispose":
((IDisposable) sw).Dispose();
break;
case "close":
sw.Close();
break;
default:
// do nothing
break;
}
}
}
}
//================================================== ========

"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Everything I have read suggests that if I open files or database


connections
or the like, that I should explicitly close those said resources before
making the object subject to GC. This is obviously good programming
technique.

However what happens if this practice is not followed. e.g.
that I have a base object that creates an instance of a filestream and forarguements sake an instance of a oledb connection.
Both sub objects are opened (one to a file and one to a db) and used to
read/ write data.

Say for instance that I implement the IDisposible interface for the parentclass definition (that instanciates both objects) and within the Dispose()method I set both object variable references to null (without closing


them).
I am under the impression that they will be made available to the GC.

My question is - could data corruption occur to any open data sources thathave been made available to garbage collection OR does (as I suspect) it
simply means that those open resources (open files) will simply be lockedout until the GC destroys them?

Obviously I know that there are several reasons for closing the resourcesbut I would like to know that corruption is not one of them.
--

Br,
Mark Broadbent
mcdba , mcse+i
=============


--
mikeb

Nov 16 '05 #4

"John Wood" <j@ro.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
That's actually very interesting -- you'd think that the CLR would ask the
GC to finalize everything before it terminates... that's kind-of a bug don't you think?


It does, there's just no guarantee. If finalizers take too long or create
too many new objects the CLR will give up and shut down the process without
finalizing all the objects.

David
Nov 16 '05 #5
thx Mike/ John!

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:eE*************@TK2MSFTNGP11.phx.gbl...
John Wood wrote:
Eventually you might run out of resources. In this case two things could
happen:
1. When you next go to use that file, it may say that it's locked or already open.
2. Opening a new file will might throw an exception stating that there are too many open files.

The GC does dispose of all objects eventually, and even if you set the
reference to null and forget to close the file handle, the file stream
itself implements IDisposable and will close the file for you. Just that it won't happen until potentially much later, leaving the option for open
handles to accumulate.


Another thing to be aware of is that on application shutdown finalizers
are not necessarily called. So, the file close will occur (as the OS
does this on process shutdown) but any buffered data might not be
flushed to the file.

Compile and run the following with no command line arguments to see this
behavior (run with "dispose" or "close" on the command line to get the
data flushed):

//================================================== ========
using System;
using System.IO;
using System.Text;

public class MyClass
{
public static void Main(string[] args)
{
string arg = null;

if (args.Length > 0) {
arg = args[0].ToLower();
}

StreamWriter sw = new StreamWriter( @"c:\temp\swtest.out");

sw.WriteLine( "Does this data actually make it into the file?");
sw.Write( "It depends...");

if (arg != null) {
switch (arg) {
case "dispose":
((IDisposable) sw).Dispose();
break;
case "close":
sw.Close();
break;
default:
// do nothing
break;
}
}
}
}
//================================================== ========

"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
Everything I have read suggests that if I open files or database


connections
or the like, that I should explicitly close those said resources before
making the object subject to GC. This is obviously good programming
technique.

However what happens if this practice is not followed. e.g.
that I have a base object that creates an instance of a filestream and forarguements sake an instance of a oledb connection.
Both sub objects are opened (one to a file and one to a db) and used to
read/ write data.

Say for instance that I implement the IDisposible interface for the parentclass definition (that instanciates both objects) and within the Dispose()method I set both object variable references to null (without closing


them).
I am under the impression that they will be made available to the GC.

My question is - could data corruption occur to any open data sources thathave been made available to garbage collection OR does (as I suspect) it
simply means that those open resources (open files) will simply be lockedout until the GC destroys them?

Obviously I know that there are several reasons for closing the resourcesbut I would like to know that corruption is not one of them.
--

Br,
Mark Broadbent
mcdba , mcse+i
=============


--
mikeb

Nov 16 '05 #6
David Browne wrote:
"John Wood" <j@ro.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
That's actually very interesting -- you'd think that the CLR would ask the
GC to finalize everything before it terminates... that's kind-of a bug


don't
you think?

It does, there's just no guarantee. If finalizers take too long or create
too many new objects the CLR will give up and shut down the process without
finalizing all the objects.


While not being guaranteed is one thing, it actually *appears* to make
little or no effort. Maybe there's something in the sample that would
cause the finalization of the StreamWriter (or some other object that I
don' know about) to take too long, but I don't know what it is and if
there's any guidance about it I'd appreciate a pointer.

In practice, this means that you'd better make sure your code calls
Close() or Dispose() (either directly or via a 'using' statement) on
buffered file objects (and other resources, too, I'm sure). The
fallback that the GC will eventually take care of Disposing objects that
your code didn't handle explicitly does not seem to hold for a
significant situation (clean process termination).

Clearly, not calling Dispose() on a disposable object is a bug in the
application; however, I just wanted to point out that resource leakage
while the app is running is not the only side-effect of neglecting
Dispose() - data could be lost as well.
--
mikeb
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found...
1
by: IDoNothing | last post by:
How does garbage collection work for custom objects instantiated during a Page_Load or during some other processing of an ASP.NET page? Are objects garbage-collected in the same manner using...
8
by: Bijoy Naick | last post by:
Just wondering if anyone has experienced any issues with garbage collection in .net. We developed an application using VB .NET; many of the pages made db calls. One of the developers forgot to...
7
by: Simon Verona | last post by:
I have a problem in my application which I believe is due to open handles.. . The symptom that users report is that after they have been using the application for a while, it will randomly just...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
11
by: Jon Mcleod | last post by:
I'm trying to wrap my ming around C#/CLR garbage collection. In an ASP.NET project, I'm having a problem because an object destructor is being called by another thread, long after my code is done....
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.