Restarting program using os.system to save memory?How to determine CPU and memory consumption from inside a process?Which Python memory profiler is recommended?How to measure actual memory usage of an application or process?How do I discover memory usage of my application in Android?Creating a memory leak with JavaSave plot to image file instead of displaying it using MatplotlibWeb parsing with python beautifulsoup producing inconsistent resultscraping issue (dynamic content)(without selenium)Using Beautiful Soup how can I return this value and use it as an integer?I am getting text error while the code is appicable for on company using python beautifulsoup

If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?

How would a lunar colony attack Earth?

Database Cache Memory in Performance Monitor drops down significantly after DBCC CheckDB

My employer is refusing to give me the pay that was advertised after an internal job move

ULQ2003 not driving a Relay properly

Create two random teams from a list of players

Why didn't General Martok receive discommendation in Star Trek: Deep Space Nine?

Patio gate not at right angle to the house

If I buy and download a game through second Nintendo account do I own it on my main account too?

What do the novel titles of The Expanse series refer to?

What kind of horizontal stabilizer does a Boeing 737 have?

How to identify in Apex what picklist fields are restricted

Should 2FA be enabled on service accounts?

Given mean and SD, can we approximate the underlying distribution?

In the Schrödinger equation, can I have a Hamiltonian without a kinetic term?

Can machine learning learn a function like finding maximum from a list?

Prepare a user to perform an action before proceeding to the next step

How do I make my photos have more impact?

What Marvel character has this 'W' symbol?

Word for soundtrack music which is part of the action of the movie

Should I intervene when a colleague in a different department makes students run laps as part of their grade?

Why would an invisible personal shield be necessary?

Introduction to the Sicilian

How to remove rebar passing through an inaccessible pipe



Restarting program using os.system to save memory?


How to determine CPU and memory consumption from inside a process?Which Python memory profiler is recommended?How to measure actual memory usage of an application or process?How do I discover memory usage of my application in Android?Creating a memory leak with JavaSave plot to image file instead of displaying it using MatplotlibWeb parsing with python beautifulsoup producing inconsistent resultscraping issue (dynamic content)(without selenium)Using Beautiful Soup how can I return this value and use it as an integer?I am getting text error while the code is appicable for on company using python beautifulsoup






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm using Python to scrape a website for specific links or keywords, and want to send a request about every 5 seconds. Initially I was using a while True loop to send my requests every 5 seconds, but with every loop my program used about 1mb of memory more than before. With me wanting to run my program on a Raspberry Pi for potentially multiple days, this would fill up the memory pretty quickly.



Instead of using the while True loop, once my program ran through all the code I use



os.system("python program.py") 


to restart the program, meaning running through the code over and over won't gain any memory and stay at an average of about 38mb.



So my question is: Is there any downside of me doing this? Can any problems occur when I want my computer to run a new instance of a program every 5 seconds for days?



EDIT: added code



import requests
from bs4 import BeautifulSoup, SoupStrainer
import time
import os
import psutil

while True:
url = "https://en.wikipedia.org/wiki/Main_Page"
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
for link in soup.find_all("a"):
print(link.get('href'))

time.sleep(5)
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
time.sleep(5)









share|improve this question





















  • 6





    If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

    – zvone
    Mar 26 at 22:04






  • 1





    If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

    – roganjosh
    Mar 26 at 22:04











  • This is a classic XY Problem in my opinion.

    – Random Davis
    Mar 26 at 22:22











  • @zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:31






  • 1





    This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

    – zvone
    Mar 26 at 23:19

















1















I'm using Python to scrape a website for specific links or keywords, and want to send a request about every 5 seconds. Initially I was using a while True loop to send my requests every 5 seconds, but with every loop my program used about 1mb of memory more than before. With me wanting to run my program on a Raspberry Pi for potentially multiple days, this would fill up the memory pretty quickly.



Instead of using the while True loop, once my program ran through all the code I use



os.system("python program.py") 


to restart the program, meaning running through the code over and over won't gain any memory and stay at an average of about 38mb.



So my question is: Is there any downside of me doing this? Can any problems occur when I want my computer to run a new instance of a program every 5 seconds for days?



EDIT: added code



import requests
from bs4 import BeautifulSoup, SoupStrainer
import time
import os
import psutil

while True:
url = "https://en.wikipedia.org/wiki/Main_Page"
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
for link in soup.find_all("a"):
print(link.get('href'))

time.sleep(5)
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
time.sleep(5)









share|improve this question





















  • 6





    If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

    – zvone
    Mar 26 at 22:04






  • 1





    If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

    – roganjosh
    Mar 26 at 22:04











  • This is a classic XY Problem in my opinion.

    – Random Davis
    Mar 26 at 22:22











  • @zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:31






  • 1





    This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

    – zvone
    Mar 26 at 23:19













1












1








1








I'm using Python to scrape a website for specific links or keywords, and want to send a request about every 5 seconds. Initially I was using a while True loop to send my requests every 5 seconds, but with every loop my program used about 1mb of memory more than before. With me wanting to run my program on a Raspberry Pi for potentially multiple days, this would fill up the memory pretty quickly.



Instead of using the while True loop, once my program ran through all the code I use



os.system("python program.py") 


to restart the program, meaning running through the code over and over won't gain any memory and stay at an average of about 38mb.



So my question is: Is there any downside of me doing this? Can any problems occur when I want my computer to run a new instance of a program every 5 seconds for days?



EDIT: added code



import requests
from bs4 import BeautifulSoup, SoupStrainer
import time
import os
import psutil

while True:
url = "https://en.wikipedia.org/wiki/Main_Page"
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
for link in soup.find_all("a"):
print(link.get('href'))

time.sleep(5)
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
time.sleep(5)









share|improve this question
















I'm using Python to scrape a website for specific links or keywords, and want to send a request about every 5 seconds. Initially I was using a while True loop to send my requests every 5 seconds, but with every loop my program used about 1mb of memory more than before. With me wanting to run my program on a Raspberry Pi for potentially multiple days, this would fill up the memory pretty quickly.



Instead of using the while True loop, once my program ran through all the code I use



os.system("python program.py") 


to restart the program, meaning running through the code over and over won't gain any memory and stay at an average of about 38mb.



So my question is: Is there any downside of me doing this? Can any problems occur when I want my computer to run a new instance of a program every 5 seconds for days?



EDIT: added code



import requests
from bs4 import BeautifulSoup, SoupStrainer
import time
import os
import psutil

while True:
url = "https://en.wikipedia.org/wiki/Main_Page"
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
for link in soup.find_all("a"):
print(link.get('href'))

time.sleep(5)
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
time.sleep(5)






python memory while-loop os.system






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 22:32







Viet NaM

















asked Mar 26 at 22:02









Viet NaMViet NaM

255 bronze badges




255 bronze badges










  • 6





    If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

    – zvone
    Mar 26 at 22:04






  • 1





    If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

    – roganjosh
    Mar 26 at 22:04











  • This is a classic XY Problem in my opinion.

    – Random Davis
    Mar 26 at 22:22











  • @zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:31






  • 1





    This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

    – zvone
    Mar 26 at 23:19












  • 6





    If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

    – zvone
    Mar 26 at 22:04






  • 1





    If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

    – roganjosh
    Mar 26 at 22:04











  • This is a classic XY Problem in my opinion.

    – Random Davis
    Mar 26 at 22:22











  • @zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:31






  • 1





    This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

    – zvone
    Mar 26 at 23:19







6




6





If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

– zvone
Mar 26 at 22:04





If the memory consumption is increasing, that means you probably have a memory leak. Instead of restarting the application, fix the memory leak problem.

– zvone
Mar 26 at 22:04




1




1





If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

– roganjosh
Mar 26 at 22:04





If you are willing to restart your program and just trash any data you've accumulated then it suggests that you're persisting data on each loop that you don't need. Without seeing your code, it's not possible to understand why the memory footprint grows but clearly you don't intend it

– roganjosh
Mar 26 at 22:04













This is a classic XY Problem in my opinion.

– Random Davis
Mar 26 at 22:22





This is a classic XY Problem in my opinion.

– Random Davis
Mar 26 at 22:22













@zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

– Viet NaM
Mar 26 at 22:31





@zvone I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

– Viet NaM
Mar 26 at 22:31




1




1





This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

– zvone
Mar 26 at 23:19





This seems to be a case of premature optimization. There is no memory leak. When I run this code without time.sleep (so memory consumption grows much much faster), it always stays at around 50 MB (as expected). It is normal that it sometimes goes up for various reasons, and it does, but then it comes down again. So everything is fine.

– zvone
Mar 26 at 23:19












1 Answer
1






active

oldest

votes


















1














To be honest it sounds like the program should be re-written. If you are storing data internally which you don't need (which from the sounds of it you are), you need to ask yourself why you are. If you need that data, write it out and reset the variable.



Some more clarity would really help here - ie - the code itself so we could figure out the real problem.






share|improve this answer

























  • I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:32











  • Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

    – Pedro Lacerda
    Mar 26 at 22:34










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366828%2frestarting-program-using-os-system-to-save-memory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To be honest it sounds like the program should be re-written. If you are storing data internally which you don't need (which from the sounds of it you are), you need to ask yourself why you are. If you need that data, write it out and reset the variable.



Some more clarity would really help here - ie - the code itself so we could figure out the real problem.






share|improve this answer

























  • I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:32











  • Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

    – Pedro Lacerda
    Mar 26 at 22:34















1














To be honest it sounds like the program should be re-written. If you are storing data internally which you don't need (which from the sounds of it you are), you need to ask yourself why you are. If you need that data, write it out and reset the variable.



Some more clarity would really help here - ie - the code itself so we could figure out the real problem.






share|improve this answer

























  • I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:32











  • Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

    – Pedro Lacerda
    Mar 26 at 22:34













1












1








1







To be honest it sounds like the program should be re-written. If you are storing data internally which you don't need (which from the sounds of it you are), you need to ask yourself why you are. If you need that data, write it out and reset the variable.



Some more clarity would really help here - ie - the code itself so we could figure out the real problem.






share|improve this answer













To be honest it sounds like the program should be re-written. If you are storing data internally which you don't need (which from the sounds of it you are), you need to ask yourself why you are. If you need that data, write it out and reset the variable.



Some more clarity would really help here - ie - the code itself so we could figure out the real problem.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 22:10









amoeba_wonderboyamoeba_wonderboy

112 bronze badges




112 bronze badges















  • I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:32











  • Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

    – Pedro Lacerda
    Mar 26 at 22:34

















  • I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

    – Viet NaM
    Mar 26 at 22:32











  • Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

    – Pedro Lacerda
    Mar 26 at 22:34
















I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

– Viet NaM
Mar 26 at 22:32





I added a code which is essentially what I'm doing, but even this simple code causes my memory to go up every loop. It started with 32mb and gains about 0.7mb every loop.

– Viet NaM
Mar 26 at 22:32













Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

– Pedro Lacerda
Mar 26 at 22:34





Instagram does it. They has some server that runs until the memory consumption is to high, then they restart it. They used a modified version of CPython with the garbage collector disabled to be honest. It wasn't a memory leak like the one in the question.

– Pedro Lacerda
Mar 26 at 22:34








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366828%2frestarting-program-using-os-system-to-save-memory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해