FAQ
This program defines a simple stack, pushing and popping elements.
   http://play.golang.org/p/NPFe_eRy2q

Does the garbage collector takes the length of a slice in account when it
looks for reachable objects ?
I mean, can GC consider the elements at indices between "length" and
"capacity" as garbage, collect them, and put a nil pointer at these indices
?

I imagine that these objects are not garbage collected, but it is not very
explicit in the specs.
And I don't see the reason why it would be forbidden...
Or it is expected that when a slice is resliced to its full length, all
objects that were "hidden" are expected to have stayed at their place...


--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Oct 6, 2014 at 3:49 pm

    On Mon, Oct 6, 2014 at 8:19 AM, wrote:
    This program defines a simple stack, pushing and popping elements.
    http://play.golang.org/p/NPFe_eRy2q

    Does the garbage collector takes the length of a slice in account when it
    looks for reachable objects ?
    I mean, can GC consider the elements at indices between "length" and
    "capacity" as garbage, collect them, and put a nil pointer at these indices
    ?

    I imagine that these objects are not garbage collected, but it is not very
    explicit in the specs.
    And I don't see the reason why it would be forbidden...
    Or it is expected that when a slice is resliced to its full length, all
    objects that were "hidden" are expected to have stayed at their place...
    I don't think that the elements between the length and capacity of a
    slice can be considered garbage. The slice is defined as pointing
    into an underlying array, and the length of that array is the capacity
    of the slice. Since you are permitted to change the length of the
    slice to any value up to the capacity, that implies that any element
    of the underlying array is still accessible by the program. Those
    elements can not be collected.

    The elements before the start of the slice, on the other hand, could
    be collected, if the garbage collector can prove that no slice points
    to them. The garbage collector does not currently implement that, and
    I doubt that it would be profitable.

    Ian

    --
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Akwillis at Oct 6, 2014 at 5:36 pm
    I'd nil it. The object is stored in the underlying array, the array holds a
    reference so it wouldn't be collected until the array was abandoned.
    On Monday, October 6, 2014 11:19:15 AM UTC-4, [email protected] wrote:

    This program defines a simple stack, pushing and popping elements.
    http://play.golang.org/p/NPFe_eRy2q

    Does the garbage collector takes the length of a slice in account when it
    looks for reachable objects ?
    I mean, can GC consider the elements at indices between "length" and
    "capacity" as garbage, collect them, and put a nil pointer at these indices
    ?

    I imagine that these objects are not garbage collected, but it is not very
    explicit in the specs.
    And I don't see the reason why it would be forbidden...
    Or it is expected that when a slice is resliced to its full length, all
    objects that were "hidden" are expected to have stayed at their place...

    --
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Akwillis at Oct 6, 2014 at 6:22 pm
    Example: http://play.golang.org/p/lNIdbDHIyD
    On Monday, October 6, 2014 1:36:35 PM UTC-4, akwillis wrote:

    I'd nil it. The object is stored in the underlying array, the array holds
    a reference so it wouldn't be collected until the array was abandoned.
    On Monday, October 6, 2014 11:19:15 AM UTC-4, [email protected] wrote:

    This program defines a simple stack, pushing and popping elements.
    http://play.golang.org/p/NPFe_eRy2q

    Does the garbage collector takes the length of a slice in account when it
    looks for reachable objects ?
    I mean, can GC consider the elements at indices between "length" and
    "capacity" as garbage, collect them, and put a nil pointer at these indices
    ?

    I imagine that these objects are not garbage collected, but it is not
    very explicit in the specs.
    And I don't see the reason why it would be forbidden...
    Or it is expected that when a slice is resliced to its full length, all
    objects that were "hidden" are expected to have stayed at their place...

    --
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Akwillis at Oct 6, 2014 at 6:25 pm
    Example: http://play.golang.org/p/_WfkqioNJS
    On Monday, October 6, 2014 1:36:35 PM UTC-4, akwillis wrote:

    I'd nil it. The object is stored in the underlying array, the array holds
    a reference so it wouldn't be collected until the array was abandoned.
    On Monday, October 6, 2014 11:19:15 AM UTC-4, [email protected] wrote:

    This program defines a simple stack, pushing and popping elements.
    http://play.golang.org/p/NPFe_eRy2q

    Does the garbage collector takes the length of a slice in account when it
    looks for reachable objects ?
    I mean, can GC consider the elements at indices between "length" and
    "capacity" as garbage, collect them, and put a nil pointer at these indices
    ?

    I imagine that these objects are not garbage collected, but it is not
    very explicit in the specs.
    And I don't see the reason why it would be forbidden...
    Or it is expected that when a slice is resliced to its full length, all
    objects that were "hidden" are expected to have stayed at their place...

    --
    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 [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 6, '14 at 3:19p
activeOct 6, '14 at 6:25p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase