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

Home Posts Topics Members FAQ

Garbage collection and new keyword in parameters

Hi all

I'm trying to better understand the behanviour of garbage collection
in .NET 2.0 and was wondering if anyone could shed some light on the
following:

If I have code something like

private void MyMethod(MyObj obj)
{
// Do something with obj
}

private void OtherMethod()
{
MyMethod(new MyObj);
// perhaps more code here
}

Assuming MyMethod uses the object, where is the point (I'm thinking in
terms of the equivalent to sequence points in the C++ language) at
which the object will be flagged for garbage collection? Is the above
code safe?

Many thanks in advance to anyone who may know the answer and reply.
Any references to MSDN pages would also be considered an added bonus
and very much appreciated.

Kind Regards

Anthony Smith

Aug 20 '07 #1
2 1209
Hi,

It seem that when it exit from the MyMethod method then it check the gc flag
for that object, why, becasue it have no root.
the new object (new MyObj) set by the method and live there, the new object
instance cannot be linked within the OtherMethod method.
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"atsmith6" wrote:
Hi all

I'm trying to better understand the behanviour of garbage collection
in .NET 2.0 and was wondering if anyone could shed some light on the
following:

If I have code something like

private void MyMethod(MyObj obj)
{
// Do something with obj
}

private void OtherMethod()
{
MyMethod(new MyObj);
// perhaps more code here
}

Assuming MyMethod uses the object, where is the point (I'm thinking in
terms of the equivalent to sequence points in the C++ language) at
which the object will be flagged for garbage collection? Is the above
code safe?

Many thanks in advance to anyone who may know the answer and reply.
Any references to MSDN pages would also be considered an added bonus
and very much appreciated.

Kind Regards

Anthony Smith

Aug 20 '07 #2
No doubt there are many more articles but the following is a good
start.
http://msdn.microsoft.com/msdnmag/issues/1100/gci/

In your example, as soon as MyMethod returns MyObj would be eligible
for garbage collection, and would be collected next time the gc ran.

Aug 20 '07 #3

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

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
5
by: rahul8143 | last post by:
hello, I read that when control moves out of function/program local variables dies automatically. then where garbage collection comes in picture? Is that needed because of memory leaks in...
2
by: Filippo | last post by:
I was told that if you pass a "ref param", the garbage collector does keep a reference and therefore you need to dispose by yourself of the object itself. Is that true ? Filippo
1
by: vinu | last post by:
Hi All, I am having a terrible problem with the garbage collector of the .NET run-time. My mangaged code(c#) application sends a System.String object to an unmanged world COM client function...
5
by: Carlos | last post by:
Hi all, I am from the C++ world, and just making baby steps into C#. Just wanted to ask you about the best way to create a dynamically sized array, and maintain it effectively (yeah, I know...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
1
by: petermichaux | last post by:
Hi, I'm working on a JavaScript drop-down menu and have come across a general question about garbage collection. Often in user interfaces you don't need to keep a reference to a object that is...
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 =...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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: 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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.