How to avoid memory leaking?

Memory leaking

How can I avoid an increasing of memory RAM usage for my app? Some suggestions?

@st.cache_resource(ttl=360)
def get_db_connection():
    database = "transactions.db"
    try:
        return create_engine(f"sqlite:///../../user_data/{database}")
    except Exception as error:
        st.error((f"Error while connecting to {database}: ", error))
        print(f"Error while connecting to {database}: ", error)
        return None


# creating a single-element container.
placeholder = st.empty()
# near real-time / live feed simulation
while True:
    # while True:
    # for seconds in range(200):
    try:
         transactions_df = pd.read_sql_query("select * from transactions order by sell_time desc", get_db_connection())
        transactions_df["time_held"] = pd.to_timedelta(transactions_df["time_held"]).dt.floor(freq="s").astype("string")
        transactions_df["buy_time"] = pd.to_datetime(transactions_df["buy_time"]).dt.strftime("%Y-%m-%d %H:%M:%S")
        transactions_df["sell_time"] = pd.to_datetime(transactions_df["sell_time"]).dt.strftime("%Y-%m-%d %H:%M:%S")

        open_columns = [
            "id",
            "buy_time",
            "symbol",
            "volume",
            "bought_at",
            "now_at",
            "change_perc",
            "profit_dollars",
            "time_held",
            "tp_perc",
            "sl_perc",
            "buy_signal",
        ]
        open_trades = transactions_df.loc[transactions_df["closed"] == 0, open_columns]
        open_trades["id"] = list(range(1, len(open_trades) + 1))
        open_trades.rename(
            columns={
                "id": "Id",
                "buy_time": "Buy Time",
                "symbol": "Symbol",
                "volume": "Volume",
                "bought_at": "Bought at",
                "now_at": "Now at",
                "change_perc": "Change %",
                "profit_dollars": "Profit $",
                "time_held": "Time held",
                "tp_perc": "TP %",
                "sl_perc": "SL %",
                "buy_signal": "Buy Signal",
            },
            inplace=True,
        )

        closed_trades_columns = [
            "id",
            "buy_time",
            "symbol",
            # "volume",
            "bought_at",
            "sold_at",
            "change_perc",
            "profit_dollars",
            "sell_time",
            "time_held",
            "tp_perc",
            "sl_perc",
            "buy_signal",
            "sell_reason",
        ]
        closed_trades = transactions_df.loc[transactions_df["closed"] == 1, closed_trades_columns]
        closed_trades["id"] = list(range(1, len(closed_trades) + 1))
        closed_trades.rename(
            columns={
                "id": "Id",
                "buy_time": "Buy Time",
                "symbol": "Symbol",
                # "volume": "Volume",
                "bought_at": "Bought at",
                "sold_at": "Sold at",
                "change_perc": "Change %",
                "profit_dollars": "Profit $",
                "sell_time": "Sell time",
                "time_held": "Time held",
                "tp_perc": "TP %",
                "sl_perc": "SL %",
                "buy_signal": "Buy Signal",
                "sell_reason": "Sell Reason",
            },
            inplace=True,
        )

    except Exception as e:
        print(e)
        pass

    with placeholder.container():
    

        st.markdown(
            f"### **_Open Trades_** (Winning: <span style='color:green;'>{open_trades[open_trades['Change %'] > 0].shape[0]}</span> | Losing: <span style='color:red;'>{open_trades[open_trades['Change %'] <= 0].shape[0]}</span>)",
            unsafe_allow_html=True,
        )
        st.dataframe(
            open_trades.style.hide(axis="index")
            .set_properties(subset=["Id"], **{"width": "100"})
            .format("{:.2f}%", subset=["Change %", "TP %", "SL %"])
            .apply(gray_background, axis=0)
            .applymap(color_negative_values, subset=["Change %", "Profit $"]),
            use_container_width=True,
            height=400,
        )
        "### **_Closed Trades_**"
        st.dataframe(
            closed_trades.style.hide(axis="index")
            .set_properties(subset=["Id"], **{"width": "100px"})
            .format("{:.2f}%", subset=["Change %", "TP %", "SL %"])
            .apply(gray_background, axis=0)
            .applymap(color_negative_values, subset=["Change %", "Profit $"]),
            use_container_width=True,
            height=400,
        )
        time.sleep(60)

Debug info

  • Streamlit version: (1.19.0)
  • Python version: (3.10.9)
  • OS version:Ubuntu 22.04
  • Browser version:

Requirements file

Package Version


aiohttp 3.8.4
aiosignal 1.3.1
altair 4.2.2
async-timeout 4.0.2
attrs 22.2.0
blinker 1.5
cachetools 5.3.0
certifi 2022.12.7
charset-normalizer 3.0.1
click 8.1.3
colorama 0.4.6
dateparser 1.1.7
decorator 5.1.1
entrypoints 0.4
frozenlist 1.3.3
gitdb 4.0.10
GitPython 3.1.31
greenlet 2.0.2
idna 3.4
importlib-metadata 6.0.0
Jinja2 3.1.2
joblib 1.2.0
jsonschema 4.17.3
loguru 0.6.0
markdown-it-py 2.2.0
MarkupSafe 2.1.2
mdurl 0.1.2
multidict 6.0.4
numpy 1.24.2
packaging 23.0
pandas 1.5.3
pandas-ta 0.3.14b0
Pillow 9.4.0
pip 23.0.1
prettytable 3.6.0
protobuf 3.20.3
PTable 0.9.2
pyarrow 11.0.0
pycryptodome 3.17
pydeck 0.8.0
Pygments 2.14.0
Pympler 1.0.1
pyrsistent 0.19.3
python-binance 1.0.17
python-dateutil 2.8.2
python-decouple 3.8
pytz 2022.7.1
pytz-deprecation-shim 0.1.0.post0
PyYAML 6.0
regex 2022.10.31
requests 2.28.2
rich 13.3.1
scikit-learn 1.2.1
scipy 1.10.1
semver 2.13.0
setuptools 65.5.1
six 1.16.0
smmap 5.0.0
SQLAlchemy 1.4.46
streamlit 1.19.0
streamlit-aggrid 0.3.3
threadpoolctl 3.1.0
toml 0.10.2
toolz 0.12.0
tornado 6.2
tradingview-ta 3.3.0
typing_extensions 4.5.0
tzdata 2022.7
tzlocal 4.2
ujson 5.7.0
urllib3 1.26.14
validators 0.20.0
watchdog 2.3.1
wcwidth 0.2.6
websockets 10.4
wheel 0.38.4
yarl 1.8.2
zipp 3.15.0

Have a look on my comment in the image.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.