473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

KeepAlive question...

Hi!

I was looking at the details of how KeepAlive
works to handle my unmanaged code interop.
Several question came up. Assuming I have:

public Test2(Object a)
{

}

public Test()
{
a = new MyObject();

Test2(a);
}

Is the call to Test2 the same as the call to GC.KeepAlive?
Can GC collect the object while one of its methods is executing,
if the object is not referenced within that method and after?

If Inside Test2 I make a reference to "a", can GC collect
the "a" immadiately after the reference, or only after Test2 finishes?

Does the behaviour change if I write:

public Test()
{
Test2(new MyObject());
}

Thanks!
Atmapuri

Mar 5 '07 #1
2 2545
In article <#s**************@TK2MSFTNGP05.phx.gbl>, di*@community.nospam
says...
Hi!

I was looking at the details of how KeepAlive
works to handle my unmanaged code interop.
Several question came up. Assuming I have:

public Test2(Object a)
{

}

public Test()
{
a = new MyObject();

Test2(a);
}

Is the call to Test2 the same as the call to GC.KeepAlive?
It might have the same effect. Don't know for sure.
Can GC collect the object while one of its methods is executing,
if the object is not referenced within that method and after?
Hmmmm... Perhaps. Maybe the JIT compiler will optimize out the passing
of the parameter since it's never used. In that case, I would think
"a" could go away anytime after the "a = new MyObject" line. But this
is theoretical -- I don't know for sure.
If Inside Test2 I make a reference to "a", can GC collect
the "a" immadiately after the reference, or only after Test2 finishes?
At a minimum, it couldn't collect it until Test2 is done with it -- that
may be when Test2 finishes, or not.

--
Patrick Steele
http://weblogs.asp.net/psteele
Mar 6 '07 #2
On Mar 5, 6:38 am, "Atmapuri" <d...@community.nospamwrote:
Hi!

I was looking at the details of how KeepAlive
works to handle my unmanaged code interop.
Several question came up. Assuming I have:

public Test2(Object a)
{

}

public Test()
{
a = new MyObject();

Test2(a);

}

Is the call to Test2 the same as the call to GC.KeepAlive?
A call to any method usually behaves the same way as GC.KeepAlive.
But, in this particular case the JIT compiler could inline Test2,
determine that it has no side-effects on 'a', and make the new
MyObject eligible for collection even before 'a' is assigned.
Can GC collect the object while one of its methods is executing,
if the object is not referenced within that method and after?
Yes. An object can be collected while one of it's methods is
executing. It may seem counter intuitive, but it's perfectly safe
(assuming all code is managed of course) if the method doesn't
reference its owner (eg. extracting an instance variable from the
'this' pointer).
>
If Inside Test2 I make a reference to "a", can GC collect
the "a" immadiately after the reference, or only after Test2 finishes?
I don't know if the GC makes a distinction between method parameters
and other variables on the stack. But, I suspect it would be eligible
after it's last use like other local variables.
>
Does the behaviour change if I write:

public Test()
{
Test2(new MyObject());

}
I don't think the behavior would be any different from your first
example.

Mar 6 '07 #3

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

Similar topics

0
by: unni | last post by:
Whats The Other Option For Keeping Connection Alive ? Since Http.keepalive = True Is Not Working In The Code. Is There Any Thing More Required Along With The Http.keepalive = True
1
by: Cliff Harris | last post by:
I am trying to automate a series of form posts on a website. This site requires that my session be kept alive through successive posts (it basically tracks me by a sessionid, and if I get a new...
0
by: Kevin | last post by:
Hi, How can you set up the timeout values of the KeepAlive Socket option like you do using WSAIoctl and SIO_KEEPALIVE_VALS using C++ ? Searching about this I found several post of people using...
4
by: Kalpesh Modha | last post by:
Hello. I sometimes get this error message "An existing connection was forcibly closed by the remote host" for no reason what so ever in my program. I was wondering keepalives would help in this...
0
by: Ziphyre | last post by:
Hi, I can't send 'Connection: Keep-Alive' property in the second connection of my HttpWebRequest. Suppose: HttpWebRequest hreq = ..... hreq.KeepAlive = true; HttpWebResponse hresp =...
0
by: Greg | last post by:
I have a problem with KeepAlives. I need to use them for crawling a secure server, but if I crawl too long the application memory keeps increasing and doesn't decrease. After lots of testing,...
1
by: okaminer | last post by:
Hi This is for the experts I have override the GetWebRequest method and set the keepalive property to false in order to overcome a certain in .NET web service problem. My problem now is that...
2
by: Volker Hetzer | last post by:
Hi! I've got a problem with the GC.keepalive function, as used in the examples at http://msdn2.microsoft.com/en-US/library/system.gc.keepalive.aspx .. There they have a class "Example",...
1
by: Robert Bravery | last post by:
HI All, I have a few aspx websites that are not that busy, but when clients go to them they complain that the are slow. I obviously put this down to the behaviour of normal aspx pages, that go...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...

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.