I wrote a code to show some points on ipyleaflet map that will be updated automatically. at first, the map shows fine, but despite removing markers after a certain time (eg. 72 hours) the notebook crashes after like 7 days with this message:

enter image description here

I checked the Chrome RAM usage of the tab; it showed about 130 MB at first. but when the notebook crashes it seems like the map uses about 3.5 GB of RAM.

here is the code snippet to update the map continuosly :

# Modify the update_map function to set z_index_offset based on max_intensity
from threading import Timer

# Function to continuously update the map
def update_map_continuously():
    while True:
            data = update_queue.get(timeout=2)
            update_map(data, waittimetorefresh)
        except queue.Empty:

def update_map(data, waittimetorefresh):
    global event_counter, last_clear_time, my_map , irsc_event_counter
        # Define function to make marker invisible after 30 seconds
    def make_invisible(marker):
        # marker.visible = False

    popup_content = f"""
    <div style="width: 250px;">
        <b>Name:</b> {data['all_channel_data'][0]['stationname']}<br>
        <b>ID: {data['identifier'].split('-')[0]}<br>
        <b>Longitude:</b> {data['all_channel_data'][0]['longitude']}<br>
        <b>Latitude:</b> {data['all_channel_data'][0]['latitude']}<br>
        <b>PGA:</b> {text_pga} [{text_pga_channel}], <b>PGV:</b> {text_pgv} [{text_pgv_channel}], <b>PGD:</b> {text_pgd} [{text_pgd_channel}]<br>
        <b>Intensity:</b> {text_intensity} [{text_pgv_channel}]<br>
    # Generate a unique variable name for each marker
    marker_name = "marker" + str(event_counter)
    marker = Marker(
        location=(float(data['all_channel_data'][0]['latitude']), float(data['all_channel_data'][0]['longitude'])),
        draggable=False, title=data['identifier'])

    # Calculate z_index_offset based on max_intensity
    z_index_offset = int(max_intensity * 100)  
    marker.z_index_offset = z_index_offset

    triangle_html = create_triangle_icon(max_intensity, scaling_factor, font_size, font_family, marker_name, border_line_color, line_thickness, farsi_font_url, reject_index)
    triangle_icon = DivIcon(html=triangle_html, bg_pos=[0, 0], icon_size=[0, 0])

    marker.icon = triangle_icon

    popup = Popup(
    marker.popup = popup


    # Schedule the visibility toggle after 30 seconds it is in seconds 72*60*60 it is 72 hours
    timer = Timer(72*60*60, make_invisible, args=[marker])
    # Increment the event counter for generating unique variable names
    event_counter += 1

and I use threads to read data from a WebSocket.

can someone help me on how to manage the RAM usage so I can have the map 24x7?

  • When you remove marker from map with my_map.remove(marker), object marker is not deleted, it stays in memory. First thing you can try is to delete marker object after you remove it from the map. If there are no more references to it, memory will be freed.
    – TomazicM
    Commented May 15 at 13:49
  • how can I delete the marker? I searched the website for ipyleaflet and it does not have "delete".
    – Shane roar
    Commented May 16 at 21:44
  • It's Python operation: del marker.
    – TomazicM
    Commented May 17 at 5:48
  • thank you for your help. I tried it. didn't work it still adds up and eats my RAM.
    – Shane roar
    Commented May 22 at 20:49
  • Then one possible solution would be to have an array of markers. When certain marker in array is not used any more, it's hidden and when new marker is needed, one of hidden markers is moved to new location, content updated and marker then displayed.
    – TomazicM
    Commented May 23 at 6:23


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.