question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suspected memory leak

See original GitHub issue

Hi, thank you for sharing your python wrapper for darknet. This is very useful to us.

We are currently doing a bench-marking experiment using your library, which includes setting different input image sizes in the cfg file and finding the performance in terms of speed.

However, we seem to be observing that as we repeatedly load a new network (with a new cfg) in a for loop, the GPU memory usage steadily increase each round, even as we went from a larger input image size to a smaller one.

Here’s our test script for reference:

from io import BytesIO
import numpy as np
import requests
import cv2
import time

from pydarknet import Detector, Image
from check_input_size import check_input_size_range
from set_input_size import set_input_size

im_path = 'frame0001.jpg'
img = cv2.imread(im_path)
img = Image(img)
cfg = 'yolo/cfg/yolov3.cfg'
data_file = 'yolo/cfg/coco.data'
wts = 'yolo/weights/yolov3.weights'

min_size = 40
max_size = 1000
sizes, _ = check_input_size_range(cfg, min_size, max_size)
print('Will run for {} sizes'.format(len(sizes)))
sizes = list(reversed(sizes))

repeat = 100
for size in sizes:
    set_input_size(cfg=cfg, new_cfg=cfg, size=size)
    net = Detector(bytes("yolo/cfg/yolov3.cfg", encoding="utf-8"), 
                   bytes("yolo/weights/yolov3.weights", encoding="utf-8"), 
                   1,
                   bytes("yolo/cfg/coco.data", encoding="utf-8"))
    duration = 0
    img = cv2.imread(im_path)
    img = Image(img)
    for i in range(repeat):
        print('Repeating {: >4}th time for size {: <4}'.format(i+1, size), end='\r')
        tic = time.time()
        results = net.detect(img)
        toc = time.time()
        duration = ( (toc-tic) + i * duration )/(i+1)
    with open('temp.log','a') as f:
        f.write('{}:{}\n'.format(size, duration))
    net = None
    img = None
    results = None

set_input_size is a simple function that goes into the cfg file to change the size of the input image specified there for varying sizes that are valid.

As we loop through the for loop, we observe the GPU memory usage slowly rising by around 20+MB each round. We added print statement in the .pyx file to check that the network and image variables are getting freed and they are correctly freed at the end of each round. Therefore we are not sure if its something else whose memory is not getting freed.

This is not a big issue now, but just concerned if the memory leak might be a problem in the future. Any ideas? Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
madhawavcommented, Dec 16, 2018

Hi, Thanks a lot @DinoHub I will look into this.

Can you try manually deleting the network after each iteration? Modify the last bit of your code as follows.

    net = None
    img = None
    results = None
    del net

This would enforce the destructor of Detector to be called immediately, which should notify darknet to release the model. Otherwise, the destructor may not be called until Python Garbage Collector kicks in.

0reactions
madhawavcommented, Jun 27, 2020

Hi, Thank you for your reply. Closing the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Memory Leaks: Solutions, Tools, Tutorials & More - Stackify
We put together this guide to help you understand how, why, and where Java memory leaks happen – and what you can do...
Read more >
How to Fix A Windows Memory Leak - Lifewire
The steps below show how to view your system's memory usage in Windows 10, 8, and 7. Press Windows key+R, enter "resmon," then...
Read more >
Windows 10 suspected memory leak - Microsoft Community
Windows memory diagnostic tool tests physical condition of memory. Memory leaks are caused by defects in programs or drivers so one need to...
Read more >
Java Memory Leak Detection: Causes & Tools to Avoid Them
The memory leak is a situation where an object or objects are no longer used, but at the same time, they can't be...
Read more >
Creating a Memory Leak Diagnosis report - IBM
In the Memory Leak Diagnosis report each row in the Suspected Memory Leaks table represents an allocation pattern for which memory allocation data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found