Example usage for java.lang System runFinalization

List of usage examples for java.lang System runFinalization

Introduction

In this page you can find the example usage for java.lang System runFinalization.

Prototype

public static void runFinalization() 

Source Link

Document

Runs the finalization methods of any objects pending finalization.

Usage

From source file:org.callimachusproject.test.TemporaryServerFactory.java

private synchronized TemporaryServer createTemporaryServer() {
    try {//w ww . ja  va  2 s  . c  om
        final File dir = createCallimachus(origin);
        return new TemporaryServer() {
            private LocalRepositoryManager manager;
            private WebServer server;
            private CalliRepository repository;
            private boolean stopped;

            public synchronized void start() throws InterruptedException, Exception {
                manager = RepositoryProvider.getRepositoryManager(dir);
                File dataDir = manager.getRepositoryDir("callimachus");
                Repository repo = manager.getRepository("callimachus");
                repository = new CalliRepository(repo, dataDir);
                repository.setDatasourceManager(new DatasourceManager(manager, "callimachus") {
                    protected CalliRepository createCalliRepository(URI uri, Repository delegate, File dataDir)
                            throws OpenRDFException, IOException {
                        CalliRepository secondary;
                        secondary = super.createCalliRepository(uri, delegate, dataDir);
                        String uriSpace = repository.getChangeFolder();
                        String webapp = repository.getCallimachusWebapp(uriSpace);
                        secondary.setChangeFolder(uriSpace, webapp);
                        return secondary;
                    }
                });
                String url = repository.getCallimachusUrl(origin, CHANGES_PATH);
                String schema = repository.getCallimachusUrl(origin, SCHEMA_GRAPH);
                repository.addSchemaGraphType(schema);
                repository.setChangeFolder(url);
                repository.setCompileRepository(true);
                server = new WebServer(new File(dataDir, "cache/server"));
                server.addOrigin(origin, repository);
                server.listen(new int[] { port }, new int[0]);
                server.start();
                HttpHost host = URIUtils.extractHost(java.net.URI.create(origin + "/"));
                HttpClientFactory.getInstance().setProxy(host, server);
                Thread.sleep(100);
            }

            public synchronized void pause() throws Exception {
            }

            public synchronized void resume() throws Exception {
                synchronized (running) {
                    TemporaryServer other = running.get(port);
                    if (stopped || !this.equals(other)) {
                        if (other != null) {
                            other.stop();
                        }
                        start();
                        running.put(port, this);
                    }
                }
            }

            public synchronized void stop() throws Exception {
                if (!stopped) {
                    server.stop();
                    server.destroy();
                    server = null;
                    repository = null;
                    manager.shutDown();
                    stopped = true;
                    Thread.sleep(100);
                    System.gc();
                    System.runFinalization();
                }
            }

            public synchronized void destroy() throws Exception {
                stop();
                FileUtil.deltree(dir);
            }

            @Override
            protected void finalize() throws Exception {
                destroy();
            }

            public String getOrigin() {
                return origin;
            }

            public String getUsername() {
                return email;
            }

            public char[] getPassword() {
                return password;
            }

            public CalliRepository getRepository() {
                return this.repository;
            }

            public String toString() {
                return server.toString();
            }
        };
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:BenchmarkApplet.java

public static void gc() {
      System.runFinalization();
      System.gc();//from   www  . j a v a  2 s  . c  om
      if (Runtime.getRuntime().freeMemory() < gcMemTarget) {
          try {
              int[] mem = new int[(int) gcMemTarget / 4];
              mem = null;
          } catch (OutOfMemoryError e) {
              gcMemTarget -= 10000;
              recalibrate();
          }
          System.gc();
      }
      try {
          Thread.sleep(100);
      } catch (InterruptedException e) {
      }
  }

From source file:org.kiji.schema.testutil.AbstractKijiIntegrationTest.java

@AfterClass
public static void teardownManagementThreads() {
    synchronized (THREAD_MANAGEMENT_LOCK) {
        // Should always have a permit available since each thread offers one
        RUNNING_TEST_SEMAPHORE.tryAcquire();

        // Last one out shuts off the lights.
        if (RUNNING_TEST_SEMAPHORE.availablePermits() == 0) {
            // Put the remaining created instances into the deletion queue
            mCreationThread.stopKijiCreation();
            KijiURI unusedKijiURI = mCreationThread.getKijiForCleanup();
            while (CLEANUP_AFTER_TEST && (null != unusedKijiURI)) {
                try {
                    mDeletionThread.destroyKiji(unusedKijiURI);
                } catch (InterruptedException exn) {
                    LOG.error("Failed to put Kiji instance '{}' into the deletion queue: {}", unusedKijiURI,
                            exn.getMessage());
                    continue;
                }// w  w w  .  j  av  a  2 s.c o m
                unusedKijiURI = mCreationThread.getKijiForCleanup();
            }

            // Finish deleting and clear the threads
            if (CLEANUP_AFTER_TEST) {
                mDeletionThread.waitForCompletion();
            }
            mCreationThread = null;
            mDeletionThread = null;

            // Force garbage compaction to find any remaining references to opened tables, etc.
            System.gc();
            System.runFinalization();
        }
    }
}

From source file:eu.liveandgov.ar.core.ARViewFragment.java

/**
 * Delete metaioSDK object, unregister sensors, call garbage collector 
 *///from  w ww  .j a v a 2 s.  c o m
@Override
public void onDestroy() {

    deb("ARViewFragment", "onDestroy");
    super.onDestroy();

    if (!isDestroyed) {
        MetaioDebug.log("ARViewActivity.onDestroy");

        if (metaioSDK != null) {
            metaioSDK.delete();
            metaioSDK = null;
        }

        MetaioDebug.log("ARViewActivity.onDestroy releasing sensors");
        if (mSensors != null) {
            mSensors.registerCallback(null);
            mSensors.release();
            mSensors.delete();
            mSensors = null;
        }

        Memory.unbindViews(v);

        System.runFinalization();
        System.gc();
        isDestroyed = true;
    }
}

From source file:org.glom.web.server.libglom.DocumentTest.java

@Test
public void testGetNumericFormat() {
    final List<String> tableNames = document.getTableNames();

    for (int i = 0; i < tableNames.size(); i++) {
        final String table = tableNames.get(i);
        final List<LayoutGroup> layoutList = document.getDataLayoutGroups(Document.LAYOUT_NAME_LIST, table);
        assertTrue(!layoutList.isEmpty());
        final LayoutGroup firstgroup = layoutList.get(0);
        assertTrue(firstgroup != null);/*w  ww.  j  a v a2 s .  c  om*/
        final List<LayoutItem> layoutItems = firstgroup.getItems();
        final int numItems = safeLongToInt(layoutItems.size());
        for (int j = 0; j < numItems; j++) {
            final LayoutItem item = layoutItems.get(j);
            assertTrue(item != null);

            if (item instanceof LayoutItemField) {
                final LayoutItemField itemField = (LayoutItemField) item;
                // don't keep a reference to the FeildFormatting object
                final NumericFormat numFormat = itemField.getFormattingUsed().getNumericFormat();
                assertTrue(numFormat != null);

                // get the values
                final boolean altForegroundColorForNegatives = numFormat.getUseAltForegroundColorForNegatives();
                final String currencySymbol = numFormat.getCurrencySymbol();
                final long decimalPlaces = numFormat.getDecimalPlaces();
                final boolean decimalPlacesRestricted = numFormat.getDecimalPlacesRestricted();
                final boolean useThousandsSepator = numFormat.getUseThousandsSeparator();
                final String alternativeColorForNegatives = NumericFormat
                        .getAlternativeColorForNegativesAsHTMLColor();
                final long defaultPrecision = NumericFormat.getDefaultPrecision();

                // Simulate a garbage collection
                System.gc();
                System.runFinalization();

                // re-get the values and test
                assertEquals(altForegroundColorForNegatives, numFormat.getUseAltForegroundColorForNegatives());
                assertEquals(currencySymbol, numFormat.getCurrencySymbol());
                assertEquals(decimalPlaces, numFormat.getDecimalPlaces());
                assertEquals(decimalPlacesRestricted, numFormat.getDecimalPlacesRestricted());
                assertEquals(useThousandsSepator, numFormat.getUseThousandsSeparator());
                assertEquals(alternativeColorForNegatives,
                        NumericFormat.getAlternativeColorForNegativesAsHTMLColor());
                assertEquals(defaultPrecision, NumericFormat.getDefaultPrecision());

            }
        }
    }
}

From source file:org.xwoot.jxta.mock.MockJxtaPeer.java

/** {@inheritDoc} **/
public void stopNetwork() {
    this.logger.info("Stopping network.");

    if (this.isConnectedToNetwork()) {
        //PeerGroup currentGroup = this.currentJoinedGroup;

        // Try to leave the current group nicely.
        try {//from   w  ww. ja va2 s .c om
            this.leavePeerGroup(currentJoinedGroup);
        } catch (Exception e) {
            // ignore, we are shutting down anyway.
        }

        this.currentJoinedGroup = null;

        this.rootGroup = null;

        this.connectedToNetwork = false;

        System.runFinalization();
        System.gc();

    } else {
        this.logger.warn("Network already stopped.");
    }
}

From source file:org.xwoot.jxta.JxtaPeer.java

/** {@inheritDoc} **/
public void stopNetwork() {
    this.logger.info("Stopping network.");

    if (this.isConnectedToNetwork() || this.isJxtaStarted()) {
        //PeerGroup currentGroup = this.currentJoinedGroup;

        // Try to leave the current group nicely.
        try {//from  w  w w.  j  a  v  a 2 s.co m
            this.leavePeerGroup(currentJoinedGroup);
        } catch (Exception e) {
            // ignore, we are shutting down anyway.
            this.logger
                    .warn("Failed to leave group " + this.currentJoinedGroup + " while stopping jxta network.");
        }

        //         currentGroup.stopApp();
        //         currentGroup.unref();
        //         currentGroup = null;

        this.currentJoinedGroup = null;

        //         this.rootGroup.stopApp();
        //         this.rootGroup.unref();
        this.rootGroup = null;

        manager.stopNetwork();

        System.runFinalization();
        System.gc();

    } else {
        this.logger.warn("Network already stopped.");
    }
}

From source file:org.geoserver.test.GeoServerSystemTestSupport.java

protected final void tearDown(SystemTestData testData) throws Exception {
    if (testData.isTestDataAvailable()) {
        onTearDown(testData);/*w  ww  .ja  va  2s. c om*/

        destroyGeoServer();

        TestHttpClientProvider.endTest();

        // some tests do need a kick on the GC to fully clean up
        if (isMemoryCleanRequired()) {
            System.gc();
            System.runFinalization();
        }
    }
}

From source file:org.billthefarmer.tuner.MainActivity.java

@Override
protected void onDestroy() {
    super.onDestroy();

    // Get rid of all those pesky objects
    audio = null;/*from   w ww.j ava2 s.  c om*/
    scope = null;
    spectrum = null;
    display = null;
    strobe = null;
    meter = null;
    status = null;
    signal = null;
    toast = null;

    // Hint that it might be a good idea
    System.runFinalization();
}

From source file:org.springframework.boot.devtools.restart.Restarter.java

/**
 * Stop the application./* w ww .  j a  v a2  s  .  co m*/
 * @throws Exception in case of errors
 */
protected void stop() throws Exception {
    this.logger.debug("Stopping application");
    this.stopLock.lock();
    try {
        triggerShutdownHooks();
        cleanupCaches();
        if (this.forceReferenceCleanup) {
            forceReferenceCleanup();
        }
    } finally {
        this.stopLock.unlock();
    }
    System.gc();
    System.runFinalization();
}