Java Code Examples for java.lang.ref.WeakReference#clear()

The following examples show how to use java.lang.ref.WeakReference#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FolderFileAccess.java    From BlueMap with MIT License 6 votes vote down vote up
@Override
public synchronized void close() throws IOException {
	IOException exception = null;
	
	for (WeakReference<InputStream> streamRef : openedStreams) {
		try {
			InputStream stream = streamRef.get();
			if (stream != null) stream.close();
			streamRef.clear();
		} catch (IOException ex) {
			if (exception == null) exception = ex;
			else exception.addSuppressed(ex);
		}
	}
	
	if (exception != null) throw exception;
	
	openedStreams.clear(); //only clear if no exception is thrown
}
 
Example 2
Source File: ThreadLocalCleaner.java    From gwtmockito with Apache License 2.0 6 votes vote down vote up
/**
 * Remove ThreadLocalMap entries of objects loaded through the given classLoader as they prevent garbage collection
 * of this classLoader while the thread that holds the ThreadLocalMap is alive
 */
public static void cleanUpThreadLocalValues(ClassLoader classLoader) {
    try {
        Object threadLocalMap = getPrivateAttribute(Thread.class, "threadLocals", Thread.currentThread());
        WeakReference[] table = (WeakReference[]) getPrivateAttribute("table", threadLocalMap);
        int length = table.length;
        for (int i = 0; i < length; i++) {
            WeakReference mapEntry = table[i];
            if (mapEntry != null && mapEntry.get() != null) {
                ClassLoader mapEntryKeyClassLoader = mapEntry.get().getClass().getClassLoader();
                Field mapEntryValueField = getPrivateAttributeAccessibleField(mapEntry.getClass(), "value");
                Object value = mapEntryValueField.get(mapEntry);
                if (value != null) {
                    ClassLoader mapEntryValueClassLoader = value.getClass().getClassLoader();
                    if (mapEntryKeyClassLoader == classLoader || mapEntryValueClassLoader == classLoader) {
                        mapEntry.clear();
                        mapEntryValueField.set(mapEntry, null);
                        // The ThreadLocalMap is able to expunge the remaining stale entries, no need to remove it from the map
                    }
                }
            }
        }
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new AssertionError("Unable to access expected class fields for cleaning ThreadLocal values", e);
    }
}
 
Example 3
Source File: AndroidJobStrategy.java    From cloudinary_android with MIT License 6 votes vote down vote up
private void killAllThreads() {
    synchronized (threadsMapLockObject) {
        for (String requestId : threads.keySet()) {
            WeakReference<Thread> ref = threads.get(requestId);
            Thread thread = ref.get();

            if (thread != null) {
                thread.interrupt();
            }

            ref.clear();
        }

        threads.clear();
    }
}
 
Example 4
Source File: CursorableLinkedList.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a {@link CursorableLinkedList.Cursor} from
 * the set of cursors to be notified of changes to this list.
 */
protected void unregisterCursor(Cursor cur) {
    for (Iterator it = _cursors.iterator(); it.hasNext(); ) {
        WeakReference ref = (WeakReference) it.next();
        Cursor cursor = (Cursor) ref.get();
        if (cursor == null) {
            // some other unrelated cursor object has been 
            // garbage-collected; let's take the opportunity to
            // clean up the cursors list anyway..
            it.remove();
            
        } else if (cursor == cur) {
            ref.clear();
            it.remove();
            break;
        }
    }
}
 
Example 5
Source File: DefaultByteBufferPool.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void close() {
    if (closed) {
        return;
    }
    closed = true;
    queue.clear();

    synchronized (threadLocalDataList) {
        for (WeakReference<ThreadLocalData> ref : threadLocalDataList) {
            ThreadLocalData local = ref.get();
            if (local != null) {
                local.buffers.clear();
            }
            ref.clear();
        }
        threadLocalDataList.clear();
    }
}
 
Example 6
Source File: MusicLibraryPagerAdapter.java    From VinylMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
    final Fragment mFragment = (Fragment) super.instantiateItem(container, position);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
    mFragmentArray.put(position, new WeakReference<>(mFragment));
    return mFragment;
}
 
Example 7
Source File: ThreadLocalCachedStorageAndLockingService.java    From tus-java-server with MIT License 5 votes vote down vote up
private void cleanupCache() {
    WeakReference<UploadInfo> ref = uploadInfoCache.get();
    if (ref != null) {
        uploadInfoCache.remove();
        ref.clear();
    }
}
 
Example 8
Source File: ArtistDetailPagerAdapter.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
    super.destroyItem(container, position, object);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
}
 
Example 9
Source File: ArtistDetailPagerAdapter.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
    final Fragment mFragment = (Fragment) super.instantiateItem(container, position);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
    mFragmentArray.put(position, new WeakReference<>(mFragment));
    return mFragment;
}
 
Example 10
Source File: XulWorker.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
public XulDrawable remove(String key) {
	WeakReference<XulDrawable> xulDrawableWeakReference = _cache.remove(key);
	if (xulDrawableWeakReference == null) {
		return null;
	}
	XulDrawable xulDrawable = xulDrawableWeakReference.get();
	xulDrawableWeakReference.clear();
	return xulDrawable;
}
 
Example 11
Source File: MusicLibraryPagerAdapter.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
    final Fragment mFragment = (Fragment) super.instantiateItem(container, position);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
    mFragmentArray.put(position, new WeakReference<>(mFragment));
    return mFragment;
}
 
Example 12
Source File: InternetAvailabilityChecker.java    From InternetAvailabilityChecker with Apache License 2.0 5 votes vote down vote up
public void removeAllInternetConnectivityChangeListeners() {
    if (mInternetConnectivityListenersWeakReferences == null) {
        return;
    }

    Iterator<WeakReference<InternetConnectivityListener>> iterator = mInternetConnectivityListenersWeakReferences.iterator();
    while (iterator.hasNext()) {
        WeakReference<InternetConnectivityListener> reference = iterator.next();
        if (reference != null) {
            reference.clear();
        }
        iterator.remove();
    }
    unregisterNetworkChangeReceiver();
}
 
Example 13
Source File: ConsolePanel.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        extension.getExtScript().invokeScript(script);
    } catch (Exception e) {
        getOutputPanel().append(e);
    } finally {
        WeakReference<ScriptExecutorThread> refScriptExecutorThread =
                runnableScriptsToThreadMap.remove(script);
        if (refScriptExecutorThread != null) {
            refScriptExecutorThread.clear();
        }
        updateButtonsState();
    }
}
 
Example 14
Source File: WeatherPagerAdapter.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
	Log.i(TAG, "instantiateItem...");
	Fragment fragment = (Fragment) super.instantiateItem(container,
			position);
	final WeakReference<Fragment> weakFragment = mFragmentArray
			.get(position);
	if (weakFragment != null) {
		weakFragment.clear();
	}
	mFragmentArray.put(position, new WeakReference<Fragment>(fragment));

	return fragment;
}
 
Example 15
Source File: MusicLibraryPagerAdapter.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
    final Fragment mFragment = (Fragment) super.instantiateItem(container, position);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
    mFragmentArray.put(position, new WeakReference<>(mFragment));
    return mFragment;
}
 
Example 16
Source File: CursorableLinkedList.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Informs all of my registered cursors that they are now
 * invalid.
 */
protected void invalidateCursors() {
    Iterator it = _cursors.iterator();
    while (it.hasNext()) {
        WeakReference ref = (WeakReference) it.next();
        Cursor cursor = (Cursor) ref.get();
        if (cursor != null) {
            // cursor is null if object has been garbage-collected
            cursor.invalidate();
            ref.clear();
        }
        it.remove();
    }
}
 
Example 17
Source File: MusicLibraryPagerAdapter.java    From Phonograph with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
    super.destroyItem(container, position, object);
    final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position);
    if (mWeakFragment != null) {
        mWeakFragment.clear();
    }
}
 
Example 18
Source File: MainPagerAdapter.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
  super.destroyItem(container, position, object);
  final WeakReference<Fragment> weakReference = mFragmentMap.get(position);
  if (weakReference != null) {
    weakReference.clear();
  }
}
 
Example 19
Source File: AndroidJobStrategy.java    From cloudinary_android with MIT License 5 votes vote down vote up
private void killThread(String requestId) {
    synchronized (threadsMapLockObject) {
        WeakReference<Thread> ref = threads.remove(requestId);
        if (ref != null) {
            Thread thread = ref.get();
            if (thread != null) {
                thread.interrupt();
            }

            ref.clear();
        }
    }
}
 
Example 20
Source File: InternetAvailabilityChecker.java    From InternetAvailabilityChecker with Apache License 2.0 4 votes vote down vote up
/**
 * remove the weak reference to the listener
 */
public void removeInternetConnectivityChangeListener(InternetConnectivityListener internetConnectivityListener) {
    if (internetConnectivityListener == null) {
        return;
    }

    if (mInternetConnectivityListenersWeakReferences == null) {
        return;
    }

    Iterator<WeakReference<InternetConnectivityListener>> iterator = mInternetConnectivityListenersWeakReferences.iterator();
    while (iterator.hasNext()) {

        //if weak reference is null then remove it from iterator
        WeakReference<InternetConnectivityListener> reference = iterator.next();
        if (reference == null) {
            iterator.remove();
            continue;
        }

        //if listener referenced by this weak reference is garbage collected then remove it from iterator
        InternetConnectivityListener listener = reference.get();
        if (listener == null) {
            reference.clear();
            iterator.remove();
            continue;
        }

        //if listener to be removed is found then remove it
        if (listener == internetConnectivityListener) {
            reference.clear();
            iterator.remove();
            break;
        }
    }

    //if all listeners are removed then unregister NetworkChangeReceiver
    if (mInternetConnectivityListenersWeakReferences.size() == 0) {
        unregisterNetworkChangeReceiver();
    }
}