The Crystal Programming Language Forum

Adding memory to System

We have this System module already that has cpu_count. I was just thinking that it would be cool if it also could report the RAM on the system.

I came across a stackoverflow post that showed how you can get some of this info on Linux with

#include <sys/sysinfo.h>

   int sysinfo(struct sysinfo *info);

   struct sysinfo {
       long uptime;             /* Seconds since boot */
       unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
       unsigned long totalram;  /* Total usable main memory size */
       unsigned long freeram;   /* Available memory size */
       unsigned long sharedram; /* Amount of shared memory */
       unsigned long bufferram; /* Memory used by buffers */
       unsigned long totalswap; /* Total swap space size */
       unsigned long freeswap;  /* swap space still available */
       unsigned short procs;    /* Number of current processes */
       unsigned long totalhigh; /* Total high memory size */
       unsigned long freehigh;  /* Available high memory size */
       unsigned int mem_unit;   /* Memory unit size in bytes */
       char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */

I don’t know C, so I’m not sure how this works, but as I understand it, sys/sysinfo doesn’t exist on Mac (and probably not on Windows either). So having something built in that already understands the current system to give us this information would be pretty helpful. Even if it was just something like total RAM to start could be cool. System.total_ram


That would be nice. Unfortunately I’m not familiar with C either. I know that Phoenix Framework built with Elixir implements something like this in their Live Dashboard. I think it uses Erlang to pull the info.

Would be nice to have out of the box.

1 Like

The problem here is that adding system information data that isn’t strictly necessary but nice to have is a bottomless hole of nice to haves that won’t stop until you have reimplemented ohai ( ) in Crystal. Which would certainly be very nice to have, but would perhaps fit better in a shard?

1 Like

You should take a look on this


That does the trick.