FAQ
Hello, I Have the following code fragment. My question is: Can I assume
LiveObj is not garbage collected while it has running goroutine associated
with it, or it is just coincidence?
Thank you.

`package main

import "fmt"
import "time"
import "runtime"

type LiveObj struct {
}

func (self *LiveObj) run() {
for {
time.Sleep(1 * time.Second)
fmt.Println("Hello, LiveObj")
}
}

func main() {
fmt.Println("Hello, playground")
{
l := &LiveObj{}
go l.run()
}
runtime.GC()

time.Sleep(1 * time.Minute)
}`

Output:

`Hello, playgroundHello, LiveObjHello, LiveObj.... (for 1 minute)
Program exited.`


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Rob Pike at Jul 26, 2013 at 10:26 pm
    The garbage collector collects garbage (its name is accurate). Your LiveObj
    is not garbage because there is a running goroutine holding a reference to
    it.

    So yes, you can assume this behavior, but it is a fundamental property and
    I'm unsure why you need to ask. It implies something else is going on, at
    least in your understanding.

    -rob

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Drago Ivanov at Jul 26, 2013 at 10:35 pm
    Habits Rob, habits...
    I was unsure how goroutines treat member functions, and their "self"
    pointer lifetime. Thanks.

    27 юли 2013, събота, 01:25:41 UTC+3, Rob Pike написа:
    The garbage collector collects garbage (its name is accurate). Your
    LiveObj is not garbage because there is a running goroutine holding a
    reference to it.

    So yes, you can assume this behavior, but it is a fundamental property and
    I'm unsure why you need to ask. It implies something else is going on, at
    least in your understanding.

    -rob
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at Jul 27, 2013 at 12:55 am
    The "self" pointer is essentially an argument to the run() function, which
    explains why it's not collected until at least run() returns.
    On Friday, July 26, 2013 3:35:24 PM UTC-7, drago....@gmail.com wrote:

    Habits Rob, habits...
    I was unsure how goroutines treat member functions, and their "self"
    pointer lifetime. Thanks.

    27 юли 2013, събота, 01:25:41 UTC+3, Rob Pike написа:
    The garbage collector collects garbage (its name is accurate). Your
    LiveObj is not garbage because there is a running goroutine holding a
    reference to it.

    So yes, you can assume this behavior, but it is a fundamental property
    and I'm unsure why you need to ask. It implies something else is going on,
    at least in your understanding.

    -rob
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitry Vyukov at Jul 27, 2013 at 9:12 am

    On Sat, Jul 27, 2013 at 2:01 AM, wrote:
    Hello, I Have the following code fragment. My question is: Can I assume
    LiveObj is not garbage collected while it has running goroutine associated
    with it, or it is just coincidence?
    It may or may not be collected.
    You must not be able to observe any collected objects. If you are not
    looking at it, why do you care?


    Thank you.

    `package main

    import "fmt"
    import "time"
    import "runtime"

    type LiveObj struct {
    }

    func (self *LiveObj) run() {
    for {
    time.Sleep(1 * time.Second)
    fmt.Println("Hello, LiveObj")
    }
    }

    func main() {
    fmt.Println("Hello, playground")
    {
    l := &LiveObj{}
    go l.run()
    }
    runtime.GC()

    time.Sleep(1 * time.Minute)
    }`

    Output:

    `Hello, playground
    Hello, LiveObj
    Hello, LiveObj
    .... (for 1 minute)

    Program exited.`


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 26, '13 at 10:01p
activeJul 27, '13 at 9:12a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase