473,378 Members | 1,067 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,378 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 1477
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
3
by: RobG | last post by:
I am playing with a script that will allow columns of a table to be moved by dragging them left or right. To do this with functions is fairly straight forward, however after looking at Richard...
5
by: Robert Zurer | last post by:
I have a large pool of business objects all referencing one another in various ways. In the client application I want to do something like employee.Delete(); Behind the scenes, I want to...
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...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.