Based on the comments in this C tutorial page which I'm using to review C after some time not working with it, I expect that a simple program compiled with a shared version of library will appear to use less memory than a version of that program using the static version of the library.
Here is a simple example program, which requests user input just so the program will be idle while I go use top
and ps
to inspect it. The goal is to compile the program with just -lm
(linking libmath), then later compile it with -lm --static
. When I run each program, I should see the static option taking up less memory associated with its running process.
/* lib_a_vs_so.c */
#include <math.h>
#include <stdio.h>
void main()
{
double x = sin(3.14);
int user_input;
printf("Enter a number: ");
scanf("%d", &user_input);
printf("You entered %d and sin(3.14) is %.2f\n", user_input, x);
}
The steps to compile two different versions:
c99 -o so_version lib_a_vs_so.c -lm
c99 -o a_version lib_a_vs_so.c -lm --static
and the output from top
on my system (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)) when I run both programs.
top - 19:06:51 up 10:20, 5 users, load average: 0.06, 0.24, 0.25
Tasks: 2 total, 0 running, 2 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.4 us, 0.8 sy, 0.0 ni, 96.4 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 16327932 total, 7522656 used, 8805276 free, 435692 buffers
KiB Swap: 0 total, 0 used, 0 free. 2836848 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7010 ely 20 0 4192 356 276 S 0.0 0.0 0:00.00 so_version
7025 ely 20 0 1080 264 212 S 0.0 0.0 0:00.00 a_version
Why does so_version
appear to use more memory?