There are several differences between HashMap
and Hashtable
in Java:
Hashtable
is synchronized, whereas HashMap
is not. This makes HashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtable
does not allow null
keys or values. HashMap
allows one null
key and any number of null
values.
One of HashMap's subclasses is LinkedHashMap
, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap
for a LinkedHashMap
. This wouldn't be as easy if you were using Hashtable
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap
.
Java is always pass-by-value. Unfortunately, when we deal with objects we are really dealing with object-handles called references which are passed-by-value as well. This terminology and semantics easily confuse many beginners.
It goes like this:
public static void main(String[] args) {
Dog aDog = new Dog("Max");
Dog oldDog = aDog;
// we pass the object to foo
foo(aDog);
// aDog variable is still pointing to the "Max" dog when foo(...) returns
aDog.getName().equals("Max"); // true
aDog.getName().equals("Fifi"); // false
aDog == oldDog; // true
}
public static void foo(Dog d) {
d.getName().equals("Max"); // true
// change d inside of foo() to point to a new Dog instance "Fifi"
d = new Dog("Fifi");
d.getName().equals("Fifi"); // true
}
In the example above aDog.getName()
will still return "Max"
. The value aDog
within main
is not changed in the function foo
with the Dog
"Fifi"
as the object reference is passed by value. If it were passed by reference, then the aDog.getName()
in main
would return "Fifi"
after the call to foo
.
Likewise:
public static void main(String[] args) {
Dog aDog = new Dog("Max");
Dog oldDog = aDog;
foo(aDog);
// when foo(...) returns, the name of the dog has been changed to "Fifi"
aDog.getName().equals("Fifi"); // true
// but it is still the same dog:
aDog == oldDog; // true
}
public static void foo(Dog d) {
d.getName().equals("Max"); // true
// this changes the name of d to be "Fifi"
d.setName("Fifi");
}
In the above example, Fifi
is the dog's name after call to foo(aDog)
because the object's name was set inside of foo(...)
. Any operations that foo
performs on d
are such that, for all practical purposes, they are performed on aDog
, but it is not possible to change the value of the variable aDog
itself.
For more information on pass by reference and pass by value, consult the following SO answer: https://stackoverflow.com/a/430958/6005228. This explains more thoroughly the semantics and history behind the two and also explains why Java and many other modern languages appear to do both in certain cases.
Best Answer
Here's a good way to create a true memory leak (objects inaccessible by running code but still stored in memory) in pure Java:
ClassLoader
.new byte[1000000]
), stores a strong reference to it in a static field, and then stores a reference to itself in aThreadLocal
. Allocating the extra memory is optional (leaking the class instance is enough), but it will make the leak work that much faster.ClassLoader
it was loaded from.Due to the way
ThreadLocal
is implemented in Oracle's JDK, this creates a memory leak:Thread
has a private fieldthreadLocals
, which actually stores the thread-local values.ThreadLocal
object, so after thatThreadLocal
object is garbage-collected, its entry is removed from the map.ThreadLocal
object that is its key, that object will neither be garbage-collected nor removed from the map as long as the thread lives.In this example, the chain of strong references looks like this:
Thread
object →threadLocals
map → instance of example class → example class → staticThreadLocal
field →ThreadLocal
object.(The
ClassLoader
doesn't really play a role in creating the leak, it just makes the leak worse because of this additional reference chain: example class →ClassLoader
→ all the classes it has loaded. It was even worse in many JVM implementations, especially prior to Java 7, because classes andClassLoader
s were allocated straight into permgen and were never garbage-collected at all.)A variation on this pattern is why application containers (like Tomcat) can leak memory like a sieve if you frequently redeploy applications which happen to use
ThreadLocal
s that in some way point back to themselves. This can happen for a number of subtle reasons and is often hard to debug and/or fix.Update: Since lots of people keep asking for it, here's some example code that shows this behavior in action.