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

Home Posts Topics Members FAQ

Detecting if an object is not reachable and can be collected by GC

GeH
Is there any possibility to detect if an object is reachable and can
be collected by the GarbageCollector?

I'm using a cache of weak references to persistent objects. Objects
are then accessed using an ObjectID. If the object is still alive, the
object is retrieved from the cache, if not, it will be fetched from a
database and the referece is returned to the caller.

Now I'm trying to implement a kind of automatic pessimistic locking:
when an object is placed into the cache, to object is locked. The
caller can now modify the object. After a defined period of time, I
would like to search the cache and detect the objects wich are not
referenced any more. These objects can then be unlocked and can be
modified by other users.

One possibility would be to call the GC.Collect(), walk the cache and
find all weak references which have been collected. Using the objectId
it is now possible to unlock the object in the database.
Unfortunatelly calling GC.Collect() is probably not a good idea. Any
other suggestions? As far as I know the GC performs a collect in two
steps: mark and sweep. Is it possible to execute just the mark step
and use the result for detecting if an object is still reachable??

Thanks,
Gerhard.

Jul 25 '07 #1
1 1275
GeH wrote:
Is there any possibility to detect if an object is reachable and can
be collected by the GarbageCollector?

I'm using a cache of weak references to persistent objects. Objects
are then accessed using an ObjectID. If the object is still alive, the
object is retrieved from the cache, if not, it will be fetched from a
database and the referece is returned to the caller.
<...>
One way you could do this would be with reference counting (which the GC
does not do).
I have investigated the same thing in the past and found that there was
no way to determine this.
There should be a bit of info regarding implementing reference counting
on the web but basically, each time something acquires a reference to
your object, you increment the ref count, when they release the
reference, you decrement the ref count.
You could use the IDisposable pattern to do reference counting,
otherwise it'd have to be manual (which idisposable is too).
HTH

JB
Jul 26 '07 #2

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

Similar topics

3
by: Ian Renfrew | last post by:
Does Javascript supply a function that destroys an object? If so, is there a dependancy on Javascript version? Thanks in advance, Ian Renfrew
3
by: jdn | last post by:
I've read various things about this and am not sure what the 'best practice' (hate that phrase but there you go) procedure is. Suppose in a class I have something like: Object myObject; ...
5
by: Paul Wilkinson | last post by:
How can I write a method that is called when an object goes out of scope? I've looked all over, so far I've only found discussions of how to write methods that don't = )
35
by: MuZZy | last post by:
Hi All, I got a issue here and hope someone can help me: Let's consider this code: // =================== CODE START ================================= using System; using System.Data; ...
7
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() {...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
5
by: Giovanni Dicanio | last post by:
Hi, please consider the following scenario: I allocate some objects in C# using new. e.g.: MyObject x = new MyObject(); then I assign this object to several places, e.g. in Tag field in
11
by: Jay Dee | last post by:
Object garbage collection of events I can not help wondering how the garbage collection system handles events. If I release all references to an object that has a void that an event somewhere...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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: 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 ...
0
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...

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.