Object References

  • Thread starter Thread starter David Pendrey
  • Start date Start date
D

David Pendrey

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
 
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
 
Otis Mukinfus said:
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 :)
 
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
 
David Pendrey said:
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?)
 
Daniel O'Connell said:
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.)
 
| > | > > 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.
 
David Pendrey said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top