SNMP4j + async -> memory leak?

I’m using snmp4j and every 10 days experience a out of memory error (4GB). Heap dump shows that 3,65GiB is org.snmp4j.Snmp$AsyncRequestKey (529164 objects). I’ve checked the source code and in org.snmp4j.Snmp for asyncRequests there’s always put and never remove except cancel() and close() but these don’t look like regular use unless I’m doing something wrong (in my app Snmp is long living object i.e. 10 days).

For asynchronous requests you need to call cancel(..) manually as first call in your response handler to avoid a memory leak.
This design allows more than one response per request (broadcast/multicast).
I have to admit, that this use case is very rare in modern networking.
I will probably add a “auto cancel” option for future releases if I have not done it yet (have to check it first).

ok, but since listener is part of the key (AsyncRequestKey in asyncRequests) , as far as I understand this should look something like this:

ResponseListener listener = new ResponseListener() {
  public < A extends Address > void onResponse( ResponseEvent< A > respEvt ) {
    snmp.cancel( pdu, listener );
    ...
  }
}
snmp.send( pdu, target, userHandle, listener );

unfortunately java does not allow using listener inside listener definition but this can be worked around with e.g. AtomicReference< ResponseListener >.

Defining listener as final should work too.