Enforcing JVM GC - for a good reason (I hope)












0














Let's look at potential race condition related to garbage collection speed.



Initial state:



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


main program references object a, object a references b, b references c. Now, we want to make a reference c instead of b.



Good:



a => c


As b is no longer referenced, it can be garbage collected.



Bad:



a = null
a => c


In between these two steps (setting a to null and re-pointing it at c), b may become garbage collected together with c. I doubt these kind of errors may be picked up by unit tests, as GC most likely won't run quickly enough to run into troubles. One way to detect the problem would be to enforce GC, or at least increase its frequency. Is there any command line param or another means of doing it? If not, what other automated means of preventing such kind of errors have we got?










share|improve this question


















  • 1




    It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
    – Steven W. Klassen
    Nov 12 at 13:52












  • I would look into Java's AtomicReference.
    – Mike
    Nov 12 at 13:56






  • 1




    How would you make the assignment a = c;, if you hadn’t a reference to c?
    – Holger
    Nov 12 at 16:02
















0














Let's look at potential race condition related to garbage collection speed.



Initial state:



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


main program references object a, object a references b, b references c. Now, we want to make a reference c instead of b.



Good:



a => c


As b is no longer referenced, it can be garbage collected.



Bad:



a = null
a => c


In between these two steps (setting a to null and re-pointing it at c), b may become garbage collected together with c. I doubt these kind of errors may be picked up by unit tests, as GC most likely won't run quickly enough to run into troubles. One way to detect the problem would be to enforce GC, or at least increase its frequency. Is there any command line param or another means of doing it? If not, what other automated means of preventing such kind of errors have we got?










share|improve this question


















  • 1




    It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
    – Steven W. Klassen
    Nov 12 at 13:52












  • I would look into Java's AtomicReference.
    – Mike
    Nov 12 at 13:56






  • 1




    How would you make the assignment a = c;, if you hadn’t a reference to c?
    – Holger
    Nov 12 at 16:02














0












0








0







Let's look at potential race condition related to garbage collection speed.



Initial state:



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


main program references object a, object a references b, b references c. Now, we want to make a reference c instead of b.



Good:



a => c


As b is no longer referenced, it can be garbage collected.



Bad:



a = null
a => c


In between these two steps (setting a to null and re-pointing it at c), b may become garbage collected together with c. I doubt these kind of errors may be picked up by unit tests, as GC most likely won't run quickly enough to run into troubles. One way to detect the problem would be to enforce GC, or at least increase its frequency. Is there any command line param or another means of doing it? If not, what other automated means of preventing such kind of errors have we got?










share|improve this question













Let's look at potential race condition related to garbage collection speed.



Initial state:



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


main program references object a, object a references b, b references c. Now, we want to make a reference c instead of b.



Good:



a => c


As b is no longer referenced, it can be garbage collected.



Bad:



a = null
a => c


In between these two steps (setting a to null and re-pointing it at c), b may become garbage collected together with c. I doubt these kind of errors may be picked up by unit tests, as GC most likely won't run quickly enough to run into troubles. One way to detect the problem would be to enforce GC, or at least increase its frequency. Is there any command line param or another means of doing it? If not, what other automated means of preventing such kind of errors have we got?







java garbage-collection jvm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 13:41









automatictester

1,084622




1,084622








  • 1




    It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
    – Steven W. Klassen
    Nov 12 at 13:52












  • I would look into Java's AtomicReference.
    – Mike
    Nov 12 at 13:56






  • 1




    How would you make the assignment a = c;, if you hadn’t a reference to c?
    – Holger
    Nov 12 at 16:02














  • 1




    It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
    – Steven W. Klassen
    Nov 12 at 13:52












  • I would look into Java's AtomicReference.
    – Mike
    Nov 12 at 13:56






  • 1




    How would you make the assignment a = c;, if you hadn’t a reference to c?
    – Holger
    Nov 12 at 16:02








1




1




It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
– Steven W. Klassen
Nov 12 at 13:52






It isn't easy to answer your question given the "hand waviness" of the description. A specific code example would be more useful. That said, just because there is no reference to "b" does not mean it is collectible. To be collectible "b" must be "unreachable" from any of the garbage collection roots. Here is a good discussion of how it works. dynatrace.com/resources/ebooks/javabook/…
– Steven W. Klassen
Nov 12 at 13:52














I would look into Java's AtomicReference.
– Mike
Nov 12 at 13:56




I would look into Java's AtomicReference.
– Mike
Nov 12 at 13:56




1




1




How would you make the assignment a = c;, if you hadn’t a reference to c?
– Holger
Nov 12 at 16:02




How would you make the assignment a = c;, if you hadn’t a reference to c?
– Holger
Nov 12 at 16:02












2 Answers
2






active

oldest

votes


















2














You're plain wrong. Even "in between these two steps", there's a reference to c somewhere (hint: google for GC roots).



What you describe would imply a sure and fast crash for all java-running servers, but there's no such problem.






share|improve this answer





























    1














    If you are setting a reference within A to refer to C, how is it possible that C does not have a reference already in your program (a local variable or something at the very least) Are you getting C from thin air? The Garbage Collector won't even consider C for garbage collection.






    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%2f53263446%2fenforcing-jvm-gc-for-a-good-reason-i-hope%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You're plain wrong. Even "in between these two steps", there's a reference to c somewhere (hint: google for GC roots).



      What you describe would imply a sure and fast crash for all java-running servers, but there's no such problem.






      share|improve this answer


























        2














        You're plain wrong. Even "in between these two steps", there's a reference to c somewhere (hint: google for GC roots).



        What you describe would imply a sure and fast crash for all java-running servers, but there's no such problem.






        share|improve this answer
























          2












          2








          2






          You're plain wrong. Even "in between these two steps", there's a reference to c somewhere (hint: google for GC roots).



          What you describe would imply a sure and fast crash for all java-running servers, but there's no such problem.






          share|improve this answer












          You're plain wrong. Even "in between these two steps", there's a reference to c somewhere (hint: google for GC roots).



          What you describe would imply a sure and fast crash for all java-running servers, but there's no such problem.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 15:54









          maaartinus

          26.8k2193226




          26.8k2193226

























              1














              If you are setting a reference within A to refer to C, how is it possible that C does not have a reference already in your program (a local variable or something at the very least) Are you getting C from thin air? The Garbage Collector won't even consider C for garbage collection.






              share|improve this answer


























                1














                If you are setting a reference within A to refer to C, how is it possible that C does not have a reference already in your program (a local variable or something at the very least) Are you getting C from thin air? The Garbage Collector won't even consider C for garbage collection.






                share|improve this answer
























                  1












                  1








                  1






                  If you are setting a reference within A to refer to C, how is it possible that C does not have a reference already in your program (a local variable or something at the very least) Are you getting C from thin air? The Garbage Collector won't even consider C for garbage collection.






                  share|improve this answer












                  If you are setting a reference within A to refer to C, how is it possible that C does not have a reference already in your program (a local variable or something at the very least) Are you getting C from thin air? The Garbage Collector won't even consider C for garbage collection.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 16:04









                  jbx

                  10.4k1057109




                  10.4k1057109






























                      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%2f53263446%2fenforcing-jvm-gc-for-a-good-reason-i-hope%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

                      Bressuire

                      Vorschmack

                      Quarantine