Machineboy空

Memory Leak(메모리 누수) 본문

Computer/개념

Memory Leak(메모리 누수)

안녕도라 2023. 11. 3. 14:51

* 정의

- an unintentional form of memory consumption whereby the developer fails to free an allocated block of memory when no longer needed.

 

- 프로그램이 작동하며 할당됐던 메모리가 더 이상 사용되지 않는 시점에서도 반환되지 않는 현상입니다. 정상적으로 반환되지 않은 메모리가 계속 누적되면 프로그램에 할당할 수 있는 메모리가 부족해지면서 프로그램이 비정상적으로 작동하거나 크래시가 발생할 수 있습니다.

 

* Memory Leak 방지 -  http 통신 시 활용되는 using문의 쓰임새 

    IEnumerator CoSendRequest(HttpInfo httpInfo)
    {
        if (httpInfo.requestType == RequestType.POST)
        {
            //  Memory leak 방지
            //  매개변수: 리소스 변수 선언
            using (UnityWebRequest req = UnityWebRequest.Post(httpInfo.url, httpInfo.body))
            {
                //body를 UTF8로 인코딩해준다.
                byte[] byteBody = Encoding.UTF8.GetBytes(httpInfo.body);
                //upload를 한다.
                req.uploadHandler = new UploadHandlerRaw(byteBody);
                //header설정을 한다.
                req.SetRequestHeader("Content-Type", "application/json");

                //요청 성공할 때 까지 잠깐 양보
                yield return req.SendWebRequest();

                //성공했다면 내용 print, 실패했다면 error 띄우는 함수 만들었다.
                RequestResult(req, httpInfo);

                //실행 후 자동으로 dispose 처리 해준다.
            }
        }
     }

 

- object will be automatically disposed of, helping to prevent memory leaks and improve the program's performance.