3

I read in article http://www.ousob.com/ng/borcpp/ng916c4.php about obtaining unused brk memory by calling coreleft(). But looks like that doesn't work on Linux. And also the file alloc.h doesn't exist. My question is that is alloc.h function family standard for Linux? If it doesn't, any alternatives for function like coreleft on Linux? Thanks.

2
  • Why do you ask? Why do you care? Please edit your question to motivate it. Commented Aug 20, 2015 at 7:44
  • @BasileStarynkevitch I can't speak for the OP but if you're curious, I ended up here by googling about a function that showed up in a very old DOS program. I took a guess that the function was outdated, and that it wouldn't work properly in a virtual memory system, and from the looks of it, I was right. Commented Jun 24, 2020 at 21:44

1 Answer 1

4

You probably want to measure memory usage and this is very ambiguous on Linux. See this & that for more. You'll want to use /proc/ pseudo-file system, see proc(5). Remember that processes use virtual memory. See also http://www.linuxatemyram.com/

Asking how much memory is left (or could be allocated, e.g. by system calls like mmap(2)...) does not make any sense, because some other process might release memory resources which would become later available for your process. Hence there cannot be any reliable coreleft function -measuring available heap memory, e.g. for future calls to malloc- on Linux (or POSIX). Both mmap & malloc can fail (and you should always check that).

You can limit many resources (including virtual memory or address space) on a process, see setrlimit(2) and the bash shell ulimit builtin.

The sbrk syscall is nearly deprecated (and no more used) on Linux. You'll get fresh virtual memory segment using mmap (and you release it using munmap). The C standard library implements malloc(3) (from <stdlib.h>) by calling mmap appropriately; it usually tries to reuse previous free-d memory zones. On Linux with GNU glibc, see also malloc_info(3), mallinfo(3), malloc_stats(3). You might study the source code of free software standard C libraries like GNU glibc or musl-libc to understand more about them.

The <alloc.h> header is non-standard and does not exist on most Linux or POSIX systems.

If you need to reserve some address space without consuming memory (i.e. swap area) consider the MAP_NORESERVE option to mmap. See e.g. this.

In C on POSIX systems (like Linux), the usual habit is to use malloc (or related calloc) function, and to handle the out-of-memory case (which is unlikely to happen in practice, except on very loaded systems or when strong small limits have been set), perhaps by exiting with an error message:

 size_t siz = some_size();
 void* p = malloc(siz);
 if (!p) { perror("malloc"); exit(EXIT_FAILURE); };

See also this answer (suggesting Boehm's GC).

I suggest you to read Advanced Linux Programming

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.