free – Display amount of free and used memory in the system

commandline session

$free -m -c 10 -s 10
             total       used       free     shared    buffers     cached
Mem:          1948       1816        131          0        105        536
-/+ buffers/cache:       1175        773
Swap:         5706         19       5687

             total       used       free     shared    buffers     cached
Mem:          1948       1816        131          0        105        536
-/+ buffers/cache:       1175        773
Swap:         5706         19       5687

             total       used       free     shared    buffers     cached
Mem:          1948       1820        128          0        105        538
-/+ buffers/cache:       1176        772
Swap:         5706         19       5687

             total       used       free     shared    buffers     cached
Mem:          1948       1820        128          0        105        538
-/+ buffers/cache:       1176        772
Swap:         5706         19       5687

             total       used       free     shared    buffers     cached
Mem:          1948       1789        159          0         96        529
-/+ buffers/cache:       1163        785
Swap:         5706         20       5686

             total       used       free     shared    buffers     cached
Mem:          1948       1797        151          0         96        533
-/+ buffers/cache:       1167        780
Swap:         5706         20       5686

             total       used       free     shared    buffers     cached
Mem:          1948       1803        145          0         96        533
-/+ buffers/cache:       1173        775
Swap:         5706         20       5686

             total       used       free     shared    buffers     cached
Mem:          1948       1791        157          0         95        526
-/+ buffers/cache:       1168        780
Swap:         5706         20       5686

             total       used       free     shared    buffers     cached
Mem:          1948       1790        158          0         95        526
-/+ buffers/cache:       1167        780
Swap:         5706         20       5686

             total       used       free     shared    buffers     cached
Mem:          1948       1792        156          0         95        527
-/+ buffers/cache:       1168        780
Swap:         5706         20       5686
$

$free -m -c 10 -s 3
             total       used       free     shared    buffers     cached
Mem:          1948       1778        170          0         88        531
-/+ buffers/cache:       1158        790
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1778        169          0         88        532
-/+ buffers/cache:       1158        790
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1778        170          0         88        531
-/+ buffers/cache:       1158        790
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1781        167          0         88        532
-/+ buffers/cache:       1160        788
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1781        167          0         88        531
-/+ buffers/cache:       1160        788
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1781        167          0         88        531
-/+ buffers/cache:       1160        788
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1681        266          0         88        529
-/+ buffers/cache:       1063        885
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1683        2100          0         88        533
-/+ buffers/cache:       1061        887
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1703        244          0         88        533
-/+ buffers/cache:       1081        867
Swap:         5706         21       5685

             total       used       free     shared    buffers     cached
Mem:          1948       1710        238          0         88        534
-/+ buffers/cache:       1086        862
Swap:         5706         21       5685
$

STITCHED TO ANOTHER free

line with cairo



/* Jeffrin Jose Licensed GPL v3 Copyright August 2010
GPL --> http://www.gnu.org/copyleft/gpl.html */

#include

int
main ()
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 800, 600);
cairo_t *cr = cairo_create (surface);

cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_set_line_width(cr,1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_line_to(cr,20,300);
cairo_stroke(cr);

cairo_destroy (cr);
cairo_surface_write_to_png (surface, "hello.png");
cairo_surface_destroy (surface);
return 0;
}

free – Display amount of free and used memory in the system

A UNIX Command

$free
             total       used       free     shared    buffers     cached
Mem:        507008     432908      74100          0      14700     186816
-/+ buffers/cache:     231392     275616
Swap:      1485972          0    1485972
$free -m
             total       used       free     shared    buffers     cached
Mem:           495        422         72          0         14        182
-/+ buffers/cache:        225        269
Swap:         1451          0       1451
$

UNIX Explanation

Display amount of free and used memory in the system.

Related Source Code Exposition


#define S(X) ( ((unsigned long long)(X) <> shift)

const char help_message[] =
"usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]\n"
" -b,-k,-m,-g show output in bytes, KB, MB, or GB\n"
" -l show detailed low and high memory statistics\n"
" -o use old format (no -/+buffers/cache line)\n"
" -t display total for RAM + swap\n"
" -s update every [delay] seconds\n"
" -c update [count] times\n"
" -V display version information and exit\n"
;

int main(int argc, char *argv[]){
int i;
int count = 0;
int shift = 10;
int pause_length = 0;
int show_high = 0;
int show_total = 0;
int old_fmt = 0;

/* check startup flags */
while( (i = getopt(argc, argv, "bkmglotc:s:V") ) != -1 )
switch (i) {
case 'b': shift = 0; break;
case 'k': shift = 10; break;
case 'm': shift = 20; break;
case 'g': shift = 30; break;
case 'l': show_high = 1; break;
case 'o': old_fmt = 1; break;
case 't': show_total = 1; break;
case 's': pause_length = 1000000 * atof(optarg); break;
case 'c': count = strtoul(optarg, NULL, 10); break;
case 'V': display_version(); exit(0);
default:
fwrite(help_message,1,strlen(help_message),stderr);
return 1;
}

do {
meminfo();
printf(" total used free shared buffers cached\n");
printf(
"%-7s %10Lu %10Lu %10Lu %10Lu %10Lu %10Lu\n", "Mem:",
S(kb_main_total),
S(kb_main_used),
S(kb_main_free),
S(kb_main_shared),
S(kb_main_buffers),
S(kb_main_cached)
);
// Print low vs. high information, if the user requested it.
// Note we check if low_total==0: if so, then this kernel does
// not export the low and high stats. Note we still want to
// print the high info, even if it is zero.
if (show_high) {
printf(
"%-7s %10Lu %10Lu %10Lu\n", "Low:",
S(kb_low_total),
S(kb_low_total - kb_low_free),
S(kb_low_free)
);
printf(
"%-7s %10Lu %10Lu %10Lu\n", "High:",
S(kb_high_total),
S(kb_high_total - kb_high_free),
S(kb_high_free)
);
}
if(!old_fmt){
unsigned KLONG buffers_plus_cached = kb_main_buffers + kb_main_cached;
printf(
"-/+ buffers/cache: %10Lu %10Lu\n",
S(kb_main_used - buffers_plus_cached),
S(kb_main_free + buffers_plus_cached)
);
}
printf(
"%-7s %10Lu %10Lu %10Lu\n", "Swap:",
S(kb_swap_total),
S(kb_swap_used),
S(kb_swap_free)
);
if(show_total){
printf(
"%-7s %10Lu %10Lu %10Lu\n", "Total:",
S(kb_main_total + kb_swap_total),
S(kb_main_used + kb_swap_used),
S(kb_main_free + kb_swap_free)
);
}
if(pause_length){
fputc('\n', stdout);
fflush(stdout);
if (count != 1) usleep(pause_length);
}
} while(pause_length && --count);

return 0;
}

Source Code Highlight


#define S(X) ( ((unsigned long long)(X) <> shift)

it  takes  a  number,  X,  casts  it  to  a  'long  long'
(presumably the  longest int type  on your system  ?) and
then it left-shifts by  10 (meaning, the the positions of
all bits are shifted 10 places to the left) only to shift
it back right 'shift' number of positions.

source : nickname - psuedonymous. server - irc.freenode.net

Featured Image


Related Knowledge

free displays the total  amount of free and used physical
and swap  memory in  the system, as  well as  the buffers
used by  the kernel.  The shared memory  column should be
ignored; it is obsolete.

source : debian manual for free.