473,799 Members | 3,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"forcing" garbage collection

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. I'm wanting
to know if I can force it to happen immediately (when the object goes
out of scope) without going back and adding all of the required
Dispose() calls..

public Method()
{
CustomClass o = new CustomClass(..) ;
..
..
..
};

Is there a way to "force" the CustomClass destructor to be called right
when the object goes out of scope, by the same thread as the Method?

Thanks.
Jun 27 '08
11 1744
First, at some point every non-memory resource in dotNET is a OS
resource and closing it when done is a good thing to do.
Yes !

So Close/Dispose those.
The real problem on whether or not to use dispose is that the framework
has blurred the lines between managed and unmanaged. For someone who
has never written to the underlying OS, the fact that a lot of the
framework classes contains a Dispose method really doesn't have any
meaning because the non-memory OS resource is "hidden" from them.
If something implements IDisposable, then you can just call Dispose.

If it is a no-op then the overhead is very small.

Better safe than sorry.

Arne
Jun 27 '08 #11
Jon Mcleod wrote:
In this case, the object is a managed object but it contains a database
connection. This is an ASP.NET project, and there are two problems.
First, these objects are being left alive for quite some time, causing
the site to start artificially running out of database connections after
running a while. Secondly, GC happens on its own thread, causing a
"Internal .Net Framework Data Provider error 1." when it calls Close on
the the apparently single-threaded SqlConnection object.

My solution right now it to use IDisposed to explicitly manage the
lifetime of these objects, to sidestep the whole issue. I don't have a
lot of experience with these technologies, though. Is there a better way?
database =call Close or Dispose

If not then you will get problems !

Arne
Jun 27 '08 #12

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

Similar topics

11
3644
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from www.sysinternals.com, or even simply look at the Handle count in the good old windows TaskManager. To demonstrate the problem, all I did was created a basic Win Forms application and with Main implemented as:
4
1845
by: John Baker | last post by:
Hi: I have a query which supports a form. Te form is used to edit, update and change records in the table the query is based on. It all works fine EXCEPT that the "New" record (blank updatable record) always appears as the end of the form (i.e. the bottom), whereas it would be very helpful if it appeared at the beginning (the top of the form). Can anyone suggest a way to make this happen? Regards
46
2271
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have to send scanf the address of.." Isn't the lvalue called a POINTER TO and the (r)value called the ADDRESS OF?
2
3560
by: Earl Teigrob | last post by:
I have run into a situation where I need to run the !IsPostBack code under one circumstance, even if it is a postback. Something that may complicate matters more is that this is a double postback event. I have a filemanager type application that allows me to dynamically create a new folder on the web server. After the new folder is created, I need to reread the directory and display all the current folders in this directory to the user....
0
856
by: Mark | last post by:
At the top of my ASP.NET .aspx web page, I have several tags which I've included below. We store our ASP.NET projects in VSS. Every time I open up this project, VSS requires me to check out this page. Comparing the local file to the VSS copy, it keeps flipping the order of the register tags below. Any ideas on how to keep this from happening? Thanks in advance. -Mark <%@ Page language="c#" Codebehind="client_homepage.aspx.cs"...
51
3968
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
350
11910
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 one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
7
2450
by: MLH | last post by:
If I drop Like "*ABC*" in a QBE grid criteria cell, the records returned include mixed case. Can I force the uppercase limitation in a QBE grid?
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
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 we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.