Johnuready
Posts: 8
Joined: Sat Jan 14, 2017 9:52 pm

Python program freezes

Sat Jan 14, 2017 10:13 pm

Hi I'm new to the Pi and python programming but able to solve most problems over the last 6 months.

I have a program that reads voltage via an adc board and switches relays according to the voltage. This runs but after a few hours it stops with a black screen and the Pi freezes.

Trying to find what is happening I wrote a very simple loop and screen print program and again it freezes after a few hours.

Any ideas? Am I missing a trick to clear working space / variables?

User avatar
DougieLawson
Posts: 38467
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python program freezes

Sat Jan 14, 2017 10:44 pm

It's probably got a memory leak.

Post your code on here in [code] your python stuff goes here ...[/code] tags.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Johnuready
Posts: 8
Joined: Sat Jan 14, 2017 9:52 pm

Re: Python program freezes

Sat Jan 14, 2017 11:40 pm

Thanks for the clue on Memory Leak. I have looked through the forum and read chats but confused on the solution. Anybody know the final solution that a novice can implement?

Also, why do Pi users have these known problems and we do not see revisions to resolve?

User avatar
DougieLawson
Posts: 38467
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python program freezes

Sat Jan 14, 2017 11:41 pm

It's not a "known problem" it's just about 100% guaranteed to be a bug in your code.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Johnuready
Posts: 8
Joined: Sat Jan 14, 2017 9:52 pm

Re: Python program freezes

Mon Jan 16, 2017 7:26 pm

It's been a very interesting weekend. I was looking on the net and found numerous ideas that the network interface connection was the reason for memory leak on Pi 3. After the advise it is always a program I found:

Running a large count and print prog in idle it would slowly take memory. Run the same program in terminal no problems.

My problem was an IP display program that runs every minute in crontab. Fixed that program and now I have no memory slipping away. Thanks for your help. John

Heater
Posts: 15364
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python program freezes

Mon Jan 16, 2017 7:46 pm

How does one actually make a memory leak in Python?

I thought part of the point of Python is that you cannot.
Memory in C++ is a leaky abstraction .

Johnuready
Posts: 8
Joined: Sat Jan 14, 2017 9:52 pm

Re: Python program freezes

Mon Jan 16, 2017 7:59 pm

My problem was that crontab was running multiple programs of my IP python program to display the IP address on boot up and every minute. Solved the problem by only running on reboot and slowing down the program until the Pi had settled on the network connection.

asavah
Posts: 371
Joined: Thu Aug 14, 2014 12:49 am

Re: Python program freezes

Mon Jan 16, 2017 8:18 pm

Heater wrote:How does one actually make a memory leak in Python?

I thought part of the point of Python is that you cannot.
http://www.lshift.net/blog/2008/11/14/t ... ory-leaks/

User avatar
Paeryn
Posts: 2900
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Python program freezes

Mon Jan 16, 2017 8:32 pm

Heater wrote:How does one actually make a memory leak in Python?

I thought part of the point of Python is that you cannot.
Whilst Python uses reference counting for most of it's memory management there are times (cyclic references) where it doesn't work. For these cases there is a garbage collector which tries to free up objects but it used to be limited in cases where objects have their own __del__() functions where Python can't determine which object to free first (they fixed it in 3.4 for all but foreign objects). In these cases it does the safest thing which is to stick the objects in a list of objects which can't be freed (gc.garbage) and take the memory loss (leaving it up to you to fix it yourself and remove the entries from the list).

From Python 3.4 they changed things and I think the only cases where objects don't get freed now is when you have foreign objects (e.g. stuff written in C) that still use the old tp_del rather than the new tp_finalize.
She who travels light — forgot something.

Heater
Posts: 15364
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python program freezes

Mon Jan 16, 2017 9:10 pm

OK, thanks.

I can imagine that any C code used can make a mess of memory. It's very good for that :) O was just wondering about Python itself.
Memory in C++ is a leaky abstraction .

Return to “General discussion”