rishadbaniya / hyperblow

A torrent client made to give a hard left blow to your daily used torrent clients
Apache License 2.0
27 stars 2 forks source link

Fix memory leak #6

Closed rishadbaniya closed 2 years ago

rishadbaniya commented 2 years ago

image A huge heap consumption was seen, around 3.8 gigs at the end of the program, need to figure out what caused this leak

rishadbaniya commented 2 years ago

A debug build showed much more details : image image

rishadbaniya commented 2 years ago

All i found out was the loop where i used getMessage is causing a lot of allocations, a lot in a loop

rishadbaniya commented 2 years ago

image

Found out that in getMessage(), the KEEP_ALIVE message was getting constantly allocated on the heap as messages.push(msg)

rishadbaniya commented 2 years ago

Not clearing the buffer before returning Message::KEEP_ALIVE as causing Message::KEEP_ALIVE to be pushed continously,

    if bytes.len() == 0 {
        // If the buffer is empty then it means there is no message
        None
    } else if bytes.len() == 4 {
        // TODO : Check if the length is (0_u32) as well
        Some(Ok(Message::KEEP_ALIVE))
    } else {
        let pstr_len = bytes[0];

        if pstr_len == 19u8 {

clearing the buffer before returning Message::KEEP_ALIVE can fix this issue

rishadbaniya commented 2 years ago

f29ef8a8a60f19c3785a0abb041d6526ec75dcdb fixes the memory allocation