Take it to the Limit

It’s been a while since I’ve been seriously concerned about wasting system resources. Back when I programmed a TRS-80 Model III with only 48K of RAM, keeping an eye on memory usage was vital. Today, not so much. In fact, if you desire to write code that consumes a lot of memory, CPU time, or other system resources, more power to you!

The only time I’m concerned about system resources is when processing humongous graphics files. Even then, the size is only a few megabytes, which shouldn’t be an issue for a modern system. And if the malloc() or realloc() function can’t find more memory, NULL is returned and the program reacts to the situation accordingly.

When you really want to know the limits, however, you can use the getrlimit() function, defined in the sys/resource.h header file. Here’s the man page format:

int getrlimit(int resource, struct rlimit *rlp);

The first argument is one of several defined constants, which are listed on the man page. Here are two choices:

RLIMIT_DATA, which directs the function to return the maximum number of bytes the process (your program) can access.

RLIMIT_FSIZE, which directs the function to return the largest file size (in bytes) that can be created.

Other options are available return other maximum values, depending on which resources you’re trying to pinch.

The second argument is a pointer to an rlimit structure. This structure has two members:

rlim_t rlim_cur, which represents the current limit.

rlim_t rlim_max, which represents the maximum limit.

The rlim_t typecast is either a long unsigned or double-long unsigned integer value.

The getrlimit() function returns zero upon success, -1 otherwise. The global errno variable is set accordingly and can be examined for further details.

In this sample code, the limit on how much data the program can access is output:

#include <stdio.h>
#include <sys/resource.h>

int main()
{
    struct rlimit limit;
    int r;

    r = getrlimit( RLIMIT_DATA, &limit );
    if( r==-1 )
    {
        fprintf(stderr,"Some kinda error\n");
        return(1);
    }

    printf("Current soft limit on data is %llu\n",
            limit.rlim_cur
          );
    printf("Current hard limit on data is %llu\n",
            limit.rlim_max
          );
    
    return(0);
}

Line 6 declares the rlimit structure variable limit as a regular variable, not a pointer. For some reason, accessing the members proved difficult when I declared it as a pointer.

The getrlimit() function is called at Line 9. The two structure members limit.rlim_cur and limit.rlim_max are output from Lines 16 onward. (Your compiler may balk at the %llu placeholder. If so, use %lu instead.)

Here’s the output on my machine:

Current soft limit on data is 9223372036854775807
Current hard limit on data is 9223372036854775807

That’s one huge number, effectively 9 quintillion bytes, which is surprising given this machine has 16GB of RAM installed. This result leads me to believe the function erred, but -1 wasn’t returned so go figure.

Curious, I test-ran the code on Ubuntu Linux and the output was twice as much, or around 18 quintillion bytes. So I’m not feeling good about the results, but at least the function is available, should you ever consider having your code access and use such humongous quantities of system resources.

3 thoughts on “Take it to the Limit

  1. I think the getrlimit must just return the maximum addressable memory for the system it is run on. I tried your code and got 18446744073709551615 which is 2^64 as I have a 64 bit PC.

    However, the value you got is 2^63!!!!!!!!!!!!!!!!! I think you should contact the manufacturer of your machine and tell them they forgot to put one of the bits in.

    If getrlimit does just return 2 to the power of 32, 64 or whatever it doesn’t seem very useful as the limit is only theoretical unless you have a huge cluster or supercomputer that does actually have that much memory.

    The actual limit for practical purposes would be however much RAM + virtual memory the OS is willing to let you borrow.

  2. The function’s more useful brother is setrlimit(), which caps resource usage. Still, I feel these are jobs for the operating system. Gadzooks if you want to run a program that consumes all that memory!

Leave a Reply