본문 바로가기
지속가능티끌/Python

Python. 문자열 + 연산자 메모리 누수 생김 . 주의 필요.

by i.got.it 2021. 11. 4.

 

개요

 

파이썬은 메모리 해제가 자동으로 이뤄진다길래 아무거나 사용해도 되는 줄 알았으나, 메모리 누수 생기는 경우 경험함.  

 

 

오류 상황예.

 

- 아래 함수는 거래소에서 웹소켓기반 실시간 수신되는 체결 틱 데이터를 DB 에 저장하는 함수인데 첫부분에 SQL 쿼리문 만들기 위하여  문자열 + 연산자로 처리했었던것. 

 

-  암호화폐 거래소의 30여개 종목의 실시간 체결 틱 데이터 수신 될 때 마다 아래 함수 호출하는데  2일 정도 지나면   반드시 PC 메모리 다 잡아먹고 먹통 만들었음.  

 


def insert_new_fill(_symbol, _trade_price, _trade_volume, _ask_bid,_trade_date, _trade_time, _i_timestamp):

    str_sql_insert = 'INSERT INTO T_Market_Fill (symbol, trade_price, trade_volume, ask_bid,trade_date, trade_time, timestamp) VALUES ("'
    str_sql_insert += _symbol + '","'
    str_sql_insert += _trade_price + '","'
    str_sql_insert += _trade_volume + '","'
    str_sql_insert += _ask_bid + '","'
    str_sql_insert += _trade_date + '","'
    str_sql_insert += _trade_time + '",'
    str_sql_insert += str(_i_timestamp) 
    str_sql_insert += ')'

    #print(str_sql_insert)

    CON_DB_CYFIN_MARKET_FILL.execute(str_sql_insert) 

    CON_DB_CYFIN_MARKET_FILL.commit()

 

 

 

해결책 

 

위 코드에서 문자열 + 연산자 사용하지 않고, 아래처럼 SQL 문자열 구성  구현하면 메모리 누수 안생김. 

 

 


def insert_new_fill(_symbol, _trade_price, _trade_volume, _ask_bid,_trade_date, _trade_time, _i_timestamp):

    str_sql_insert = 'INSERT INTO T_Market_Fill (symbol, trade_price, trade_volume, ask_bid,trade_date, trade_time, timestamp) \
    VALUES ("%s","%s","%s","%s","%s","%s","%s")' \
    % (_symbol, _trade_price,_trade_volume, _ask_bid,_trade_date ,_trade_time, str(_i_timestamp))

    #print(str_sql_insert)

    CON_DB_CYFIN_MARKET_FILL.execute(str_sql_insert) 

    CON_DB_CYFIN_MARKET_FILL.commit()

 

 

 

 


첫 등록 : 2021.11.03

최종 수정 : 

단축 주소 : https://igotit.tistory.com/3043

 


 

'지속가능티끌 > Python' 카테고리의 다른 글

Python . Visual Studio 파이썬 한글 깨짐 해결책  (0) 2022.01.27
Visual Studio 2022. 파이썬 개발환경 설치  (0) 2021.12.04
Python. numpy  (0) 2021.10.10
Python. List 리스트  (0) 2021.10.10
Python. 클래스  (0) 2021.10.06

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5