Heap Size

R

Raman

Hi All,


Could any one tell me, how can I determine/Change size of heap on per-
process basis on Unix based systems.


Thanks.

Regards
Raman Chalotra
 
A

Army1987

Raman said:
Hi All,


Could any one tell me, how can I determine/Change size of heap on per-
process basis on Unix based systems.

If by "heap" you mean "memory available for dynamic allocation":

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t size = 1;
unsigned char *p = NULL;
do {
size *= 2;
p = realloc(p, size);
} while (p);
printf("Heap size is less than %lu but more than %lu bytes.\n",
(unsigned long)size, (unsigned long)size / 2);
return 0;
}

But to change it you (if you can) would need system-specific ways.
Try asking a newsgroup about Unix.
 
R

Richard Bos

Army1987 said:
"Raman" <[email protected]> ha scritto nel messaggio
If by "heap" you mean "memory available for dynamic allocation":

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t size = 1;
unsigned char *p = NULL;
do {
size *= 2;
p = realloc(p, size);
} while (p);
printf("Heap size is less than %lu but more than %lu bytes.\n",
(unsigned long)size, (unsigned long)size / 2);
return 0;
}

Do note that the message that is printed may, instead, be "This process
has been terminated by the sysadmin because of runaway memory abuse.",
and the second time you run it it may be "This account has been
temporarily deactivated.NO CARRIER".

IOW, please do not do this on a multi-user system.

Richard
 
A

Army1987

Richard Bos said:
Do note that the message that is printed may, instead, be "This process
has been terminated by the sysadmin because of runaway memory abuse.",
and the second time you run it it may be "This account has been
temporarily deactivated.NO CARRIER".

IOW, please do not do this on a multi-user system.

Very fine point. I had temporarily forgotten what happened the last
time I did something like while (1) { p = malloc(200); } on such a
system.
(Also, now I realized what happens if (SIZE_MAX + 1) / 2 bytes of
memory can successfully allocated, and if realloc(p, 0) does not
return a null pointer...)
 
B

Barry Schwarz

If by "heap" you mean "memory available for dynamic allocation":

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t size = 1;
unsigned char *p = NULL;
do {
size *= 2;
p = realloc(p, size);
} while (p);
printf("Heap size is less than %lu but more than %lu bytes.\n",
(unsigned long)size, (unsigned long)size / 2);
return 0;
}


This does not relate to the total amount available but to the largest
contiguous block available and even then it could report a number much
too low.


Remove del for email
 
D

Dale Henderson

Army> If by "heap" you mean "memory available for dynamic
Army> allocation":

Army> #include <stdio.h>
Army> #include <stdlib.h>
Army> int main(void)
Army> {
Army> size_t size = 1;
Army> unsigned char *p = NULL;
Army> do {
Army> size *= 2;
Army> p = realloc(p, size);
Army> } while (p);
Army> printf("Heap size is less than %lu but more than %lu
Army> bytes.\n",
Army> (unsigned long)size, (unsigned long)size / 2);
Army> return 0;
Army> }

Army> But to change it you (if you can) would need system-specific
Army> ways. Try asking a newsgroup about Unix.


I don't think this does what the OP wants since realloc can (and on my
system does) change the size of the heap when it needs to.

<OT>
The heap size can be changed on Unix systems, man sbrk.
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top