C Programming

The jemalloc() Function in C

In any programming language, when we are doing some coding on a certain problem, the coder must remember his knowledge about two things. One is the time complexity, and another is the space complexity. Space complexity is the most important prospect of programming since our device memory is not unlimited. To execute a program with less memory, enhance our programming capability.

Remembering the previous concept about space complexity, the jemalloc() function is introduced in the C language to boost our programming performance.

Why Was jemalloc() Function Introduced?

The jemalloc() function is introduced in the C language to improve the performance of the server programs. The programmer controls any server program to bring the concept of threading. Multiple threads execute in a different manner to control the server program. The jemalloc() function helps the server program manage its memory complexity by introducing minimum memory when executing these threads.

Now, the jemalloc() function has its header file in C language. It gives a better option to reduce the drawback of the malloc() function or calloc() function when memory is allocated dynamically for the data or information in C language. Earlier, these predefined functions (malloc(), calloc()) were used. But, as these functions have certain limitations related to memory performances, the jemalloc() function will be introduced in C language.

How To Download and Install the jemalloc?

1. To download the updated version of the jemalloc, we have to use the following command:

$ wget https://github.com/jemalloc/jemalloc/releases/download/5.2.0/jemalloc-5.2.0.tar.bz2

 
After downloading the file from the previous site, we have to unzip the file:

$ tar xjvf jemalloc-5.2.0.tar.bz2

 

2. Next, we will open the downloaded version of the jemalloc-5.2.0 file.

$ cd jemalloc-5.2.0/

 

3. After opening the file, we will now configure the jemalloc.

$ ./configure --with-jemalloc-prefix=je_

 
To configure the jemalloc means we want to indicate in what procedure the jemalloc function will be compiled. Here, compilation will be done for malloc in the term of je_malloc, for calloc in the term of je_calloc. This will help avoid any confusing situations with the system’s libc system function malloc.

The configuring steps are given below, as a form of snapshots:


 

How To Compile jemalloc

To compile the jemalloc file, we have to install make. For this, we have to write a

$ sudo apt install make

 

When we give the password and press enter, the make will start to install. The following steps of the installation process of make are given in the following screenshots:

The Newly Generated Library Files

After the configuration is done, several library files are installed in the lib directory. They are the following:

    • libjemalloc.a
    • libjemalloc_pic_a
    • libjemalloc.so
    • libjemalloc.so.2


Among these library files, libjemalloc.a and libjemalloc.so.2 are the most important library files. The libjemalloc.a file represents a static library file and libjemalloc.so.2 represents a dynamic library file.

Application of the jemalloc() Function

We have to generate the following three directories:

    1. $ mkdir jemalloctesting
    2. $ mkdir ./ jemalloctesting/lib
    3. $ mkdir ./ jemalloctesting/include

Copying the Header Files

Now, we will copy the jemalloc.h, jemalloc_defs.h, and libjemalloc.a files inside and include lib jemalloc inside the jemalloc library.

Generating Header File

Now, we will generate a header file named userdefined_malloc.h. Then, we have to write several codes inside this header file. The codes are described below:

Programming Example 1

Creating a .c file to run the code by including the “userdefined_malloc.h” header file.

#include <stdio.h>
#include <stdlib.h>
#include “userdefined_malloc.h”

int main()
{
           
    //This is the pointer (point) that will hold the base address of the block created

    int* point;
    int n,
             j;

    // To get the numbers from the user
    printf ("Enter Numbers : ");
    scanf ("%d",&n);
    printf ("The numbers entered by the user : %d\n", n);

    // Allocating the memory dynamically using malloc()
    point = (int*)malloc(n * sizeof(int));

    // Checking if the memory allocated by malloc () or not
    if (point == NULL)
             {
        printf ("Memory not allocated.\n");
        exit(0);
                 
    }
    else
             {

        // Memory allocated
        printf ("memory allocated successfully by malloc ()\n");

        // for loop to get elements of the array
        for (j = 0; j < n; ++j) {
            point[j] = j + 1;
        }

        // displaying all the elements present in the array
        printf  ("The elements of the array are: ");

        for (j = 0; j < n; ++j) {
            printf("%d, ", point[j]);
        }
    }
             printf ("\n");
    return 0;
}

 

Generating the Makefile

 

$ gedit makefile

 

 

CC=gcc
CFLAGS=-Wall -g
INCLUDES=-I ./include/
ALLOC_DEP=./lib/libjemalloc.a
ALLOC_LINK=$(ALLOC_DEP) -lpthread -ldl

testing: testing.o
    $(CC) $(INCLUDES) $(CFLAGS) -o testing testing.o $(ALLOC_LINK)

testing.o: testing.c $(ALLOC_DEP)
    $(CC) -c $(INCLUDES) $(CFLAGS) testing.c
clean:
    rm -f testing testing.o

 

Output

 

$ make
$ ./testing

 

Explanation

Here, we are trying to run a program of the jemalloc() function by including the header file named “userdefined_malloc.h”. Now, we enter some values from the user and get memory dynamically by using the malloc() function. The malloc() function creates a block of memory and then returns its address. To print the values, we use a loop. The jemalloc() function successfully executes this program by including the header file in the program.

Check the Program Using the GDB Tool

 

$ gdb ./testing

 

 

 

Explanation

To debug or see step-by-step the same program, we can use “$ gdb ./testing”. We simply type “$ gdb ./testing” in the command prompt and run this particular program with the help of GDB commands like “r”, “s”, “n”, etc.

Programming Example 2

Here, we will try to do some operations on the data already removed by the free() function.

#include <stdio.h>
#include <stdlib.h>
#include "userdefined_malloc.h"

int main ()
{

    int *darray;
    int i,n;
    printf ("\n Enter size of array = ");
    scanf ("%d", &n);
    darray = (int *) malloc (n*sizeof(int));
    printf ("\n Before putting contents \n");
    for (i=0; i<n; i++)
    {

        printf ("%d \n", darray[i]);

    }
    printf ("\n Enter %d elements = \n",n);
    for (i= 0; i<n; i++)
    {

        scanf ("%d", &darray[i]);
    }
    printf ("\n The contents of the array are \n");
    for (i=0; i<n; i++)
    {

        printf ("%d \n", darray[i]);
    }
    darray = NULL;
    free (darray);
    free (darray);
    printf ("\n After freeing the memory \n");
    for (i=0; i<n;i++)
    {
        printf ("%d \n", darray [i]);

    }
   
   

    printf ("\n");     
}

 

Compile and Run the Program

 

$ make
$ ./testing

 

Run Using GDB

 

$ gdb ./testing

 

 

 

Explanation

In this particular programming example, we will create an array dynamically with the help of the malloc() function and put some values from the user with the help of for loop. Then, we print those values on the monitor. After, we will deallocate the memory by calling the free() function. This function deallocates all the memory created dynamically by the malloc() function.

After that, if we try to print those values again, considering all the values are present in the array, and run the program, we will see some error like “segmentation fault (core dumped)”. Because after freeing the memory, if we will again do some operations on those values, it is not possible practically as the free() function removes all the data.

Conclusion

In this article, we are trying to understand the jemalloc() function properly, starting from its download, installation, creating header file, and running a program. It is very useful using the jemalloc() function to make life easier to other functions, such as malloc(), calloc() function, realloc() function, and free() function. The jemalloc() function successfully executes these functions in the program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com