And, when you are dealing with really large files, this difference can be huge. Remember that all memory is virtual, which means that it can unexpectedly become a disk-file. If you are processing a one-megabyte file on a machine with nothing better to do and plenty of RAM to spare, sure, it might be just fine to “slurp” the whole file into memory. However, if the size of the file begins to approach the amount of RAM that the operating system is willing to devote to your process’s working set, the data will start to spill-over into the swap file. We used to call that “going into Maytag® Mode,” back in the days when disk drives were little boxes that sat on the floor, and suddenly one of them would start vibrating like a Maytag washing-machine on the “spin” cycle. Needless to say, throughput for the entire machine goes to hell.
If you read the file a line at a time, operating systems are pretty smart. They will automatically buffer the data, and they will designate the RAM which they use for that purpose as “buffer,” thus known to be an opportunistic use of memory and the first thing to be discarded if a short-on-storage (aptly called, “SOS”) condition begins to develop. If you slurp the entire file, you create an SOS condition, and the operating system has no idea what RAM is being used merely as a buffer. All it knows is that your process has become a giant elephant that is now stepping and pooping on everybody else.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link or
or How to display code and escape characters
are good places to start.
|