How are Messengers garbage collected?
<p>Consider this code:</p>

<pre><code>Message message = Message.obtain(null, MSG_REQUEST_QUOTE, symbol);
message.replyTo = new Messenger(new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case StockMessengerService.MSG_QUOTE_VALUE:
                callback.onQuote(symbol, msg.arg1);
                break;
            case StockMessengerService.MSG_QUOTE_FAILURE:
                callback.onFailure(symbol, msg.obj.toString());
                break;
            default:
                super.handleMessage(msg);
        }
    }
});

mService.send(message);
</code></pre>

<p>How is the JVM able to garbage collect the replyTo Messenger? In the Android javadocs, they only have a single Messenger, bound to the Activity. But this code works, but I'm quite curious how it is the JVM doesn't GC the Messenger before it gets its message, OR, whether this code is quite risky and will leak memory?</p>

<p>Edit2:
mService is like:</p>

<pre><code>private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mService = new Messenger(service);
</code></pre>

<p>This is based on the sample code in the Service javadoc at <a href="http://developer.android.com/reference/android/app/Service.html" rel="nofollow">http://developer.android.com/reference/android/app/Service.html</a></p>

<p>Thanks,
Eric</p>
