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

The following examples show how to use java.lang.ref.WeakReference#clear() . These examples are extracted from open source projects. 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 Project: gwtmockito   File: ThreadLocalCleaner.java    License: 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 2
Source Project: BlueMap   File: FolderFileAccess.java    License: 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 3
/**
 * 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 4
@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 5
Source Project: cloudinary_android   File: AndroidJobStrategy.java    License: 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 6
Source Project: cloudinary_android   File: AndroidJobStrategy.java    License: 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 7
@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 8
@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
/**
 * 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 10
@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 11
Source Project: WayHoo   File: WeatherPagerAdapter.java    License: 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 12
Source Project: zap-extensions   File: ConsolePanel.java    License: 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 13
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 14
@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 15
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 16
@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 17
@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
private void cleanupCache() {
    WeakReference<UploadInfo> ref = uploadInfoCache.get();
    if (ref != null) {
        uploadInfoCache.remove();
        ref.clear();
    }
}
 
Example 19
@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 20
/**
 * 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();
    }
}