lambdaj use WeakReference which might cause NPE

21 views
Skip to first unread message

veaven

unread,
Aug 22, 2011, 9:59:18 AM8/22/11
to lambdaj
Hi,

I want to use lambdaj in my project, however, I notice that lambdaj
uses several WeakReference in its code. According to my understanding,
JVM GC will automatically collect these WeakReference objects, so, it
means we could get NullPointException in the runtime if my process is
heavily loaded.

Could you please help me to understand why lambdaj uses these
WeakRefernce? can we use do not use WeakReference here? thanks.


In ArgumentsFactory class
private static final Map<InvocationSequence, Object>
PLACEHOLDER_BY_INVOCATION = new WeakHashMap<InvocationSequence,
Object>();

In ProxyArgument class
this.invocationSequence = new
WeakReference<InvocationSequence>(invocationSequence);
...
// the following code could throw NPE in the Runtime as
invocationSequence could be NULL.

public Object invoke(Object proxy, Method method, Object[] args) {
return createArgument(method.getReturnType(), new
InvocationSequence(invocationSequence.get(), new
Invocation(proxiedClass, method, args)));
}

In Invocation class.
weakArgs[i] = invokedMethod.getParameterTypes()[i].isPrimitive() ?
new StrongParameterReference(args[i]) : new
WeakParameterReference(args[i]);

David

Mario Fusco

unread,
Aug 23, 2011, 6:41:25 AM8/23/11
to lam...@googlegroups.com
Hi,

the WeakReference is necessary to avoid memory leaks, i.e Arguments still present in the Map even if no longer reachable in your code and then totally useless. Indeed the fact that a given Argument is reachable inside the client code is what to prevent the GC to collect it, and when it is no longer reachable there is no reason to keep it in memory anymore.

I hope that explains why I used WeakReference, but if you still have any doubt about it feel free to ask again. Moreover if you experienced any NPE while using lambdaj please report this issue as soon as possible.

Thanks,
Mario

veaven

unread,
Aug 27, 2011, 7:28:41 AM8/27/11
to lambdaj
Thanks Mario.
I will test it in my project, and let you know if I get any
NullPointerException from it.

David
Reply all
Reply to author
Forward
0 new messages