-
Notifications
You must be signed in to change notification settings - Fork 57
Memory Leak After Disconnect (when acting as a BLE central) #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
After a bit of looking at the source for the adafruit_ble library, I think I may see the cause of the issue that I described above. Cause: After each connect/disconnect, the _connection_cache dictionary grows by an entry. Since these entries are never cleared, a reference is retained for all of these connections which prevents the objects and all of the memory they hold references to from being garbage collected. In this example, all of these connections are closed and could/should be discarded (arguably). This can be observed by using the code from my first post above by adding the following line after the 'print("gc.mem_free:", gc.mem_free())' line. print(ble._connection_cache) Sample Run Output
As can be seen above, after just 4 connect/disconnects to the same peripheral, the '_connection_cache' dictionary holds 4 entries. Possible improvements:
|
Prevent unbounded BLERadio._connection_cache growth from causing memory exhaustion (issue #146)
Closed via #147. |
Issue: When acting as a ble central, there appears to be a persistent memory leak after disconnecting from a device.
Conditions: Circuitpython 7.0.0, mpy=517 library bundle: 7.x-mpy-20211125
Hardware: Feather nrf52840
Description: My application (acting as a central) connects to another adafruit board (Itsy nrf52840) acting as a peripheral, reads some data, and then disconnects. It repeats this process with one or more peripheral boards. I noticed that memory trends monotonically downward, even when explicitly requesting garbage collection, until the application crashes/freezes.
Duplication: I created the following code snippet that demonstrates the issue. Copy the code into code.py on a circuitpython system (see conditions I tested under at top). Replace the address in 'ble_target_address' with the address of a known, connectable device that you can use as the peripheral to connect to. Running the program will repeatedly connect, read services, and disconnect. Each time, it manually requests garbage collection and then prints the free memory. See a snippet of the output of a sample run below the code listing.
Sample Run Output:
This sample run ended with the device freezing and requiring a hardware reset. The elapsed time from the beginning of the run to the end was approximately 7 minutes and 45 seconds.
The text was updated successfully, but these errors were encountered: