当前位置: 首页>>代码示例>>Java>>正文


Java GcFinalization类代码示例

本文整理汇总了Java中com.google.common.testing.GcFinalization的典型用法代码示例。如果您正苦于以下问题:Java GcFinalization类的具体用法?Java GcFinalization怎么用?Java GcFinalization使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


GcFinalization类属于com.google.common.testing包,在下文中一共展示了GcFinalization类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testFinalizeDeletesFile

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testFinalizeDeletesFile() throws Exception {
  byte[] data = newPreFilledByteArray(100);
  FileBackedOutputStream out = new FileBackedOutputStream(0, true);

  write(out, data, 0, 100, true);
  final File file = out.getFile();
  assertEquals(100, file.length());
  assertTrue(file.exists());
  out.close();

  // Make sure that finalize deletes the file
  out = null;

  // times out and throws RuntimeException on failure
  GcFinalization.awaitDone(new GcFinalization.FinalizationPredicate() {
    @Override
    public boolean isDone() {
      return !file.exists();
    }
  });
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:22,代码来源:FileBackedOutputStreamTest.java

示例2: testInputGCedIfUnreferenced

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
@AndroidIncompatible // reference is never cleared under some versions of the emulator
@GwtIncompatible
public void testInputGCedIfUnreferenced() throws Exception {
  SettableFuture<Long> future1 = SettableFuture.create();
  SettableFuture<Long> future2 = SettableFuture.create();
  WeakReference<SettableFuture<Long>> future1Ref = new WeakReference<>(future1);
  WeakReference<SettableFuture<Long>> future2Ref = new WeakReference<>(future2);

  ImmutableList<ListenableFuture<Long>> delegates =
      inCompletionOrder(ImmutableList.<ListenableFuture<Long>>of(future1, future2));

  future1.set(1L);

  future1 = null;
  // First future is complete, should be unreferenced
  GcFinalization.awaitClear(future1Ref);
  ListenableFuture<Long> outputFuture1 = delegates.get(0);
  delegates = null;
  future2 = null;
  // No references to list or other output future, second future should be unreferenced
  GcFinalization.awaitClear(future2Ref);
  outputFuture1.get();
}
 
开发者ID:google,项目名称:guava,代码行数:24,代码来源:FuturesTest.java

示例3: testFinalizeDeletesFile

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testFinalizeDeletesFile() throws Exception {
  byte[] data = newPreFilledByteArray(100);
  FileBackedOutputStream out = new FileBackedOutputStream(0, true);

  write(out, data, 0, 100, true);
  final File file = out.getFile();
  assertEquals(100, file.length());
  assertTrue(file.exists());
  out.close();

  // Make sure that finalize deletes the file
  out = null;

  // times out and throws RuntimeException on failure
  GcFinalization.awaitDone(
      new GcFinalization.FinalizationPredicate() {
        @Override
        public boolean isDone() {
          return !file.exists();
        }
      });
}
 
开发者ID:google,项目名称:guava,代码行数:23,代码来源:FileBackedOutputStreamTest.java

示例4: testProxyClassUnloading

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testProxyClassUnloading() {
  Object testObject =
      Guice.createInjector(interceptorModule, testModule).getInstance(proxyTestClass);
  assertNotNull(testObject.getClass().getClassLoader());
  assertNotSame(testObject.getClass().getClassLoader(), systemClassLoader);

  // take a weak reference to the generated proxy class
  WeakReference<Class<?>> clazzRef = new WeakReference<Class<?>>(testObject.getClass());

  assertNotNull(clazzRef.get());

  // null the proxy
  testObject = null;

  /*
   * this should be enough to queue the weak reference
   * unless something is holding onto it accidentally.
   */
  GcFinalization.awaitClear(clazzRef);

  // This test could be somewhat flaky when the GC isn't working.
  // If it fails, run the test again to make sure it's failing reliably.
  assertNull("Proxy class was not unloaded.", clazzRef.get());
}
 
开发者ID:google,项目名称:guice,代码行数:25,代码来源:BytecodeGenTest.java

示例5: invalidatedValuesAreGCedAsExpected

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
@Test
public void invalidatedValuesAreGCedAsExpected() throws Exception {
  SkyKey key = GraphTester.skyKey("a");
  HeavyValue heavyValue = new HeavyValue();
  WeakReference<HeavyValue> weakRef = new WeakReference<>(heavyValue);
  tester.set("a", heavyValue);

  graph = new InMemoryGraphImpl();
  eval(false, key);
  invalidate(graph, new DirtyTrackingProgressReceiver(null), key);

  tester = null;
  heavyValue = null;
  if (gcExpected()) {
    GcFinalization.awaitClear(weakRef);
  } else {
    // Not a reliable check, but better than nothing.
    System.gc();
    Thread.sleep(300);
    assertThat(weakRef.get()).isNotNull();
  }
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:23,代码来源:EagerInvalidatorTest.java

示例6: deleteInvalidatedValue

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
@Test
public void deleteInvalidatedValue() throws Exception {
  initializeTester();
  SkyKey top = GraphTester.toSkyKey("top");
  SkyKey toDelete = GraphTester.toSkyKey("toDelete");
  // Must be a concatenation -- COPY doesn't actually copy.
  tester.getOrCreate(top).addDependency(toDelete).setComputedValue(CONCATENATE);
  tester.set(toDelete, new StringValue("toDelete"));
  SkyValue value = tester.evalAndGet("top");
  SkyKey forceInvalidation = GraphTester.toSkyKey("forceInvalidation");
  tester.set(forceInvalidation, new StringValue("forceInvalidation"));
  tester.getOrCreate(toDelete, /*markAsModified=*/true);
  tester.invalidate();
  tester.eval(/*keepGoing=*/false, forceInvalidation);
  tester.delete("toDelete");
  WeakReference<SkyValue> ref = new WeakReference<>(value);
  value = null;
  tester.eval(/*keepGoing=*/false, forceInvalidation);
  tester.invalidate(); // So that invalidation receiver doesn't hang on to reference.
  GcFinalization.awaitClear(ref);
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:22,代码来源:MemoizingEvaluatorTest.java

示例7: testMultiThreadedUse

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
private void testMultiThreadedUse(int numMainExceptions, int numSuppressedPerMain)
    throws InterruptedException {
  CountDownLatch latch = new CountDownLatch(numMainExceptions * numSuppressedPerMain);
  ConcurrentWeakIdentityHashMap map =
      testFunctionalCorrectnessForMultiThreadedUse(
          numMainExceptions, numSuppressedPerMain, latch);
  /*
   * Calling the following methods multiple times to make sure the keys are garbage collected,
   * and their corresponding entries are removed from the map.
   */
  map.deleteEmptyKeys();
  GcFinalization.awaitFullGc();
  map.deleteEmptyKeys();
  GcFinalization.awaitFullGc();
  map.deleteEmptyKeys();

  assertThat(map.size()).isEqualTo(0);
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:19,代码来源:ConcurrentWeakIdentityHashMapTest.java

示例8: testWeakKeySet_integration_mapbinder

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testWeakKeySet_integration_mapbinder() {
  Key<Map<String, String>> mapKey = Key.get(new TypeLiteral<Map<String, String>>() {});
  
  Injector parentInjector = Guice.createInjector(new AbstractModule() {
        @Override protected void configure() {
          bind(String.class).toInstance("hi");
        }
      });
  WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);

  Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {
    @Override protected void configure() {
      MapBinder<String, String> binder =
          MapBinder.newMapBinder(binder(), String.class, String.class);
      binder.addBinding("bar").toInstance("foo");
    }
  });
  WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
  WeakKeySetUtils.assertBlacklisted(parentInjector, mapKey);
  
  // Clear the ref, GC, and ensure that we are no longer blacklisting.
  childInjector = null;
  
  GcFinalization.awaitClear(weakRef);
  WeakKeySetUtils.assertNotBlacklisted(parentInjector, mapKey);
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:27,代码来源:MapBinderTest.java

示例9: testWeakKeySet_integration

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testWeakKeySet_integration() {   
  Injector parentInjector = Guice.createInjector(new AbstractModule() {
        @Override protected void configure() {
          bind(String.class).toInstance("hi");
        }
      });
  WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));

  Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {
    @Override protected void configure() {
      OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault().toInstance(4);
    }
  });
  WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
  WeakKeySetUtils.assertBlacklisted(parentInjector, Key.get(Integer.class));
  
  // Clear the ref, GC, and ensure that we are no longer blacklisting.
  childInjector = null;
  
  GcFinalization.awaitClear(weakRef);
  WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:23,代码来源:OptionalBinderTest.java

示例10: testEviction

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testEviction() {
  TestState state = new TestState();
  Key<Integer> key = Key.get(Integer.class);
  Object source = new Object();
  
  WeakReference<Key<Integer>> weakKeyRef = new WeakReference<Key<Integer>>(key);

  set.add(key, state, source);
  assertTrue(set.contains(key));
  assertEquals(1, set.getSources(key).size());
  assertTrue(set.getSources(key).contains(source));

  state = null;

  GcFinalization.awaitFullGc();

  assertFalse(set.contains(Key.get(Integer.class)));
  assertNull(set.getSources(Key.get(Integer.class)));

  // Ensure there are no hanging references.
  key = null;
  GcFinalization.awaitClear(weakKeyRef);
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:24,代码来源:WeakKeySetTest.java

示例11: testEviction_nullSource

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testEviction_nullSource() {
  TestState state = new TestState();
  Key<Integer> key = Key.get(Integer.class);
  Object source = null;
  
  WeakReference<Key<Integer>> weakKeyRef = new WeakReference<Key<Integer>>(key);

  set.add(key, state, source);
  assertTrue(set.contains(key));
  assertEquals(1, set.getSources(key).size());
  assertTrue(set.getSources(key).contains(source));

  state = null;

  GcFinalization.awaitFullGc();

  assertFalse(set.contains(Key.get(Integer.class)));
  assertNull(set.getSources(Key.get(Integer.class)));

  // Ensure there are no hanging references.
  key = null;
  GcFinalization.awaitClear(weakKeyRef);
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:24,代码来源:WeakKeySetTest.java

示例12: testWeakKeySet_integration

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testWeakKeySet_integration() {
  Injector parentInjector = Guice.createInjector(new AbstractModule() {
        @Override protected void configure() {
          bind(Integer.class).toInstance(4);
        }
      });
  assertNotBlacklisted(parentInjector, Key.get(String.class));

  Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {
    @Override protected void configure() {
      bind(String.class).toInstance("bar");
    }
  });
  WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
  assertBlacklisted(parentInjector, Key.get(String.class));
  
  // Clear the ref, GC, and ensure that we are no longer blacklisting.
  childInjector = null;
  GcFinalization.awaitClear(weakRef);
  assertNotBlacklisted(parentInjector, Key.get(String.class));
}
 
开发者ID:cgruber,项目名称:guice-old,代码行数:22,代码来源:WeakKeySetTest.java

示例13: testFreesNextReference

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
@GwtIncompatible // weak references
public void testFreesNextReference() {
  Iterator<Object> itr = new AbstractIterator<Object>() {
    @Override public Object computeNext() {
      return new Object();
    }
  };
  WeakReference<Object> ref = new WeakReference<Object>(itr.next());
  GcFinalization.awaitClear(ref);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:11,代码来源:AbstractIteratorTest.java

示例14: testUnloadableInStaticFieldIfClosed

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
public void testUnloadableInStaticFieldIfClosed() throws Exception {
  Policy oldPolicy = Policy.getPolicy();
  SecurityManager oldSecurityManager = System.getSecurityManager();
  try {
    Policy.setPolicy(new PermissivePolicy());
    System.setSecurityManager(new SecurityManager());
    WeakReference<ClassLoader> loaderRef = doTestUnloadableInStaticFieldIfClosed();
    GcFinalization.awaitClear(loaderRef);
  } finally {
    System.setSecurityManager(oldSecurityManager);
    Policy.setPolicy(oldPolicy);
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:14,代码来源:FinalizableReferenceQueueClassLoaderUnloadingTest.java

示例15: doTestUnloadableInStaticFieldIfClosed

import com.google.common.testing.GcFinalization; //导入依赖的package包/类
private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception {
  final URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
  final URL[] urls = myLoader.getURLs();
  URLClassLoader sepLoader = new URLClassLoader(urls, myLoader.getParent());

  Class<?> frqC = FinalizableReferenceQueue.class;
  Class<?> sepFrqC = sepLoader.loadClass(frqC.getName());
  assertNotSame(frqC, sepFrqC);

  Class<?> sepFrqSystemLoaderC =
      sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName());
  Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled");
  disabled.setAccessible(true);
  disabled.set(null, true);

  Class<?> frqUserC = FrqUser.class;
  Class<?> sepFrqUserC = sepLoader.loadClass(frqUserC.getName());
  assertNotSame(frqUserC, sepFrqUserC);
  assertSame(sepLoader, sepFrqUserC.getClassLoader());

  Callable<?> sepFrqUser = (Callable<?>) sepFrqUserC.newInstance();
  WeakReference<?> finalizableWeakReference = (WeakReference<?>) sepFrqUser.call();

  GcFinalization.awaitClear(finalizableWeakReference);

  Field sepFrqUserFinalizedF = sepFrqUserC.getField("finalized");
  Semaphore finalizeCount = (Semaphore) sepFrqUserFinalizedF.get(null);
  boolean finalized = finalizeCount.tryAcquire(5, TimeUnit.SECONDS);
  assertTrue(finalized);

  Field sepFrqUserFrqF = sepFrqUserC.getField("frq");
  Closeable frq = (Closeable) sepFrqUserFrqF.get(null);
  frq.close();

  return new WeakReference<ClassLoader>(sepLoader);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:37,代码来源:FinalizableReferenceQueueClassLoaderUnloadingTest.java


注:本文中的com.google.common.testing.GcFinalization类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。