Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The max size malloc can allocate?

Tags:

c

linux-kernel

my os is arch linux 64 bit.

free -m result below

             total       used       free     shared    buffers     cached
Mem:         32172      31909        262          0        119      13054
-/+ buffers/cache:      18735      13436
Swap:          258        258          0
ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 257329
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 257329
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

the kenerl option overcommit_memroy is 0

But when i try to use malloc to allocate 2G bytes ,i fail for the reason:Cannot allocate memory?

like image 274
user1817385 Avatar asked Nov 12 '12 07:11

user1817385


People also ask

How much does malloc allocate?

Malloc(12) and malloc(16) allocate 16 bytes for the user, plus an extra 8 bytes for bookkeeping for a total of 24 bytes. Malloc(100) allocates 104 bytes for the user, plus an extra 8 bytes for bookkeeping.

How much memory can allocate?

The restriction concerning the size of statically allocated memory is 2 Gbytes both for 32-bit and 64-bit programs. Dynamic data. This is data for which memory is being allocated while executing the program. In C++, this allocation is usually performed by the malloc function or new operator.

Can malloc allocate more memory than RAM?

Malloc allocates memory more than RAM.

What is malloc sizeof?

The malloc line allocates a block of memory of the size specified -- in this case, sizeof(int) bytes (4 bytes). The sizeof command in C returns the size, in bytes, of any type. The code could just as easily have said malloc(4), since sizeof(int) equals 4 bytes on most machines.

How much more memory can be allocated in malloc?

In this case memory “allocated” can be a few bytes more (usually a word or 2) but also could be less depending on the size and implementation of malloc. Writing an efficient malloc is definitely an interesting piece of work and something you should try.

What is the size of malloc in Linux?

Malloc takes a size_t, which can be up to SIZE_MAX, which on a 32-bit system is 2 to the 32° power (4,294,967,296), and on a 64-bit system, 2 to the 64° (18,446,744,073,709,551,616) bytes. (4GiB resp. 16EiB ≈ 18EB) Virtual memory size available on the host, less that in use by other processes, plus overcommit space.

How does malloc work?

malloc does its own memory management, managing small memory blocks itself, but ultimately it uses the Win32 Heap functions to allocate memory. You can think of malloc as a "memory reseller". The windows memory subsystem comprises physical memory (RAM) and virtual memory (HD).

How to tell if malloc is I/2 bytes or i bytes?

You can, however, find it out by doing a hunt+halving search, caling malloc with ever larger arguments until it fails, and then homing in on the value it balks at. Something like By this point you know that i/2 bytes is ok, while i bytes is not ok.


1 Answers

It could be anything, actually. It may have worked before because the free memory was all contiguous, and now the free memory isn't, or it might be that your memory request fails the heuristics being used.

see this link here - http://linuxtoolkit.blogspot.com/2011/08/tweaking-linux-kernel-overcommit.html - for more information on turning the overcommit know.

Basically:

  • 0 = system heuristically determines whether an overcommit is allowed
  • 1 = always overcommit (even if out of address range, so rather stupid if you ask me)
  • 2 = never overcommit past certain constraints
like image 106
Lelanthran Avatar answered Oct 15 '22 10:10

Lelanthran