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

Memory leak #34

Closed
mloo-sp opened this issue Jan 19, 2017 · 6 comments
Closed

Memory leak #34

mloo-sp opened this issue Jan 19, 2017 · 6 comments

Comments

@mloo-sp
Copy link

mloo-sp commented Jan 19, 2017

We have been noticing a memory leak with watchtower for long running processes.
Logs are being delivered to Cloudwatch but memory usage keeps going up.
Python version 2.7.10 and watchtower 0.3.3

Snippet of code to reproduce

import watchtower, logging, time

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(str(time.time()))
wt = watchtower.CloudWatchLogHandler(use_queues=True, send_interval=60, max_batch_count=10)
logger.addHandler(wt)
logger.propagate = False

for _ in xrange(50000):
    logger.info('Junk data | {}'.format("0" * 10000))
    time.sleep(0.05)
@kageurufu
Copy link

I'm seeing this as well, and had to remove the watchtower handler to keep our servers alive

@jcerjak
Copy link

jcerjak commented Feb 8, 2018

I checked the snippet above with Python 2.7.6 and latest watchtower master by using the memory_profiler.

When using queues (use_queues=True):

watchtower_memory_leak_with_queues

Without using queues (use_queues=False):

watchtower_memory_leak_no_queues

Upon investigation, it seems that the issue is related to botocore, and might be further amplified with the way threads are used in watchtower: boto/botocore#805

This seems to be confirmed by doing manual garbage collection after pushing logs to CloudWatch:

import gc

...
response = self.cwl_client.put_log_events(**kwargs)
gc.collect()

When using queues (use_queues=True) and gc:

watchtower_memory_leak_with_queues_and_gc

@abend-arg
Copy link

abend-arg commented Aug 3, 2021

According to this: boto/botocore#1246 (comment)

To avoid memory leaks using botocore/boto3, the idea is to create one session per thread. Currently this package creates a single session that is being used by every thread. This is causing this issue apparently. It is the recommendation from AWS as well.

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html#multithreading-and-multiprocessing

@StevenMapes
Copy link

Is this still an issue? I was about to use this package with for some clients but if it has memory leaks still like those shown above as their services tend to run a long time without restarting and that sort of memory burn rate will drain resources too quickly

@abend-arg
Copy link

abend-arg commented Feb 11, 2022

I have not experienced problems related to this, but my service is small in comparison to others. According to this opened issue the problem with botocore still persists: boto/boto3#1670

@kislyuk
Copy link
Owner

kislyuk commented Feb 11, 2022

We use watchtower in production and do not observe any memory leaks. Also, watchtower does not share boto3 clients across threads unless explicitly passed a configuration that forces it to do so.

I am going to close this issue for now. When I run the reproduction script above, it stabilizes at a steady state memory consumption of 60MB. Network outages and associated retries may temporarily increase this footprint. If somebody has a specific concern, please post a complete reproduction and an explanation of why it is different from what you expect.

@kislyuk kislyuk closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants