<legend id='ipTBZ'><style id='ipTBZ'><dir id='ipTBZ'><q id='ipTBZ'></q></dir></style></legend><tfoot id='ipTBZ'></tfoot>
  • <i id='ipTBZ'><tr id='ipTBZ'><dt id='ipTBZ'><q id='ipTBZ'><span id='ipTBZ'><b id='ipTBZ'><form id='ipTBZ'><ins id='ipTBZ'></ins><ul id='ipTBZ'></ul><sub id='ipTBZ'></sub></form><legend id='ipTBZ'></legend><bdo id='ipTBZ'><pre id='ipTBZ'><center id='ipTBZ'></center></pre></bdo></b><th id='ipTBZ'></th></span></q></dt></tr></i><div id='ipTBZ'><tfoot id='ipTBZ'></tfoot><dl id='ipTBZ'><fieldset id='ipTBZ'></fieldset></dl></div>

    <small id='ipTBZ'></small><noframes id='ipTBZ'>

      <bdo id='ipTBZ'></bdo><ul id='ipTBZ'></ul>

        垃圾收集中的非直观对象驱逐

        Non-intuitive object eviction from garbage collection(垃圾收集中的非直观对象驱逐)
        <tfoot id='Q0cf5'></tfoot>

          • <bdo id='Q0cf5'></bdo><ul id='Q0cf5'></ul>
            <i id='Q0cf5'><tr id='Q0cf5'><dt id='Q0cf5'><q id='Q0cf5'><span id='Q0cf5'><b id='Q0cf5'><form id='Q0cf5'><ins id='Q0cf5'></ins><ul id='Q0cf5'></ul><sub id='Q0cf5'></sub></form><legend id='Q0cf5'></legend><bdo id='Q0cf5'><pre id='Q0cf5'><center id='Q0cf5'></center></pre></bdo></b><th id='Q0cf5'></th></span></q></dt></tr></i><div id='Q0cf5'><tfoot id='Q0cf5'></tfoot><dl id='Q0cf5'><fieldset id='Q0cf5'></fieldset></dl></div>

            <legend id='Q0cf5'><style id='Q0cf5'><dir id='Q0cf5'><q id='Q0cf5'></q></dir></style></legend>

            <small id='Q0cf5'></small><noframes id='Q0cf5'>

                    <tbody id='Q0cf5'></tbody>
                • 本文介绍了垃圾收集中的非直观对象驱逐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm debugging a memory leak and had to dive into CompletableFuture internals. There is this piece of code (CompletableFuture.uniComposeStage):

                  CompletableFuture<V> g = f.apply(t).toCompletableFuture();
                  ...
                  CompletableFuture<V> d = new CompletableFuture<V>();
                  UniRelay<V> copy = new UniRelay<V>(d, g);
                  g.push(copy);
                  copy.tryFire(SYNC);
                  return d;
                  

                  The code itself is quite clear to me: apply a function that returns CompletionStage (g), create a relay that eventually will transfer value to another CompletableFuture (d), then return this another future (d). I see following reference situation:

                  • copy references both d and g (and there is no magic in constructor, only field assignments)
                  • g references copy
                  • d references nothing

                  Only d is returned, so, in fact, both g and copy seem as internal method variables to me, that (on first sight) should never leave the method and be eventually gc'd. Both naive testing and the fact that it was written long ago by proven developers suggests me that i'm wrong and missing something. What is the reason that make those objects being omitted from garbage collection?

                  解决方案

                  In the cited code, there is nothing preventing garbage collection of these futures and there is no need to. This code in question applies to the scenario that the first CompletableFuture (the this instance) has been completed and the CompletableFuture returned by the directly evaluated compose function has not completed yet.

                  Now, there are two possible scenarios

                  1. There is an ongoing completion attempt. Then, the code which will eventually complete the future will hold a reference to it and when completing, it will trigger the completion of the dependent stages (registered via g.push(copy)). In this scenario, there is no need for the dependent stage to hold a reference to its prerequisite stage.

                    This is a general pattern. If there is a chain x --will complete-→ y, there will be no reference from y to x.

                  2. There is no other reference to that CompletableFuture instance g and g has not been completed yet. In this case, it will never be completed at all and holding a reference to g internally wouldn’t change that. That would only waste resources.

                  The following example program will illustrate this:

                  public static void main(String[] args) throws Throwable {
                      ReferenceQueue<Object> discovered = new ReferenceQueue<>();
                      Set<WeakReference<?>> holder = new HashSet<>();
                  
                      CompletableFuture<Object> initial = CompletableFuture.completedFuture("somevalue");
                  
                      CompletableFuture<Object> finalStage = initial.thenCompose(value -> {
                          CompletableFuture<Object> lost = new CompletableFuture<>();
                          holder.add(new WeakReference<>(lost, discovered));
                          return lost;
                      });
                      waitFor(finalStage, holder, discovered);
                      finalStage = initial.thenCompose(value -> {
                          CompletableFuture<Object> saved = CompletableFuture.supplyAsync(()-> {
                              LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
                              return "newvalue";
                          });
                          holder.add(new WeakReference<>(saved, discovered));
                          return saved;
                      });
                      waitFor(finalStage, holder, discovered);
                  }
                  private static void waitFor(CompletableFuture<Object> f, Set<WeakReference<?>> holder,
                                      ReferenceQueue<Object> discovered) throws InterruptedException {
                      while(!f.isDone() && !holder.isEmpty()) {
                          System.gc();
                          Reference<?> removed = discovered.remove(100);
                          if(removed != null) {
                              holder.remove(removed);
                              System.out.println("future has been garbage collected");
                          }
                      }
                      if(f.isDone()) {
                          System.out.println("stage completed with "+f.join());
                          holder.clear();
                      }
                  }
                  

                  The first function passed to thenCompose creates and returns a new uncompleted CompletableFuture without any attempt to complete it, not holding nor storing any other reference to it. In contrast, the second function creates the CompletableFuture via supplyAsync providing a Supplier which will return a value after a second.

                  On my system, it consistently printed

                  future has been garbage collected
                  stage completed with newvalue
                  

                  showing that the abandoned future will not be prevented from garbage collection while the other will be held at least until completion.

                  这篇关于垃圾收集中的非直观对象驱逐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  <tfoot id='HOc83'></tfoot>

                      <tbody id='HOc83'></tbody>

                      <small id='HOc83'></small><noframes id='HOc83'>

                          <bdo id='HOc83'></bdo><ul id='HOc83'></ul>

                        • <i id='HOc83'><tr id='HOc83'><dt id='HOc83'><q id='HOc83'><span id='HOc83'><b id='HOc83'><form id='HOc83'><ins id='HOc83'></ins><ul id='HOc83'></ul><sub id='HOc83'></sub></form><legend id='HOc83'></legend><bdo id='HOc83'><pre id='HOc83'><center id='HOc83'></center></pre></bdo></b><th id='HOc83'></th></span></q></dt></tr></i><div id='HOc83'><tfoot id='HOc83'></tfoot><dl id='HOc83'><fieldset id='HOc83'></fieldset></dl></div>
                          1. <legend id='HOc83'><style id='HOc83'><dir id='HOc83'><q id='HOc83'></q></dir></style></legend>