471,354 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Object References

Greetings all!

I am writing a program which will contain a large amount of information
which is saved onto disk and read into objects as it is required. Because of
the size of information I need to create a buffer for it rather than having
everything loaded at once. Unfortunatly data access is random and the
information needs to be kept for random periods of time (anything between 10
clock ticks and 10 hours).

I saw a great method of handling this kind of buffering once in visual
basic... Each object keeps track of how many variables it is stored in, and
once that value reaches zero the object is unloaded from memory by the
garbage collector. This VB code decremented this counter by one, so once
only the buffer had a copy of the object it was unloaded. To me this seems
like the optimal solution, I don't have to keep track of when data isn't
being used anymore, just load the data when it's not in the cache.

Can anyone point me in the right direction for implementing this in C#, so
far I've been unable to figure out how to do it myself.

Regards, Dave
Feb 21 '06 #1
8 1307
On Tue, 21 Feb 2006 00:57:29 GMT, "David Pendrey" <fa*******@dodo.com.au.delme>
wrote:
Greetings all!

I am writing a program which will contain a large amount of information
which is saved onto disk and read into objects as it is required. Because of
the size of information I need to create a buffer for it rather than having
everything loaded at once. Unfortunatly data access is random and the
information needs to be kept for random periods of time (anything between 10
clock ticks and 10 hours).

I saw a great method of handling this kind of buffering once in visual
basic... Each object keeps track of how many variables it is stored in, and
once that value reaches zero the object is unloaded from memory by the
garbage collector. This VB code decremented this counter by one, so once
only the buffer had a copy of the object it was unloaded. To me this seems
like the optimal solution, I don't have to keep track of when data isn't
being used anymore, just load the data when it's not in the cache.

Can anyone point me in the right direction for implementing this in C#, so
far I've been unable to figure out how to do it myself.

Regards, Dave

That method of garbage collection is what made VB a memory leaker.

In .NET you don't have to worry about that sort of thing. You are worried about
releasing memory, aren't you? If not I guess I don't understand your question.
You can read about how the Garbage Collector works here:
http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Feb 21 '06 #2

"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:qi********************************@4ax.com...
That method of garbage collection is what made VB a memory leaker.

In .NET you don't have to worry about that sort of thing. You are worried
about
releasing memory, aren't you? If not I guess I don't understand your
question.
You can read about how the Garbage Collector works here:
http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com


I'm after a way to hold an object in a cache and have it unloaded by the
garbage collector if it is only in the cache. After some poking around in
the link you posted I found the System.WeakReference class, which does just
that.

Thanks for the help Otis :)
Feb 21 '06 #3
On Tue, 21 Feb 2006 03:58:17 GMT, "David Pendrey" <fa*******@dodo.com.au.delme>
wrote:

"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:qi********************************@4ax.com.. .
That method of garbage collection is what made VB a memory leaker.

In .NET you don't have to worry about that sort of thing. You are worried
about
releasing memory, aren't you? If not I guess I don't understand your
question.
You can read about how the Garbage Collector works here:
http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com


I'm after a way to hold an object in a cache and have it unloaded by the
garbage collector if it is only in the cache. After some poking around in
the link you posted I found the System.WeakReference class, which does just
that.

Thanks for the help Otis :)

You're welcome :o)

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Feb 21 '06 #4
Thought other people should know about this, the WeakReference object
doesn't seem to work with strings, I spent an hour trying to figure it out
then I tried the same code with a 'new object()' rather than '"test
string"'. Luckily the data I'm storing isn't plain strings but it should be
possible to work around it by creating a wrapper class (I'm guessing it just
doesn't like primitives).

Dave
"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:ad********************************@4ax.com...
On Tue, 21 Feb 2006 03:58:17 GMT, "David Pendrey"
<fa*******@dodo.com.au.delme>
wrote:

You're welcome :o)

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Feb 23 '06 #5

"David Pendrey" <fa*******@dodo.com.au.delme> wrote in message
news:43******@news.comindico.com.au...
Thought other people should know about this, the WeakReference object
doesn't seem to work with strings, I spent an hour trying to figure it out
then I tried the same code with a 'new object()' rather than '"test
string"'. Luckily the data I'm storing isn't plain strings but it should
be possible to work around it by creating a wrapper class (I'm guessing it
just doesn't like primitives).


Are you using a string literal or a string you built or pulled from a file?
It could be that the WeakReference class doesn't deal with interned strings
like one would expect(does the GC even really bother with them, for that
matter?)
Feb 23 '06 #6
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
"David Pendrey" <fa*******@dodo.com.au.delme> wrote in message
news:43******@news.comindico.com.au...
Thought other people should know about this, the WeakReference object
doesn't seem to work with strings, I spent an hour trying to figure it out
then I tried the same code with a 'new object()' rather than '"test
string"'. Luckily the data I'm storing isn't plain strings but it should
be possible to work around it by creating a wrapper class (I'm guessing it
just doesn't like primitives).


Are you using a string literal or a string you built or pulled from a file?
It could be that the WeakReference class doesn't deal with interned strings
like one would expect(does the GC even really bother with them, for that
matter?)


Interned strings may be released if an AppDomain is unloaded, but I
think that's as far as it goes. (I'm not sure what happens if you've
got two AppDomains which use the same literal - I don't know if they're
interned together or not.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
| Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
| > "David Pendrey" <fa*******@dodo.com.au.delme> wrote in message
| > news:43******@news.comindico.com.au...
| > > Thought other people should know about this, the WeakReference object
| > > doesn't seem to work with strings, I spent an hour trying to figure it
out
| > > then I tried the same code with a 'new object()' rather than '"test
| > > string"'. Luckily the data I'm storing isn't plain strings but it
should
| > > be possible to work around it by creating a wrapper class (I'm
guessing it
| > > just doesn't like primitives).
| >
| > Are you using a string literal or a string you built or pulled from a
file?
| > It could be that the WeakReference class doesn't deal with interned
strings
| > like one would expect(does the GC even really bother with them, for that
| > matter?)
|
| Interned strings may be released if an AppDomain is unloaded, but I
| think that's as far as it goes. (I'm not sure what happens if you've
| got two AppDomains which use the same literal - I don't know if they're
| interned together or not.)
|

Interned strings are shared across ADs.
That is, a string literal "Hello" in AD #1 is the exact same object as
"Hello" in AD #2.
They are only released when the default AD unloads.

Willy.
Feb 24 '06 #8

"David Pendrey" <fa*******@dodo.com.au.delme> wrote in message
news:43******@news.comindico.com.au...
Thought other people should know about this, the WeakReference object
doesn't seem to work with strings, I spent an hour trying to figure it out
then I tried the same code with a 'new object()' rather than '"test
string"'. Luckily the data I'm storing isn't plain strings but it should
be possible to work around it by creating a wrapper class (I'm guessing it
just doesn't like primitives).

Dave


I wouldn't expect it to work as you expect with constant strings because the
code references them.

You wouldn't really want the compiler to bother allocating 100 strings in
the following would you?

for(i=0;i<100;++i) s = "hello";

I bet that if you create your strings using StringBuilder they will
dissapear just like any other object.
Feb 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris S. | last post: by
44 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by RobG | last post: by
5 posts views Thread by Michael Moreno | last post: by
16 posts views Thread by anonymous.user0 | last post: by
26 posts views Thread by yb | last post: by
275 posts views Thread by Astley Le Jasper | last post: by

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.