LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2008, 06:16 AM   #1
Ronayn
Member
 
Registered: Jan 2006
Posts: 55

Rep: Reputation: 0
File I/O and System Memory


Hello,

I have designed an application which reads in X-sized chunks from a large file (1G) and writes each of the chunks out to its own file.

I have noticed that while the program is running, the system memory (shown by free) gets clobbered. In fact, even when the application closes, the system memory remains at a very low level.

I have noticed this "problem" before when doing file I/O. Is this just how Linux works? Please note that I am not doing any dynamic memory allocation in the code (other than to create the initial buffer, which I reuse and delete at the end of the application).

I am currently running this app on RedHat 9, kernel 2.4.20-6smp.
 
Old 12-17-2008, 07:13 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes, this sounds very normal. Linux keeps file data it has read in memory. Chances are that it needs to be read again soon, and when it is still in memory, it will be way faster.

So Linux uses almost all memory for IO-caching. When more memory is requested by programs, it will be made available to the programs, and less room will be used for caching and buffering of IO.

The program "free" gives information about how much memory is free and how much is used for caching and buffering.
Code:
bash$ free -m
             total       used       free     shared    buffers     cached
Mem:          3289       3160        128          0        111       2399
-/+ buffers/cache:        649       2639
Swap:         9538          0       9538
The -m option makes it show number in megabytes.
The number in blue is the one you'd want to watch to see it memory is being eaten by some program. It indicates free memory, counting cache and buffers as "free".
 
Old 12-18-2008, 08:16 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just FYI, RedHat 9, kernel 2.4.20-6smp is very old (about 10 versions behind the replacement RH Fedora 10) and hasn't been updated/supported (inc security holes) for yrs. Try to upgrade if possible.
 
Old 12-19-2008, 03:48 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yes this is interesting.

i wrote a perl program to test buffer sizes once,
a buffer writer piped into a reader.

I noticed when I print'd without a \n the system memory just filled up.
(due to line buffering)

interestingly on linux most times it eventually froze up the system
needing a power off.
solaris and BSD kicked the process off.

it didn't happen using C stdio strangely.

buffer_write
Code:
#!/usr/bin/perl  -s

$\ = undef;

$main::size ||= 100;

$count;

$| = 1 if $pipe;

sub do_write {

    $buf = chr int(rand(25)+65);
    $message = $buf x $size;
    $message .= "\n" if $line;;
    printf "$message";
    $count += length $message;
    warn "$count bytes\n";
    
}

while ($loop || <>) {

    do_write;
}
Code:
#!/usr/bin/perl -n

print length($_), "\n";
print;
 
Old 12-23-2008, 06:37 AM   #5
Ronayn
Member
 
Registered: Jan 2006
Posts: 55

Original Poster
Rep: Reputation: 0
Thanks to all for the replies

And yes, I want to update my OS -- however, I still have projects that require support and are running under RH9.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
installed system memory differs from mounted (available) memory landroni Debian 5 08-13-2007 06:43 AM
Information for virtual memory file system peiwai Linux - Software 1 05-01-2006 12:09 PM
USB memory stick file system linmix Linux - Hardware 2 12-02-2004 05:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration