473,544 Members | 550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1483
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***@emailadd ress.com> wrote in message
news:qi******** *************** *********@4ax.c om...
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.WeakRefe rence 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***@emailadd ress.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.WeakRefe rence 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***@emailadd ress.com> wrote in message
news:ad******** *************** *********@4ax.c om...
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******@n ews.comindico.c om.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******@n ews.comindico.c om.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
| Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
| > "David Pendrey" <fa*******@dodo .com.au.delme> wrote in message
| > news:43******@n ews.comindico.c om.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******@n ews.comindico.c om.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
2243
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 data: a= b= c= d=
4
5536
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: http://groups.google.com/groups?selm=bcq6fn%24g53%241%248300dec7%40news.demon.co.uk This message summarizes some testing I've done and their...
44
2403
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 semantics"? What, if any, associated semantics are shared by all objects? That part seems to go beyond the FAQ. Does anybody know of a resource...
3
1340
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 Cornford's version of Table Highlighter I decided to do it using an object. Richard's basic code structure is: var SomeObject = (function() {...
5
9712
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 remove all references to the object allowing it to be garbage collected. I then want to remove the physical representation of the object from...
5
2503
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
16
2878
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
26
5639
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 several parts of the DOM, but this does not include the window object. Thank you
28
2152
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 that it simplifies the simulator very much. In this translation I want to keep the JAL-syntax as much as possible intact, so anyone who can read...
275
12058
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'
0
7424
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7772
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7376
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7709
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5297
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4918
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
988
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.