Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15597

Re: Controlling the Garbage Collector

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Controlling the Garbage Collector
Date 2012-06-26 06:46 -0700
Organization http://groups.google.com
Message-ID <5d156697-1477-49c1-a098-f7bfa76783b4@googlegroups.com> (permalink)
References (9 earlier) <js9nn1$lba$1@dont-email.me> <slrnjuh6bd.u9l.avl@gamma.logic.tuwien.ac.at> <jsa838$vih$1@dont-email.me> <a4rpnjForkU1@mid.individual.net> <slrnjujaih.u9l.avl@gamma.logic.tuwien.ac.at>

Show all headers | View raw


On Tuesday, June 26, 2012 2:25:53 PM UTC+2, Andreas Leitgeb wrote:
> Robert Klemme <shortcutter@googlemail.com> wrote:
> > Thank you for the elaboration, Eric and Andreas!  I think a non uniform 
> > object memory model (e.g. some GCed, some manually managed) cannot get 
> > rid of the GC overhead even for the manual kind as long as it is allowed 
> > to have references between the different kinds of objects.
> 
> It is no problem for GC'ed objects to be referenced from manually
> managed objects.

I think it is.  The point in separating object kinds is to reduce the number of objects GC has to look at in order to avoid looking at long lived old objects over and over again.  But to determine the liveliness state of an "automatic" object GC would still have to look at the "manual" object _during each allocation_.  Even worse, since references only point forward (otherwise we'd have introduced a tremendous memory - and probably CPU - hit for bidirectional references) it is necessary to look at _all_ "manual" objects reachable from roots.

> In the reverse case, it could mean that references to manually
> collected items could become "stale". Of course that would have
> to be a fundamentally different kind of "stale" as what we know
> from C.
> 
> Imagine there was a class ManualReference, similar to WeakReference.
> When a manually managed object was created, it would be transparently
> wrapped by such a ManualReference. Any ref to such an object would
> really be to a ManualReference that in turn holds the object, and
> certain things done on the reference are passed to it's payload, like
> accessing fields, or calling methods. Other stuff (assignment, or 
> passing around the value) would still be a local access on the variable
> itself. That would surely need some changes in the JVM and/or the
> compiler to work.
> 
> (I'm not proposing that. I just find it challenging to think it through.)
> 
> Now, if by some new mechanism (e.g. a new keyword "free"), a manually
> managed object was freed, then the (transparent) ManualReference would
> be simply cleared, and all further accesses cause a NullPointerException.
> 
> The ManualReference object itself would be normally GC-managed, thus
> remain in memory at least until all references to it are indeed cleared.

You replaced the automatic collected object by another automatic collected object (the ManualReference).  Again, nothing gained in terms of number of objects to look at for the GC.

More abstract: you introduced a reference type which can be set to null at any time (i.e. when someone decides to "free" this object).  Basically that's the same situation we have with WeakReference now and as consequence you need to check the reference for null on every access.

Because of concurrency you need to be able to lock the reference for a certain duration (e.g. a method call) in order to avoid clearing during a logical operation.  You would at least need to be able to to some atomic "check for null and use ref" otherwise the null check would be moot.  With WeakReference you do that by copying the reference to an ordinary reference.  The clearing mechanism would need to prevent clearing while the reference is locked / copied into a regular reference.  As a consequence the effect of your "free" operation is not to actually remove the object but only to _schedule_ it for removal.  This is a quite similar situation to "automatic" objects which have all references to them cleared (or GC'ed themselves) btw.  Since we cannot impose a limit on the locking time and the actual time lag between "free" and last "unlock" can be quite high, we might not have gained much in term of control of memory usage.

Even worse, someone else might come along and do a "lock" after "free" thus prolonging the locking period.  The alternative would be to disallow "lock" after free but that comes with issues of it's own: then any "lock" operation could fail and the user of the language is prompted with the issue that he needs to deal with the exception originating from a failed lock.  Maybe that could be salvaged by making all "lock" operations after "free" return null.  That would be similar to WeakReference.

"locking" of course would need to be reentrant so several methods of a class could cooperate and use the same "manual" member object.  That means: a "lock" after "free" must return null only if it is the first "lock" of a thread, i.e. if the thread did not have the lock before.  As another side effect we will have a situation where after a "free" some threads might still see an instance while others don't.  Well, that can happen with WeakReference as well depending on when threads fetch the reference, so that might not be too big an issue.

Now you would have to check the refcount of the "manual" object on every "unlock".  This has a number of consequences

 - refcount checking might be imposed on the thread which uses a "manual" object creating CPU overhead
 - additional synchronization is necessary to avoid race conditions for "lock" and "unlock"
 - refcount checking does not work with circular references

> Gist: I think it *could* be done, but it would surely require some
>  deep changes in Java, that are not justified by any so far identified
>  advantage.

I don't even think there is an advantage.  The - admittedly brief - analysis above shows that there is a ton of issues lurking and I believe allowing mixed references between "manual" and "automatic" objects introduces more complexity (in terms of code, runtime and probably also memory) instead of relieving us from the old object overhead.

Kind regards

robert

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recommendations for Lightweight Threading? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-15 17:33 -0500
  Re: Recommendations for Lightweight Threading? markspace <-@.> - 2012-06-15 15:55 -0700
    Re: Recommendations for Lightweight Threading? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-15 18:12 -0500
      Re: Recommendations for Lightweight Threading? markspace <-@.> - 2012-06-15 16:31 -0700
        Re: Recommendations for Lightweight Threading? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-15 20:00 -0500
        Re: Recommendations for Lightweight Threading? Robert Klemme <shortcutter@googlemail.com> - 2012-06-16 14:39 +0200
          Re: Recommendations for Lightweight Threading? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-16 12:13 -0700
      Re: Recommendations for Lightweight Threading? Roedy Green <see_website@mindprod.com.invalid> - 2012-06-16 00:57 -0700
  Re: Recommendations for Lightweight Threading? Lew <lewbloch@gmail.com> - 2012-06-15 15:57 -0700
    Re: Recommendations for Lightweight Threading? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-15 18:12 -0500
  Re: Recommendations for Lightweight Threading? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-15 20:19 -0400
    Re: Recommendations for Lightweight Threading? "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-15 19:59 -0500
      Re: Recommendations for Lightweight Threading? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-15 21:37 -0400
        Controlling the Garbage Collector "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-16 11:51 -0500
          Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-16 14:24 -0400
            Re: Controlling the Garbage Collector markspace <-@.> - 2012-06-16 12:24 -0700
              Re: Controlling the Garbage Collector Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-16 13:14 -0700
                Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-16 16:35 -0400
                Re: Controlling the Garbage Collector "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-16 19:43 -0500
                Re: Controlling the Garbage Collector Wanja Gayk <brixomatic@yahoo.com> - 2012-06-23 13:36 +0200
                Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-23 15:39 +0200
                Re: Controlling the Garbage Collector markspace <-@.> - 2012-06-16 14:34 -0700
          Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-18 04:32 -0700
            Re: Controlling the Garbage Collector Roedy Green <see_website@mindprod.com.invalid> - 2012-06-24 14:31 -0700
          Re: Controlling the Garbage Collector Arne Vajhøj <arne@vajhoej.dk> - 2012-06-20 21:19 -0400
            Re: Controlling the Garbage Collector "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-21 13:24 -0500
              Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-21 11:37 -0700
                Re: Controlling the Garbage Collector Fred Greer <fggreer@nospam.invalid> - 2012-06-21 21:20 +0000
              Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-21 15:24 -0400
              Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-21 23:46 +0200
                Re: Controlling the Garbage Collector Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-06-25 15:28 +0300
                Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 09:05 -0400
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-25 17:01 +0000
                Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 13:45 -0400
                Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-25 13:49 -0400
                Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-25 20:41 +0200
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-26 12:25 +0000
                Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-26 06:46 -0700
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-26 16:26 +0000
                Re: Controlling the Garbage Collector Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-26 13:07 -0400
                Re: Controlling the Garbage Collector Robert Klemme <shortcutter@googlemail.com> - 2012-06-26 22:28 +0200
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-26 23:49 +0000
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-26 18:20 -0700
                Re: Controlling the Garbage Collector Highway to Hell <HtH49439112@gmail.com> - 2012-06-26 21:52 -0400
                Re: Controlling the Garbage Collector Gene Wirchenko <genew@ocis.net> - 2012-06-26 20:01 -0700
                Re: Controlling the Garbage Collector Highway to Hell <HtH49439112@gmail.com> - 2012-06-26 23:23 -0400
                Re: Controlling the Garbage Collector Gene Wirchenko <genew@ocis.net> - 2012-06-27 09:05 -0700
                Re: Controlling the Garbage Collector Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-27 20:15 +0000
                Re: Controlling the Garbage Collector Gene Wirchenko <genew@ocis.net> - 2012-06-27 13:52 -0700
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-27 13:41 -0700
                Re: Controlling the Garbage Collector Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:02 -0700
                Re: Controlling the Garbage Collector Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-28 21:45 +0000
                Re: [OT] Driver's license restrictions (Was: Controlling the Garbage Collector) Lew <lewbloch@gmail.com> - 2012-06-28 15:16 -0700
                Re: [OT] Driver's license restrictions (Was: Controlling the Garbage Collector) Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-29 02:10 +0000
                Re: [OT] Driver's license restrictions (Was: Controlling the Garbage Collector) Gene Wirchenko <genew@ocis.net> - 2012-06-28 19:57 -0700
                Re: [OT] Driver's license restrictions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-06-29 04:06 +0000
                Re: [OT] Driver's license restrictions Tim Slattery <Slattery_T@bls.gov> - 2012-06-29 08:35 -0400
                Re: [OT] Driver's license restrictions (Was: Controlling the Garbage Collector) Tim Slattery <Slattery_T@bls.gov> - 2012-06-29 08:33 -0400
                Re: Controlling the Garbage Collector Tim Slattery <Slattery_T@bls.gov> - 2012-06-29 08:30 -0400
                Re: Controlling the Garbage Collector Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-29 23:04 +0000
                Re: Controlling the Garbage Collector Highway to Hell <HtH49439112@gmail.com> - 2012-06-27 16:53 -0400
                Re: Controlling the Garbage Collector Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-06-27 11:32 -0500
                Re: Controlling the Garbage Collector Highway to Hell <HtH49439112@gmail.com> - 2012-06-27 16:54 -0400
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-27 11:45 -0700
                Re: Controlling the Garbage Collector Highway to Hell <HtH49439112@gmail.com> - 2012-06-27 16:55 -0400
                Re: Controlling the Garbage Collector glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-06-27 02:06 +0000
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-27 16:34 +0000
                Re: Controlling the Garbage Collector Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-06-27 11:45 -0500
                Re: Controlling the Garbage Collector Wanja Gayk <brixomatic@yahoo.com> - 2012-07-02 11:21 +0200
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-26 13:52 -0700
                Re: Controlling the Garbage Collector Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-06-26 23:40 +0000
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-26 16:48 -0700
              Re: Controlling the Garbage Collector markspace <-@.> - 2012-06-21 15:15 -0700
                Re: Controlling the Garbage Collector Lew <lewbloch@gmail.com> - 2012-06-21 15:33 -0700
                Re: Controlling the Garbage Collector "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-21 21:24 -0500
              Re: Controlling the Garbage Collector markspace <-@.> - 2012-06-21 15:23 -0700
        Re: Recommendations for Lightweight Threading? Wanja Gayk <brixomatic@yahoo.com> - 2012-06-17 15:49 +0200
    Re: Recommendations for Lightweight Threading? Roedy Green <see_website@mindprod.com.invalid> - 2012-06-16 01:00 -0700
      Re: Recommendations for Lightweight Threading? Roedy Green <see_website@mindprod.com.invalid> - 2012-06-16 01:04 -0700
  Re: Recommendations for Lightweight Threading? Wanja Gayk <brixomatic@yahoo.com> - 2012-06-16 04:03 +0200
  Re: Recommendations for Lightweight Threading? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2012-06-15 22:27 -0700
    Re: Recommendations for Lightweight Threading? Robert Klemme <shortcutter@googlemail.com> - 2012-06-16 14:39 +0200
      Re: Recommendations for Lightweight Threading? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2012-06-18 19:37 -0700
        Re: Recommendations for Lightweight Threading? Robert Klemme <shortcutter@googlemail.com> - 2012-06-19 07:46 -0700

csiph-web