One possibility is to create a wrapper for an ArrayList that only provides one method: one that adds things to the ArrayList. Make the ArrayList itself private. Now, construct one of these wrapper objects in global scope (as a static object in a class) and qualify it with the final keyword (e.g. public static final ArrayListWrapper wrapperClass = new ArrayListWrapper()
). So now the reference cannot be altered. That is, wrapperClass = null
won't work and can't be used to free the memory. But there's also no way to do anything with wrapperClass
other than add objects to it. Therefore, any objects you do add to wrapperClass
are impossible to recycle.