JVM GC and unused references












0














Let's say there are following four objects in a Java program:




  • main

  • a

  • b

  • c


The dependencies are as follows:



main ==> a ==> b ==> c



In other words, main uses a, a uses b, b uses c.



Now, value of object a changes from reference to b to null. What it means is that there are no active references to b, making it available for garbage collection:



main ==> a =/=> b ==> c



What could happen now:



A) In laymans terms, b can be garbage collected at any time. My assumption is that c will become elligible for gargabe collection only after b is garbage collected.



B) However, I imagine this could be a case that JVM could somehow distinguish between references to c from GC-eligible and non-GC-eligible objects, and therefore mark c as eligible at the same time as b.



Is the behaviour determined by Java Language Specification, or any JVM-related specification? Or is it left for the JVM implementation to decide?










share|improve this question


















  • 1




    Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
    – Slaw
    Nov 12 '18 at 13:23






  • 1




    All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
    – Holger
    Nov 12 '18 at 16:03
















0














Let's say there are following four objects in a Java program:




  • main

  • a

  • b

  • c


The dependencies are as follows:



main ==> a ==> b ==> c



In other words, main uses a, a uses b, b uses c.



Now, value of object a changes from reference to b to null. What it means is that there are no active references to b, making it available for garbage collection:



main ==> a =/=> b ==> c



What could happen now:



A) In laymans terms, b can be garbage collected at any time. My assumption is that c will become elligible for gargabe collection only after b is garbage collected.



B) However, I imagine this could be a case that JVM could somehow distinguish between references to c from GC-eligible and non-GC-eligible objects, and therefore mark c as eligible at the same time as b.



Is the behaviour determined by Java Language Specification, or any JVM-related specification? Or is it left for the JVM implementation to decide?










share|improve this question


















  • 1




    Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
    – Slaw
    Nov 12 '18 at 13:23






  • 1




    All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
    – Holger
    Nov 12 '18 at 16:03














0












0








0







Let's say there are following four objects in a Java program:




  • main

  • a

  • b

  • c


The dependencies are as follows:



main ==> a ==> b ==> c



In other words, main uses a, a uses b, b uses c.



Now, value of object a changes from reference to b to null. What it means is that there are no active references to b, making it available for garbage collection:



main ==> a =/=> b ==> c



What could happen now:



A) In laymans terms, b can be garbage collected at any time. My assumption is that c will become elligible for gargabe collection only after b is garbage collected.



B) However, I imagine this could be a case that JVM could somehow distinguish between references to c from GC-eligible and non-GC-eligible objects, and therefore mark c as eligible at the same time as b.



Is the behaviour determined by Java Language Specification, or any JVM-related specification? Or is it left for the JVM implementation to decide?










share|improve this question













Let's say there are following four objects in a Java program:




  • main

  • a

  • b

  • c


The dependencies are as follows:



main ==> a ==> b ==> c



In other words, main uses a, a uses b, b uses c.



Now, value of object a changes from reference to b to null. What it means is that there are no active references to b, making it available for garbage collection:



main ==> a =/=> b ==> c



What could happen now:



A) In laymans terms, b can be garbage collected at any time. My assumption is that c will become elligible for gargabe collection only after b is garbage collected.



B) However, I imagine this could be a case that JVM could somehow distinguish between references to c from GC-eligible and non-GC-eligible objects, and therefore mark c as eligible at the same time as b.



Is the behaviour determined by Java Language Specification, or any JVM-related specification? Or is it left for the JVM implementation to decide?







java garbage-collection jvm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 13:16









automatictester

1,084622




1,084622








  • 1




    Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
    – Slaw
    Nov 12 '18 at 13:23






  • 1




    All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
    – Holger
    Nov 12 '18 at 16:03














  • 1




    Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
    – Slaw
    Nov 12 '18 at 13:23






  • 1




    All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
    – Holger
    Nov 12 '18 at 16:03








1




1




Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
– Slaw
Nov 12 '18 at 13:23




Some related questions: stackoverflow.com/questions/31451346/…, stackoverflow.com/questions/8812635/…, and stackoverflow.com/questions/10587868/…
– Slaw
Nov 12 '18 at 13:23




1




1




All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
– Holger
Nov 12 '18 at 16:03




All that a garbage collector cares for, is that main and a are still reachable. Everything else is garbage. Despite its misleading name, a garbage collector never looks at the garbage.
– Holger
Nov 12 '18 at 16:03












1 Answer
1






active

oldest

votes


















0














Thanks to this link, I believe option B is correct:
https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
Once b and c are no longer reachable from GC roots, they are both collectible at the same time.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262985%2fjvm-gc-and-unused-references%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to this link, I believe option B is correct:
    https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
    Once b and c are no longer reachable from GC roots, they are both collectible at the same time.






    share|improve this answer


























      0














      Thanks to this link, I believe option B is correct:
      https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
      Once b and c are no longer reachable from GC roots, they are both collectible at the same time.






      share|improve this answer
























        0












        0








        0






        Thanks to this link, I believe option B is correct:
        https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
        Once b and c are no longer reachable from GC roots, they are both collectible at the same time.






        share|improve this answer












        Thanks to this link, I believe option B is correct:
        https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/
        Once b and c are no longer reachable from GC roots, they are both collectible at the same time.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 19:12









        automatictester

        1,084622




        1,084622






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262985%2fjvm-gc-and-unused-references%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values