Is there a way in C# to stop garbage collector from killing our object
automatically.I'd like to kill it myself??
Is there away to to do so?
Thanks 10 2266
I forgot to ask what happenes in this scenario.
ArrayList first = new ArrayList();
ArrayList second = first
first.Dispose()
Dose these three lines of code makes second a useless variable poiting to no
where?
Thanks
"Ray5531" <Ra*****@microsoft.com> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl... Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself??
Is there away to to do so? Thanks
A couple of things to note here.
1. You're missing a semicolon at the end of the second and third lines, so
you won't compile =)
2. The ArrayList object doesn't have a Dispose method.
3. Assuming instead that you set it to null, "first" would then indeed be
null, but "second" would still be the ArrayList instance. Essentially,
you're removing the pointer from "first" to the object. The object doesn't
get destroyed/collected/whatever, because "second" now has a reference to
it.
Also, the garbage collector isn't called "the object snatcher" for a reason.
It's only going to collect garbage (space you aren't using), not stuff that
you are still using.
Hope this helps,
Dan Cox
DISCLAIMER: I didn't write it, just trying to help! =)
"Ray5531" <Ra*****@microsoft.com> wrote in message
news:uQ*************@TK2MSFTNGP15.phx.gbl... I forgot to ask what happenes in this scenario.
ArrayList first = new ArrayList();
ArrayList second = first
first.Dispose()
Dose these three lines of code makes second a useless variable poiting to no where? Thanks
"Ray5531" <Ra*****@microsoft.com> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself??
Is there away to to do so? Thanks
Is there a way to manually grage collect an object?
Thanks
"CSharper" <do**@botherme.com> wrote in message
news:OL********************@bignews4.bellsouth.net ... A couple of things to note here.
1. You're missing a semicolon at the end of the second and third lines, so you won't compile =) 2. The ArrayList object doesn't have a Dispose method. 3. Assuming instead that you set it to null, "first" would then indeed be null, but "second" would still be the ArrayList instance. Essentially, you're removing the pointer from "first" to the object. The object doesn't get destroyed/collected/whatever, because "second" now has a reference to it.
Also, the garbage collector isn't called "the object snatcher" for a reason. It's only going to collect garbage (space you aren't using), not stuff that you are still using.
Hope this helps,
Dan Cox DISCLAIMER: I didn't write it, just trying to help! =)
"Ray5531" <Ra*****@microsoft.com> wrote in message news:uQ*************@TK2MSFTNGP15.phx.gbl...I forgot to ask what happenes in this scenario.
ArrayList first = new ArrayList();
ArrayList second = first
first.Dispose()
Dose these three lines of code makes second a useless variable poiting to no where? Thanks
"Ray5531" <Ra*****@microsoft.com> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself??
Is there away to to do so? Thanks
Accrding to this article : http://msdn.microsoft.com/library/de...posemethod.asp
Dispose(True) ,Managed and unmanaged resources
can be disposed,dose it include the object itself(if there is no reference
to it?
Thanks
"CSharper" <do**@botherme.com> wrote in message
news:OL********************@bignews4.bellsouth.net ... A couple of things to note here.
1. You're missing a semicolon at the end of the second and third lines, so you won't compile =) 2. The ArrayList object doesn't have a Dispose method. 3. Assuming instead that you set it to null, "first" would then indeed be null, but "second" would still be the ArrayList instance. Essentially, you're removing the pointer from "first" to the object. The object doesn't get destroyed/collected/whatever, because "second" now has a reference to it.
Also, the garbage collector isn't called "the object snatcher" for a reason. It's only going to collect garbage (space you aren't using), not stuff that you are still using.
Hope this helps,
Dan Cox DISCLAIMER: I didn't write it, just trying to help! =)
"Ray5531" <Ra*****@microsoft.com> wrote in message news:uQ*************@TK2MSFTNGP15.phx.gbl...I forgot to ask what happenes in this scenario.
ArrayList first = new ArrayList();
ArrayList second = first
first.Dispose()
Dose these three lines of code makes second a useless variable poiting to no where? Thanks
"Ray5531" <Ra*****@microsoft.com> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself??
Is there away to to do so? Thanks
From MSDN:
In scenarios where resources must be released at a specific time, classes
can implement the IDisposable interface, which contains the
IDisposable.Dispose method that performs resource management and cleanup
tasks. Classes that implement Dispose must specify, as part of their class
contract, if and when class consumers call the method to clean up the
object. The garbage collector does not, by default, call the Dispose method;
however, implementations of the Dispose method can call methods in the GC
class to customize the finalization behavior of the garbage collector.
So, your best bet (as I understand it) is to implement IDisposable on you
class and call the Dispose method when you're ready for your object to be
collected. That WILL NOT force the Garbage Collector to collect your object.
Need more detail? Here's the best I've seen: http://www.bluebytesoftware.com/blog...3-20c06ae539ae
Hope this helps,
Dan Cox
"Ray5531" <Ra*****@microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Is there a way to manually grage collect an object? Thanks
Yup. This really details it: http://www.bluebytesoftware.com/blog...3-20c06ae539ae
"Ray5531" <Ra*****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Accrding to this article : http://msdn.microsoft.com/library/de...posemethod.asp
Dispose(True) ,Managed and unmanaged resources can be disposed,dose it include the object itself(if there is no reference to it?
Thanks
"CSharper" <do**@botherme.com> wrote in message news:OL********************@bignews4.bellsouth.net ...A couple of things to note here.
1. You're missing a semicolon at the end of the second and third lines, so you won't compile =) 2. The ArrayList object doesn't have a Dispose method. 3. Assuming instead that you set it to null, "first" would then indeed be null, but "second" would still be the ArrayList instance. Essentially, you're removing the pointer from "first" to the object. The object doesn't get destroyed/collected/whatever, because "second" now has a reference to it.
Also, the garbage collector isn't called "the object snatcher" for a reason. It's only going to collect garbage (space you aren't using), not stuff that you are still using.
Hope this helps,
Dan Cox DISCLAIMER: I didn't write it, just trying to help! =)
"Ray5531" <Ra*****@microsoft.com> wrote in message news:uQ*************@TK2MSFTNGP15.phx.gbl...I forgot to ask what happenes in this scenario.
ArrayList first = new ArrayList();
ArrayList second = first
first.Dispose()
Dose these three lines of code makes second a useless variable poiting to no where? Thanks
"Ray5531" <Ra*****@microsoft.com> wrote in message news:O3**************@TK2MSFTNGP09.phx.gbl... Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself??
Is there away to to do so? Thanks
Ray5531 <Ra*****@microsoft.com> wrote: Accrding to this article : http://msdn.microsoft.com/library/de...ary/en-us/cpgu ide/html/cpconimplementingdisposemethod.asp
Dispose(True) ,Managed and unmanaged resources can be disposed,dose it include the object itself(if there is no reference to it?
No. There is no way of manually collecting an object, other than by
calling GC.Collect to force a garbage collection.
Why do you think you need to do it anyway?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi Skeet.
I was pretty sure that if I raise this duiscussion you would pop up soon.I
still have the discussion about reference types and Value types and your
supper article in that regards as well.You did a great job man! I love your
article.
Jon,by the way,can u tell me why pointers in C# can not refer to reference
types where as in your article I remember that you mentioned a refernce type
can refer to another reference type which mens it simply stores the address
of the other object while its own address is kept on the heap. What's
reallly the difference between Reference types and pointers in C#? only the
fact that pointers are unsafe and reference type are safe?That's it?
Thanks Mr.Memory geek;-)
Reza
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Ray5531 <Ra*****@microsoft.com> wrote: Accrding to this article : http://msdn.microsoft.com/library/de...ary/en-us/cpgu ide/html/cpconimplementingdisposemethod.asp
Dispose(True) ,Managed and unmanaged resources can be disposed,dose it include the object itself(if there is no reference to it?
No. There is no way of manually collecting an object, other than by calling GC.Collect to force a garbage collection.
Why do you think you need to do it anyway?
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Ray5531 <Ra*****@microsoft.com> wrote: I was pretty sure that if I raise this duiscussion you would pop up soon.I still have the discussion about reference types and Value types and your supper article in that regards as well.You did a great job man! I love your article.
Goodo :)
Jon,by the way,can u tell me why pointers in C# can not refer to reference types where as in your article I remember that you mentioned a refernce type can refer to another reference type which mens it simply stores the address of the other object while its own address is kept on the heap. What's reallly the difference between Reference types and pointers in C#? only the fact that pointers are unsafe and reference type are safe?That's it?
Pointers are indeed unsafe, and aren't tracked by the garbage
collector.
You also can't have a pointer to a managed type - pointers have to be
to value types which don't themselves contain any reference types
(however indirectly).
Personally I don't think I've ever used pointers in C#...
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bob |
last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
|
by: pachanga |
last post by:
The Hans-Boehm garbage collector can be successfully used with C and
C++, but not yet a standard for C++.. Is there talks about Garbage
Collector to become in the C++ standard?
|
by: Vadym Stetsyak |
last post by:
Hi there!
Is there any way how can I stop garbage collection for a period of time.
Lets say, I have a form. Before form load I pause the GC and when the form
is shown resume GC
|
by: Mingnan G. |
last post by:
Hello everyone.
I have written a garbage collector for standard C++ application. It has
following main features.
1) Deterministic Finalization
Providing deterministic finalization, the system...
|
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...
|
by: jacob navia |
last post by:
Abstract
--------
Garbage collection is a method of managing memory by using a "collector"
library. Periodically, or triggered by an allocation request, the
collector looks for unused memory...
|
by: Paul.Lee.1971 |
last post by:
Hi everyone,
A program that I'm helping to code seems to slow down drastically
during initialisation, and looking at the profiling graph, it seems to
be the garbage collector thats slowing things...
|
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 =...
|
by: Carlo Milanesi |
last post by:
Hello,
traditionally, in C++, dynamically allocated memory has been
managed explicitly by calling "delete" in the application code.
Now, in addition to the standard library strings, containers,...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |