find size of heap allocated


 
Thread Tools Search this Thread
Top Forums Programming find size of heap allocated
# 8  
Old 05-06-2011
Bug

Quote:
Originally Posted by rupeshkp728
I want to find the size of the total memory allocated on the heap for the following statement:
int* a = new int[1000];How can I use the sizeof operator for this?
I used:
printf("\t===> %d\n",sizeof(*a));

Is this statement correct?

I have asked the question because when I checked the memory of heap allocated in windbg it shows me the size as fc4 i.e. 4036 which is more then 4000 and not as desired. Any hint as to what may be the cause?
Hi ,

You can use sizeof() operator to find the size of any data structure by providing the base address of the structure. the implementation of memory allocation are different on different architectures and they may allocate slightly more space than you requested for in order to have alignment of the memory allocated which can increase performance benefits.

Thanks and Regards,
Gaurav.
# 9  
Old 05-06-2011
Quote:
Originally Posted by alister
malloc is not required to allocate from the heap.
heap is just a moniker given to the dynamic part of the data segment...the part from where memory is allocated at runtime.
Quote:
Originally Posted by alister
In fact, quite a few implementations may not do so. Some use mmap exclusively (e.g. OpenBSD). Some may use sbrk for some allocations and mmap for others (e.g. Linux (glibc), FreeBSD).
Whether mmap or sbrk is used for mem allocation...the point is that it is always allocated from the dynamic portion of the data segment aka the heap.
Quote:
Originally Posted by alister
Where malloc'd memory resides is highly implementation dependent.
Not exactly because of the the above.
This User Gave Thanks to shamrock For This Post:
# 10  
Old 05-06-2011
Quote:
Originally Posted by shamrock
Whether mmap or sbrk is used for mem allocation...the point is that it is always allocated from the dynamic portion of the data segment aka the heap.
Incorrect. The "dynamic portion of the data segment" is delimited by the break. The break is manipulated with the brk() and sbrk() system calls. As its name implies, the segment is contiguous. mmap(), however, is allowed to map/allocate memory at random locations in non-contiguous segments beyond the break. Such behavior has nothing to do with the heap.

Perhaps your mmap implementation does not do so, but there's nothing to forbid it.

Alternatively, you have a very loose definition of "data segment". Smilie

Regards,
Alister
# 11  
Old 05-06-2011
Quote:
Originally Posted by alister
Incorrect. The "dynamic portion of the data segment" is delimited by the break. The break is manipulated with the brk() and sbrk() system calls.
Yes you are right the heap is capped by the break pointer...and mmap allocates memory in the hole between break and the upper bound of the stack segment.
Quote:
Originally Posted by alister
As its name implies, the segment is contiguous. mmap(), however, is allowed to map/allocate memory at random locations in non-contiguous segments beyond the break. Such behavior has nothing to do with the heap.
All segments are contiguous virtually speaking but none are contiguous physically.
Quote:
Originally Posted by alister
Perhaps your mmap implementation does not do so, but there's nothing to forbid it.
mmap is only used in openbsd for dynamic storage allocation and since my system isnt openbsd it still uses malloc.
Quote:
Originally Posted by alister
Alternatively, you have a very loose definition of "data segment". Smilie
That might be true as anything between the text and stack segment i'm calling collectively the data segment Smilie
# 12  
Old 05-06-2011
I believe that the malloc statements in my original post in this thread are 100% correct. At the risk of appearing to be trying to win an internet argument, I am dissecting your responses only in the hope that the thread contains useful and accurate information for those who may read it in the future.

Quote:
Originally Posted by shamrock
All segments are contiguous virtually speaking but none are contiguous physically.
Well, for the purposes of this discussion, physical memory is irrelevant; it's all about process virtual memory maps. Still, the statement isn't quite accurate. It implies that each segment is guaranteed to be fragmented in physical memory. It very well could be, but it's not a certainty.

Quote:
Originally Posted by shamrock
mmap is only used in openbsd for dynamic storage allocation and since my system isnt openbsd it still uses malloc.
OpenBSD uses malloc(), as do all UNIX/POSIX systems (to my knowledge).

You seem to be confusing interfaces that operate at different levels. mmap() is a lower level system call whose implementation is part of the kernel. malloc() is a higher level function whose implementation is part of the userland standard C library. malloc() does not directly allocate any memory; it must invoke a system call to do the job. The question is, which system call is used. Some malloc() implementations use brk()/sbrk(), some use mmap(), some use both.

If you're using Linux, you're almost certainly using glibc, and glibc's malloc implementation uses sbrk() for small allocations and mmap() for large ones.

FreeBSD tries to use mmap() for all allocations but if mmap() fails it will retry with sbrk().

OpenBSD's malloc() uses mmap() exclusively.

I believe OSX's malloc() uses mmap(), but I am not sure.

I could be mistaken, but I suspect that if there are any malloc() implementations still in widespread use which depend exclusively on brk()/sbrk(), they belong to the more conservative, proprietary Unices out there.

Quote:
Originally Posted by shamrock
... anything between the text and stack segment i'm calling collectively the data segment Smilie
You are of course free to choose your nomenclature as you wish, but in this instance your usage is not in agreement with standard practice.

Being a rebel only makes life harder. Submit. Be one with the collective. Resistance is futile. Smilie

Regards,
Alister
# 13  
Old 05-06-2011
Quote:
Originally Posted by alister
Incorrect. The "dynamic portion of the data segment" is delimited by the break. The break is manipulated with the brk() and sbrk() system calls. As its name implies, the segment is contiguous. mmap(), however, is allowed to map/allocate memory at random locations in non-contiguous segments beyond the break. Such behavior has nothing to do with the heap.

Perhaps your mmap implementation does not do so, but there's nothing to forbid it.

Alternatively, you have a very loose definition of "data segment". Smilie

Regards,
Alister
man mapmalloc

There are a lot of ways to make a heap.

Sun once even released a library that when preloaded would put your heap into shared memory. But it may have only been for SPARC, and it was definitely only for 32-bit processes. It also failed miserably for multithreaded processes, IIRC.

So I wrote a better one for a client who wanted fast restarts for a process that used about a few-hundred GB heap. We'd create and zero-fill a single huge ISM shared memory segment upon system boot, when it would be the fastest since memory wasn't fragmented yet. Then the process would start up in about 10 seconds, instead of the 10+ minutes or longer it would take to create and zero-fill the giant heap.

And yes, you have to zero-fill such a heap upon creation, or whenever your application code that by requirement has to be really, really fast may very well hang up waiting for the OS to actually create the VM page the process merely reserved with its call to brk()/sbrk(). Like trying to do a few tens of GB of IO from a SAN at a few GB/sec into a newly-created chunk of memory that hasn't actually been allocated yet... That can generate some nasty problems since the data is coming in faster than the kernel's VM code can create application pages to put them into.
# 14  
Old 05-06-2011
Or if your system supports it, MAP_POPULATE.

hugepages may also be helpful here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

/tmp size is less whereas size allocated to swap is more

Hi, the /tmp size is less whereas the size allocated to swap is quite big. how to increase the size of /tmp - #: swap -l swapfile dev swaplo blocks free /dev/md/dsk/d20 85,20 8 273096 273096 #: swap -s total: 46875128k bytes allocated + 2347188k reserved =... (2 Replies)
Discussion started by: psb74
2 Replies

2. UNIX for Dummies Questions & Answers

How to find the disk space allocated.

Hello, I need to find the total allocated disk space for the home directory. How can i find that in unix?(in GB). Thanks. (4 Replies)
Discussion started by: kailash19
4 Replies

3. UNIX for Dummies Questions & Answers

Checking heap memory size for java app

Hi I have one Java application installed in my Solaris system. Is there a way to find out the heap memory allocated size/used size/free size for the particular Java process? If anyone knows the command, please let me know. Even I appreciate if I have any scripts to find out the same. ... (0 Replies)
Discussion started by: nthiruvenkatam
0 Replies

4. HP-UX

how to find size of memory allocated to a pointer?

Hi, Am new to HP UX , is there a way to find out the size of memory allocated to a pointer on hp ux? For example we can use the _msize() on windows to find the size of memory allocated to a pointer . #include <stdio.h> #include <malloc.h> void main() { void *buffer; ... (0 Replies)
Discussion started by: Wkdunreal
0 Replies

5. Shell Programming and Scripting

Finding Heap size Command--Urgent

Hi Friends, i need a unix command to check the heap space availability on unix system as I am getting following error: Error occurred during initialization of VM Could not reserve enough space for object heap Error occurred during initialization of VM Could not reserve enough space for... (2 Replies)
Discussion started by: Anji
2 Replies

6. Shell Programming and Scripting

script to gather weblogic jvm heap size stats

Hello, has anyone written something that will monitor/gather weblogic heap info ? I need to gather size, high/low stats to a file that I can upload to a speadsheet thanks for your help! (2 Replies)
Discussion started by: galenw
2 Replies

7. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

8. UNIX for Advanced & Expert Users

monitor jvm heap size

I'm running websphere 4.5 on AIX 5 with java 1.3 and would like to find out the following: How much memory is allocated to each JVM, and how much of the allocated heap size is actually being used by a specific JVM? (0 Replies)
Discussion started by: rein
0 Replies

9. Filesystems, Disks and Memory

heap size for JVM!

Hi all, Thanks 'thehoghunter' and 'hugo' for the comments! I've to increase the size of the heap size for AIX 4.3.3. Now what's the command that I've and also is it something similar to growing the file system in Solaris (growfs) (1 Reply)
Discussion started by: i2admin
1 Replies

10. Filesystems, Disks and Memory

heap size!

I'm a new guy to this field and I'm learning a lot about UNIX! Can any explan to me what exactly does 'heap size' mean and how can i increase the size for AIX 4.3.3? (2 Replies)
Discussion started by: i2admin
2 Replies
Login or Register to Ask a Question