Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests memory leak: Caused computer to crash #4644

Closed
Kodingu opened this issue May 12, 2018 · 3 comments
Closed

Requests memory leak: Caused computer to crash #4644

Kodingu opened this issue May 12, 2018 · 3 comments

Comments

@Kodingu
Copy link

Kodingu commented May 12, 2018

So as I was working on coding a notifier for a website, I came across an issue where my computer would gradually slow down after a long period of time. I thought it was nothing until my computer crashed. I then decided to look up on this issue by testing every single line of code in my while loop until i finally found out that it was caused by the requests module.

I decided to look up on google if anyone else has gotten an issue like this and I actually did manage to find a page with someone who had this exact same problem.

My code that I used to create this memory leak was this:

import requests

ListedItems = {}

while True:
	r = requests.get("urlToGet").json()
	time.sleep(1)
	for itm in r:
		print(itm)
	del r

I even tried to delete the r variable after I got everything I needed, but it still resulted in my computer to crash after a few hours. Please note that I need to have this program running for days.

@nateprewitt
Copy link
Member

Hi @Kodingu, we have another issue open (#4601) for something similar, but haven't been given enough information to identify a problem.

Could you please provide the information originally requested in the template used to create this ticket? It also looks like you may have been storing items in ListedItems at some point since you've included it in your repro. To verify, this is the exact code you've used to repro and you aren't storing anything from these responses?

Would you also mind testing this with the minimum required amount of code to reproduce?

e.g. Does this repro the problem for you?

import requests
import time

while True:
    r = requests.get("urlToGet")
    time.sleep(1)
    print(r.content)
    del r

@initbar
Copy link

initbar commented May 20, 2018

I've used @nateprewitt's sample code and the memory usage seems stable to me:

figure_1

I'm also curious whether ListItems dict was modified to keep historic or memo-istic records of the json elements.

If you do want to use a dictionary, nonetheless, and would like to have an upper bound for the contained elements, I would recommend using the following dict implementation:

from collections import OrderedDict

class limited_dict(OrderedDict):

    def __init__(self, *a, **kw):
        self.limit = kw.pop("maxsize", None)
        OrderedDict.__init__(self, *a, **kw)

    def __setitem__(self, key, value):
        OrderedDict.__setitem__(self, key, value)
        self.__check()

    def __check(self):
        if self.limit is not None:
            while len(self) > self.limit:
                self.popitem(last=False)

# test
# a = limited_dict(maxsize=10)
# for i in range(20):
#     a[str(i)] = i
#
# print a
# limited_dict([('10', 10),
#               ('11', 11),
#               ('12', 12),
#               ('13', 13),
#               ('14', 14),
#               ('15', 15),
#               ('16', 16),
#               ('17', 17),
#               ('18', 18),
#               ('19', 19)])

@nateprewitt
Copy link
Member

Thanks so much for taking the time to look into this @initbar! It does look like there may have been more than just Requests code involved in the initial POC with the potential for lingering pointers.

I think we may need a longer look at memory usage to really rule out a leak, but that should probably be tracked in the main issue. I'm closing this out since @Kodingu hasn't provided any more information, and we'll move forward in #4601.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants