Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm currently using Java in a larger project and was curious which of the technical arguments in JWZ's famous "java sucks" article were still valid ten years later. The article starts like this: I think Java is the best language going today, which is to say, it's the marginally acceptable one among the set of complete bagbiting loser languages that we have to work with out here in the real ...
Do any of you know of a Java Map or similar standard data store that automatically purges entries after a given timeout? Preferably in an open source library that is accessible via maven? I know of ways to implement the functionality myself and have done it several times in the past, so I'm not asking for advice in that respect, but for pointers to a good reference implementation. WeakReferen...
MySql has a handy function: SELECT GET_LOCK("SomeName") This can be used to create simple, but very specific, name based locks for an application, but it requires a database connection. I have many situations like: someMethod() { // do stuff to user A for their data for feature X } It doesn't make sense to simply synchronize this method, because, for example, if this method is called...
After reading the books, surfing the nets regarding the type of references in Java, I still have some doubts (or I may have interpreted the concept wrong). It would be a great help for me if anyone clear my doubts. Let me take an example of a class containing class variables, instance variables and local variables. public class Test { public static ArrayList<String> listCommon = n...
How do you optimize the heap size usage of an application that has a lot (millions) of long-lived objects? (big cache, loading lots of records from a db) Use the right data type Avoid java.lang.String to represent other data types Avoid duplicated objects Use enums if the values are known in advance Use object pools String.intern() (good idea?) Load/keep only the objects you need I am l...
Is there a way to see all the references to an object in execution time? I'm using Netbeans, this feature exist in it? EDIT: No problem in using profilers to do this, I only need know the references, no matters how.
Suppose I have a weak reference to a car which has an ordinary reference to an engine. No other references to the car or the engine exist. Can the engine be garbage collected?
In what situations in java is explicit nulling useful. Does it in any way assist the garbage collector by making objects unreachable or something? Is it considered to be a good practice?
I'd like to know if there is a way to check how many references a Java object has. As far as I could check the only way to do that is using JVMTI through a JNI interface. Is there a pure java (without using native libraries) solution to get this information? We are developing an educational tool for data structure animation(to be used with students implementation of certain algorithms), so it ...
I have created an object in Java, Named FOO. FOO contains a large amount of data.. I don't know say for a ten mega byte text file that I have pulled into ram for manipulation.(This is just an example) This is clearly a huge amount of space and I want to deallocate it from memory. I set FOO to NULL. Will this free up that space in memory automatically? or Will the memory taken by the loaded...
If I have two objects on the heap referring to each other but they are not linking to any reference variable then are those objects eligible for garbage collection?
My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern or example for correctly using ThreadLocal with WeakReference, if one exists. For instance, in this code snippet where would the WeakReference be int...
I just came across this answer in SO where it is mentioned that the Google-collections MapMaker is awesome.I went through the documentation but couldn't really figure out where i can use it.Can any one point out some scenario's where it would be appropriate to use a MapMaker.
We know that for any other objects GC will take care of deallocation. but what happens to a String objects which resides in String pool. who will decide it to deallocate and who actually does it? because we know that there will be String literals still present even after dereferencing it.
I was just digging around in the commons-io library and found this: Keeps track of files awaiting deletion, and deletes them when an associated marker object is reclaimed by the garbage collector. This can be found in the documentation for the FileCleaningTracker object. Now I am just curious how I can do this by myself? How can my code detect when an object is reclaimed by the garbage col...
Here's the deal: I have an Android application that needs to call a web service every X seconds (currently 60 seconds). This application has multiple tabs and these tabs all need to interact with the data themselves. One is a MapView, one is a ListView and then the third is irrelevant but will need to also get some global data eventually. The issue is that I want my main activity to have a thre...
How come JFrame instances are always reachable within a Swing application? Won't that prevent it from being garbage collected? JFrame frame = new JFrame(); System.out.println(Window.getWindows().length); // says '1' Calling dispose() doesn't help. It is not even shown.
I've a JME application running in a samsung i617 cell phone, and the applications is throwing OutOfMemoryError when its heap goes over 7.1 MB (and the cell phone has 64 mb)... Is it possible to use the -Xmx and -Xms parameters in JME
This blog post demonstrates a way to implement a mutex per string id idiom. The String ids used are to represent HttpSession ids. Why do we need to wrap a WeakReference around the Mutex instances ? Isn't it better to just create a Map from String -> Mutex ? Why do we need to call put twice ? public Mutex getMutex( String id ) { Mutex key = new MutexImpl(...
In the constructor of my class, I map the current object (this), along with its key (a string entered as a parameter in the constructor) into a static LinkedHashMap so I can reference the object by the string anywhere I might need it later. Here's the code (if it helps): public class DataEntry { /** Internal global list of DataEntry objects. */ private static LinkedHashMap _INTERNAL_L...
How do I know how much memory is available for my app on the fly? My app downloads some bitmaps and saves (in memory) them so not to bug the user by downloading it again. But the Java's heap is only 16MB, as far as I know, so I will need to handle some low memory cases and throw away some downloaded bitmaps (not all the bitmaps are shown at the same time, but they might be visible to the user w...
 /*
  * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang.ref;


Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings.

Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references. At the same time it will declare all of the formerly weakly-reachable objects to be finalizable. At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.

Author(s):
Mark Reinhold
Since:
1.2
public class WeakReference<T> extends Reference<T> {

    
Creates a new weak reference that refers to the given object. The new reference is not registered with any queue.

Parameters:
referent object the new weak reference will refer to
    public WeakReference(T referent) {
        super(referent);
    }

    
Creates a new weak reference that refers to the given object and is registered with the given queue.

Parameters:
referent object the new weak reference will refer to
q the queue with which the reference is to be registered, or null if registration is not required
    public WeakReference(T referentReferenceQueue<? super T> q) {
        super(referentq);
    }
New to GrepCode? Check out our FAQ X