473,657 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory error

def windows():
import os
excludes = ['hiberfil.sys', 'ipnathlp.dll', 'helpctr.exe', 'etc',
'etc', 'etc']
size_list = []
for root, dirs, files in os.walk('/'):
total = [x for x in files if x not in excludes]
for t in total:
s = file(os.path.jo in(root,t))
size = s.read()
size_list.appen d(size)
s.close()

windows()

The above function crashes with a memory error on Windows XP Pro at the
'size = s.read()' line. Page File usage (normally ~120 MB) will rise to
300+ MB and pythonw.exe will consume about 200 MB of actual ram right
before the crash. The machine has 512 MB of ram and is doing nothing
else while running the script.

I've written the script several ways, all with the same result. I've
noticed that a binary read 'rb' consumes almost twice as much physical
memory and causes the crash to happen quicker, but that's about it.

Initially, I wanted to use Python to open every file on the system (that
could be opened) and read the contents so I'd know the size of the file
and then add all of the reads to a list that I'd sum up. Basically
attempt to add up all bytes on the machine's disk drive.

Any ideas on what I'm doing wrong or suggestions on how to do this
differently?

Thanks,

Bart
Jul 18 '05 #1
3 3558
Bart Nessux wrote:
def windows():
import os
excludes = ['hiberfil.sys', 'ipnathlp.dll', 'helpctr.exe', 'etc',
'etc', 'etc']
size_list = []
for root, dirs, files in os.walk('/'):
total = [x for x in files if x not in excludes]
for t in total:
s = file(os.path.jo in(root,t))
size = s.read()
size_list.appen d(size)
s.close()

windows()

The above function crashes with a memory error on Windows XP Pro at the
'size = s.read()' line. Page File usage (normally ~120 MB) will rise to
300+ MB and pythonw.exe will consume about 200 MB of actual ram right
before the crash. The machine has 512 MB of ram and is doing nothing
else while running the script.

I've written the script several ways, all with the same result. I've
noticed that a binary read 'rb' consumes almost twice as much physical
memory and causes the crash to happen quicker, but that's about it.

Initially, I wanted to use Python to open every file on the system (that
could be opened) and read the contents so I'd know the size of the file
and then add all of the reads to a list that I'd sum up. Basically
attempt to add up all bytes on the machine's disk drive.

Any ideas on what I'm doing wrong or suggestions on how to do this
differently?

Your building an array containing the *contents* of all your files.
If you really need to use read(), use "size = len(s.read())", but this
still requires to read and hold a complete file at a time in memory (and
probably chokes when it stumbles over your divx collection ;)
I think using os.stat() should be better...
Jul 18 '05 #2
Am Mittwoch, 2. Juni 2004 15:11 schrieb Bart Nessux:
size = s.read()
You read the complete content of the file here. size will not contain the
length of the file, but the complete file data. What you want is either
len(s.read()) (which is sloooooooooow), or have a look at os.path.getsize ().
size_list.appen d(size)


This appends the complete file to the list. And as such should explain the
memory usage you're seeing...

HTH!

Heiko.

Jul 18 '05 #3
On Wed, 02 Jun 2004 09:11:13 -0400, Bart Nessux
<ba*********@ho tmail.com> wrote:
def windows():
import os
excludes = ['hiberfil.sys', 'ipnathlp.dll', 'helpctr.exe', 'etc',
'etc', 'etc']
size_list = []
for root, dirs, files in os.walk('/'):
total = [x for x in files if x not in excludes]
for t in total:
s = file(os.path.jo in(root,t))
size = s.read()
size_list.appen d(size)
s.close()

windows()
Yeah, what the other guys said about os.stat and os.path.getsize .
Also, if you really want to read the actual file into memory, just get
small chunks and add those up.

Like (untested):

numberofbytes = 0
CHUNKSIZE = 4096
for root,dirs, files in os.walk('/'):
for name in files:
if name not in excludes:
f = file(os.path.jo in(root,name))
while 1:
s = f.read(CHUNKSIZ E)
if not s:
f.close()
break
numberofbytes += len(s)

this way you never have more than 4k of data in memory at once.
(well it might be 8k, I dont know enought about the internals to tell
you when the previous 's' is garbage collected.)
<{{{*>


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4393
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py
1
4359
by: Attila.Rajmund.Nohl | last post by:
Hello! I'm using KAI C++ Compiler (KCC) Version 4.0d on Sparc Solaris 8 with Sun WorkShop 6 update 2 backend (KCC compiles C++ code to C than uses the Sun compiler to produce machine executables). When I run our program under purify to check for memory errors, I get the following interesting error messages: MSE: Memory segment error: * This is occurring while in:
6
2291
by: Andrzej | last post by:
Used to read newsgroup for answers, now have to ask for them as well. I have an application (C#, .NET 1.1) that connects to local db on MSDE 2000 SP3a (using ADO from MDAC 2.71) on one side and to a web service on the other (not relevant I guess). Some stored procedures consume xml produced by DataSet objects (values as elements) using OPENXML statements. The application should be distributed among users having also Win98. Established...
25
2369
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My memory profile shows that those instances survive 3 rounds of GC collections - it's not what I expected. In my real code, CMyClass occupies big amount of memory and they all share one stance of another class that I don't have enough memory hold...
4
5997
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over data from August or any prior month for that matter works fine which is why this is so weird. Note that June 2004 through today is stored in the same f_pageviews table. Nothing has changed on the server in the last couple of months. I upgraded...
2
3812
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some breaking point. The application is a typical web application w/ 2 web servers running Apache/Tomcat connecting to a dedicated DB server running only MySQL. This seems to occur as a result of running many statements in a single transaction, both...
6
4153
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
5
24702
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
27
2945
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a. try { a = new int ;
2
3308
by: ravishi | last post by:
Well, this is my first topic at this forum and I'm a newbie on C programming too. I'm coding a little program and I've used some "dynamic arrays" on it. Compiling and running the program on Linux worked fine for me. But I was very curious to see how the program runs on Windows, so I decided to test it. Compilation was fine too, but when I run the program, I get a memorry allocation error. Here's the code: int *tspos, *tepos = NULL;...
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.