Strange Python memory usage with Scapy
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction
--
Chapters
00:00 Strange Python Memory Usage With Scapy
02:31 Answer 1 Score 0
02:55 Answer 2 Score 1
03:40 Answer 3 Score 0
04:18 Accepted Answer Score 17
04:41 Thank you
--
Full question
https://stackoverflow.com/questions/3111...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #mysql #sqlalchemy
#avk47
ACCEPTED ANSWER
Score 17
I managed to find the actual cause to the memory consumption. It was scapy itself. Scapy by default is set to store all packets it captures. But you can disable it.
Disable:
sniff(iface=interface, prn=sniffmgmt, store=0)
Enable:
sniff(iface=interface, prn=sniffmgmt, store=1)
Thanks to BitBucket Ticket
ANSWER 2
Score 1
As you can see profiler output suggests you use less memory by the end, so this is not representative of your situation.
Some directions to dig deeper: 1) add_time (why is it increasing memory usage?) 2) db_add (why is it decreasing memory usage? caching? closing/opening db connection? what happens in case of failure?) 3) populate_observed_list (is return value safe for garbage collection? may be there are some packets for which certain exception occurs?)
Also, what happens if you sniff more packets than your code is able to process do to performance?
I would profile these 3 functions and analyze possible exceptions/failures.
ANSWER 3
Score 0
Very hard to say anything without the code, assuming it's not a leak in SQLAlchemy or scapy rather than in your code (seems unlikely).
You seem to have an idea of where the leak might happen, do some memory profiling to see if you were right.
Once your python process eats enough memory, you will probably get a MemoryError exception.
ANSWER 4
Score 0
Thanks for the guidance everyone. I think I managed to resolve the increasing memory consumption.
A: Code logic plays a very big role in memory consumption as I have learnt. If you look at the memory_profiler output in my initial question, I moved lines 131-133 into the IF statement at line 136. This seems to not increase the memory so frequently. I now need to refine that populate_observedlist() a bit more to not waste so much memory.