简体   繁体   English

增加正在运行的进程的堆大小

[英]Increase heap size of a running process

I'm new to linux programming and i want to know is it possible to increase the heap size of a running process.我是 linux 编程的新手,我想知道是否可以增加正在运行的进程的堆大小。 If it is possible, please help me how to do it right.如果可能,请帮助我如何正确地做。 Thanks anyone for helping.感谢任何人的帮助。

Heap is just memory.堆只是内存。 There is nothing special about it.没有什么特别之处。 Any memory can become heap.任何内存都可以变成堆。 Diagrams showing a heap area are pedagogical, rather than real.显示堆区域的图表是教学性的,而不是真实的。

"Heap" is is "Heap" only because the memory is allocated by a heap manager. “堆”之所以是“堆”,只是因为内存是由堆管理器分配的。 While most programs only have on heap manager, it is possible to have multiple heap managers.虽然大多数程序只有堆管理器,但可以有多个堆管理器。

Thus heap size is controlled by a heap manager.因此堆大小由堆管理器控制。 Most simple heap managers give the user no control over the heap size.大多数简单的堆管理器让用户无法控制堆的大小。 The heap manager allocates more memory when it needs memory to respond to allocation calls.当堆管理器需要内存来响应分配调用时,它会分配更多内存。

Some heap managers give the user function calls to allow him to allocate an expand the heap size.一些堆管理器给用户函数调用以允许他分配一个扩展堆大小。

Just use a function like malloc() or calloc() to allocate memory dynamically.只需使用malloc()calloc()类的函数来动态分配内存。 To deallocate the memory and return it to the heap, use free() .要释放内存并将其返回到堆中,请使用free() These functions will manage the size of the heap by expanding or shrinking it as needed.这些函数将根据需要通过扩展或收缩来管理堆的大小。

Example:例子:

Everything in heap is anonymous.堆中的一切都是匿名的。 You can't access the memory directly.你不能直接访问内存。 Every access is indirect.每次访问都是间接的。 So store the address of allocated memory returned by malloc() in a pointer.因此,将malloc()返回的已分配内存地址存储在一个指针中。

int *ptr = malloc(sizeof(int));

We can use *ptr to access the memory's contents.我们可以使用*ptr来访问内存的内容。

*ptr = 3;
printf("%d", *ptr);

Once you are done using the memory.一旦你完成使用内存。 You deallocate it with你解除分配它

free(ptr);

According to Peter van der Linden's book on C programming,根据 Peter van der Linden 关于 C 编程的书,

The end of the heap is marked by a pointer known as the "break".堆的末尾由一个称为“break”的指针标记。 When the heap manager needs more memory, it can push the break further away using the system calls brk and sbrk .当堆管理器需要更多内存时,它可以使用系统调用brksbrk将中断推得更远。

You typically don't call brk yourself explicitly, but if you malloc enough memory, brk will eventually be called for you.您通常不会自己显式调用brk ,但是如果您malloc足够的内存,最终会为您调用brk

Your program may not call both malloc() and brk() .您的程序可能不会同时调用malloc()brk() If you use malloc , malloc expects to have sole control over when brk and sbrk are called.如果您使用mallocmalloc期望在调用brksbrk时拥有唯一的控制权。

The limit on the maximum size of heap is determined by the size of virtual memory of the system.堆最大大小的限制由系统虚拟内存的大小决定。

Here's a crude reproduction of the image:这是图像的粗略复制:

记忆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM