--
dustin
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I argue that this is not particularly useful for caches, because the behavior of your cache will change without corresponding code changes; what works today might break with another compiler or a new compiler version. If the GC is sufficiently optimal, it seems like with a weak reference you would never find anything cached!
Why it is impossible to explicitly manage the cache in this case? I
mean explicitly decide what to cache, when to cache and for how long.
John Nagle
I don't understand how to use a weak reference safely in a parallel
program without using special APIs to get and set the value.
To me it seems like a minor convenience. A weak reference is
effectively a cache. Having the GC manage the cache for you is handy
but on the other hand you give up the ability to control the
replacement strategy or the size of the cache.
This is magic, even though feasible by the runtime.
Go excels in
explicitness, even though that may mean more user coding. My feeling
is that those two approaches don't mix well, to put it mildly.
Anyhow, I don't care if Go ever gets them, but there are other fun things that can be done with weak references that don't have anything to do with caches.
On Mar 5, 2013, at 3:13 PM, Steve McCoy <mcc...@gmail.com> wrote:Anyhow, I don't care if Go ever gets them, but there are other fun things that can be done with weak references that don't have anything to do with caches.They’re also useful for uniquing objects, of which Java’s String.intern is a simple example. Unique objects have nice characteristics, such as being able to compare them using pointer equality, and avoiding any need for coordinating state changes among multiple objects representing the same thing. (For example, I like this pattern for objects representing database rows/records.)
These objects are generally managed with a central map that will return an existing object or generate a new one if it doesn’t exist yet. Without weak references, this map grows monotonically as no uniqued object will ever get collected. (Unless you resort to workaround like a retain/release API, i.e. building ref-counting on top of a GC system, which seems perverse.)
—Jens
I understand what a soft reference can be useful for, but what is the use for a weak reference since GC could happen anytime?
Remy
Remy
In my opinion weak references should not be added to the language. If
Go ever has them, they should only be implemented via a runtime API.
With that approach, a stop-the-world GC is not required. If anybody
is holding a pointer that they retrieved via the runtime API, the
object will not be freed.
On Tue, Mar 5, 2013 at 9:46 AM, Dustin <dsal...@gmail.com> wrote:
>
> On Monday, March 4, 2013 10:56:05 PM UTC-8, Dmitry Vyukov wrote:
>>
>> Why it is impossible to explicitly manage the cache in this case? I
>> mean explicitly decide what to cache, when to cache and for how long.
>
>
> It's also possible to explicitly manage memory, but we have a GC.
>
> I have objects that I don't *need* to be in memory for long periods of
> time, but it can be awfully convenient to hint to the GC that an object is
> less important than another object during GC. I convert a strongref to a
> weakref when I'm not using the object at all, and copy out a strongref for
> the times when I need it. When I need it and it's not there, I have a more
> expensive path to get it. When that becomes a really common thing, I have a
> fairly consistent degradation path.
I am not sure how one can want something along the lines of "discard
on the following GC" w/o any knowledge about what it actually means
(can be right now, never or at random time). What you are asking about
seems to be timed and/or memory limited cache component with some
tunable policy on when to discard cached objects. Such a component can
be implemented using some fancy references provided that it has some
intimate channels to and understanding of current GC, but that's an
implementation detail as well.
> In general, it seems like the kind of thing that one would expect from a
> gc (e.g. those coming from java, python, ruby, smalltalk, etc...), so at the
> very least, it seems like a canned response for why it isn't/shouldn't be in
> go might be a good thing to point to. It's fine if the answer is something
> along the lines of, "we think this is a bad idea!"
>
> e.g. Kyle's argument for a straight cache is a good one, though I'm
> thinking about something closer to Jens' example where I am actually using a
> lot of the objects and may continue to use them, but my "central" reference
> is weak, only held by all of the live activity around the object until it's
> no longer available.
-----Original Message-----
From: tgpti...@gmail.com
Sent: Jun 13, 2019 5:39 AM
To: golang-nuts
Subject: Re: [go-nuts] Re: weak references
I find WeakReferences when implementing observer pattern: A service generate events and signal listeners that the event was triggered. This usually requires the service to have a list to all of the listeners. Now, without weak refernece it means that any temporary listener will not be garbage collected and thus we will have memory grow without limits, since the listeners will not be collected as they are referenced by the service. When it comes to this, WeakReferences are mandatory and unfortunately Go doesn't support them :'(
terça-feira, 5 de Março de 2013 às 23:13:05 UTC, Steve McCoy escreveu:
On Tuesday, March 5, 2013 5:43:35 PM UTC-5, Jan Mercl wrote:On Tue, Mar 5, 2013 at 11:24 PM, Dustin Sallings <dsal...@gmail.com> wrote:
> Strong reference: "hands off, gc"
> Weak reference: "0 this if no strong reference exists"
> Soft refernce: "0 this if no strong reference exists and we're low
> on RAM"
This is magic, even though feasible by the runtime. Go excels in
explicitness, even though that may mean more user coding. My feeling
is that those two approaches don't mix well, to put it mildly.
-jIt's not magic; it's not as though people won't know they have an instance of a weak reference on their hands.Anyhow, I don't care if Go ever gets them, but there are other fun things that can be done with weak references that don't have anything to do with caches. I wrote a Java tool+package that keeps track of every object created while the program runs and asserts some preconditions whenever an object is accessed. Of course, memory would be extremely wasted if I kept them all in a map with strong references for the keys, so I instead used a WeakHashMap, allowing the garbage collector to clean things up in a typical fashion. Using explicit reference counting would've been painful for that.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5f6c64a4-f45b-4547-9e14-6039b97f26cb%40googlegroups.com.
-----Original Message-----
From: Ricardo Alves
Sent: Jun 13, 2019 2:12 PM
To: "tgpti...@gmail.com" , golang-nuts , Robert Engels
Subject: Re: [go-nuts] Re: weak references
Yes, I understand. But if I have a productive language that doesn't need me to call it better. In C/C++ it's also easy, whoever calls new should call delete, but trust me, I've had my troubles finding memory leaks in other people's code. With a large enterprise application made in Go and working with other people and observer pattern, sooner or later I will have the same trouble finding memory this "memory leak".
Obter o Outlook para Android
From: Robert Engels <ren...@ix.netcom.com>
Sent: Thursday, June 13, 2019 8:08:41 PM
To: tgpti...@gmail.com; golang-nuts
-----Original Message-----
From: Ricardo Alves
Sent: Jun 13, 2019 2:46 PM
To: "tgpti...@gmail.com" , golang-nuts , Robert Engels
Subject: Re: [go-nuts] Re: weak references
Thank you for your response again. In the case where anonymous classes are used, the anonymous class would add it self as listeners as you said, but what happens is that the service where the listener will be added will have a weak reference to the this listener. This means that the listener can be collected anytime without calling the RemoveListener. I worked with projects which all the arquitechture was event based, and sometimes even small things like a field of a class are listening to something to keep it's value updated. Sometimes you end up with lots of listeners. Regarding this last case, imagine you have a class A that inside has a class B. Object B is listening to something but it will not be collected until object A is collected. So, Object B never really knows when he needs to stop listening. In this case I will have to come up with methods to communicate between the two objects or instead I will have to turn object A as a listener to act as a gateway to object B. This is not productive at all in these cases, that's why I like to have WeakReferences.
Obter o Outlook para Android
From: Robert Engels <ren...@ix.netcom.com>
Sent: Thursday, June 13, 2019 8:35:15 PM
To: Ricardo Alves; tgpti...@gmail.com; golang-nuts
Subject: Re: [go-nuts] Re: weak references
But using weak references for listeners is fraught with problems. The most common being that listeners are often anonymous inner classes (in something like Java) or at the point instantiated (closure), so if a direct reference is not held, the listener is would be immediately removed. Holding the reference to the inner instance is cumbersome - often requiring the developer to create an instance variable and assign to that.
But you still need to manage the lifecycle of the object that was listening... so why not remove the listener then.
I don't think a finalizer helps in this case - since the service will have a hard reference to the listener, it will never be finalized... or GC'd, unless the removeListener is called anyway.
That being said, if the service is no longer referenced, all listeners will be GC'd - there will be no memory leak.
For example, Java Swing doesn't use weak listeners, but it does have weak references in some cases to the Window (i.e. Service), which contains references to all of the components (which are the listeners). When the Window goes out of scope, everything will be destroyed (cleaned up by GC).
-----Original Message-----
From: Ricardo Alves
Sent: Jun 13, 2019 2:12 PM
To: "tgpti...@gmail.com" , golang-nuts , Robert Engels
I've been looking around for some information on weak references and,
while I've found a few open threads, I haven't seen much of a closure on
this, nor an issue opened on the project.
I'd like to have a weak reference. It seems like this requires GC
cooperation, though I'm open to a supportable strategy that doesn't
require any core changes.
A friendly irc user suggested something like this could theoretically
work: http://play.golang.org/p/IHRU2TwgF9
But in general, being able to build a map[thing]WeakReference would be
very useful for many caching scenarios. WeakReference.Get() ->
interface{} would be fine. Something typesafe would be even better.
Is there a writeup about this? Should I file an issue?
--
dustin