Visualize C Data-Types on Linux with Ruby

Intro

int main() {
char *b = "Hello!" ;
while(1) {
char *a = "This is a String!" ;
b = "Something!" ;
}
}
GC.disable         # Disable the Garbage Collection featurewhile true
b = "Something" # Create a new String and assign it to b
end

Our Plan

  1. We are going to create large C files with the help of Ruby.
  2. In the C files we are going to declare a humongous amount of variables like float, int, double, long doubleetc.
  3. Each code will also measure the memory of themselves in the end and show the amount of memory they are consuming before exiting. It actually uses Linux's /proc/PID/statm file.
  4. We are then going to compile and execute them to see the memory usage they are consuming.

Creating the Main Ruby File

int _a=0,_b=0,_c=0…_n=0;
TYPES = <<~'EOF'
char % int % short % long % long long
float % double % long double
EOF
unsigned long long calcMemUsage() {
unsigned long long resident, shared ;
FILE *f = fopen("/proc/self/statm", "r") ;
if (fscanf(f, "%*llu %llu %llu", &resident, &shared) != 2) return 0 ;
fclose(f) ;
return (unsigned long long)((resident - shared) * sysconf(_SC_PAGESIZE)) ;
}
$ gcc -O0 cfiles/double.c -o double
$ ./double
Memory: 786432 Bytes

Generate Makefile

# Filename: Makefileall:
echo "Hello World!"
make all
$ ruby generate_sources.rb
$ ruby generate_makefile.rb
CC=gcc
CFLAGS="-O0"
all: generate char int short long long_long float double long_double
char:
$(CC) $(CFLAGS) "cfiles/char.c" -o "binaries/char"
int:
$(CC) $(CFLAGS) "cfiles/int.c" -o "binaries/int"
short:
$(CC) $(CFLAGS) "cfiles/short.c" -o "binaries/short"
long:
$(CC) $(CFLAGS) "cfiles/long.c" -o "binaries/long"
long_long:
$(CC) $(CFLAGS) "cfiles/long long.c" -o "binaries/long_long"
float:
$(CC) $(CFLAGS) "cfiles/float.c" -o "binaries/float"
double:
$(CC) $(CFLAGS) "cfiles/double.c" -o "binaries/double"
long_double:
$(CC) $(CFLAGS) "cfiles/long double.c" -o "binaries/long_double"
Generated Makefile
Please Run "make all -j4"
$ make all -j4
$ time make all -j4
gcc "-O0" "cfiles/char.c" -o "binaries/char"
gcc "-O0" "cfiles/int.c" -o "binaries/int"
gcc "-O0" "cfiles/short.c" -o "binaries/short"
gcc "-O0" "cfiles/long.c" -o "binaries/long"
gcc "-O0" "cfiles/long long.c" -o "binaries/long_long"
gcc "-O0" "cfiles/float.c" -o "binaries/float"
gcc "-O0" "cfiles/long double.c" -o "binaries/long_double"
real 2m50.049s
user 10m1.554s
sys 0m1.147s

Execution

$ for i in binaries/* ; do echo "$i: `./$i`" ; done
binaries/char: Memory: 73728 Bytes
binaries/double: Memory: 729088 Bytes
binaries/float: Memory: 282624 Bytes
binaries/int: Memory: 282624 Bytes
binaries/long: Memory: 790528 Bytes
binaries/long_double: Memory: 1622016 Bytes
binaries/long_long: Memory: 798720 Bytes
binaries/short: Memory: 262144 Bytes
PC running the binary files and shows the memory usage
Average memory usage of different data types

Recap

  1. Created a new file called generate_sources.rb, and pasted the content from the above generate_sources.rb GitHub gist.
  2. Created a new file called generate_makefile.rb and pasted the content from the above generate_makefile.rb GitHub gist.
  3. ran N=100_000 ruby generate_sources.rb to generate 100,000 data types.
  4. Ran ruby generate_makefile.rb to generate the Makefile.
  5. Ran make all -j4 , replaced -j4with the suggested number of CPU by generate_makefile.rb
  6. Executed the files with the bash script for i in binaries/* ; do echo "$i: `./$i`" ; done . That showed the total memory usage by the 100K data types together.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store