Listing 1. Setting the Trim Threshold with mallopt()

#include <stdio.h>
#include <malloc.h>

int main(int argc, char **argv) {

  int thr;
  char *p1;

  if (argc != 2) {
	printf("Usage: Listing_1 <TRIM threshhold "
		   "[KB]>\n");
	exit(0);
  }

  thr = atoi(argv[1])*1024;
  if (!mallopt(M_TRIM_THRESHOLD, thr)) {
	printf("mallopt() failed\n");
  }

  printf("Allocating 100k, trim threshhold is "
		 "set to %d bytes\n", thr);
  p1 = malloc(100000);
  malloc_stats();

  printf("\nNow freeing 100k\n");
  free(p1);
  malloc_stats();
}
