find size of heap allocated | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

find size of heap allocated

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-05-2011
rupeshkp728 rupeshkp728 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2014, 6:14 AM EDT
Posts: 172
Thanks: 120
Thanked 1 Time in 1 Post
find size of heap allocated

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?

Last edited by rupeshkp728; 05-05-2011 at 06:42 AM..
Sponsored Links
    #2  
Old 05-05-2011
kumaran_5555 kumaran_5555 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 27 August 2012, 1:46 AM EDT
Location: India
Posts: 342
Thanks: 9
Thanked 48 Times in 47 Posts
This will not take memory from heap. This would be allocated from stack because this is a local variable and you aren't using malloc() / any alloc() functions.

But if you want to see the size of heap, you can check using pmap <PID>

which will display the anonymous pages allocated for this process, which also includes the stack.

---------- Post updated at 03:49 PM ---------- Previous update was at 03:16 PM ----------

Quote:
Originally Posted by rupeshkp728 View Post
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?
This statement will print the size of one integer.

Last edited by kumaran_5555; 05-05-2011 at 06:17 AM..
Sponsored Links
    #3  
Old 05-05-2011
rupeshkp728 rupeshkp728 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2014, 6:14 AM EDT
Posts: 172
Thanks: 120
Thanked 1 Time in 1 Post
:(

hey kumaran in C++ new is used in place of malloc of C and so the memory will be allocated from heap.
    #4  
Old 05-05-2011
Loic Domaigne Loic Domaigne is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 26 December 2011, 4:26 PM EST
Location: Munich (Germany)
Posts: 244
Thanks: 0
Thanked 24 Times in 24 Posts
Quote:
Originally Posted by rupeshkp728 View Post
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?
The statement is not correct, you'll get the size of an int on your architecture.

windbg... hmm... that's doesn't sound really Un*x...

Remember that new[] is an operator provided by your compiler, we don't know what it does... It may for instance add a header for internal bookkeeping; or perhaps the underlying allocation mechanism on your system imposed this... Who knows?

You'll have to look at the interna of the new[] operator to find it out.

Cheers, Loc
Sponsored Links
    #5  
Old 05-05-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
Only the heap knows that. But unless you're allocating lots of really tiny things it's going to be pretty close to what you asked for.
Sponsored Links
    #6  
Old 05-05-2011
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 790 Posts
Quote:
Originally Posted by kumaran_5555 View Post
This will not take memory from heap. This would be allocated from stack because this is a local variable and you aren't using malloc() / any alloc() functions.
Quote:
Originally Posted by rupeshkp728 View Post
hey kumaran in C++ new is used in place of malloc of C and so the memory will be allocated from heap.
I know very little about C++. The following comment is not intended to address the original poster's issue. It's just a brief response to what seems to be a tendency to equate malloc'd memory with the heap.

malloc is not required to allocate from the heap. 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).

Where malloc'd memory resides is highly implementation dependent.

Regards,
Alister
The Following 2 Users Say Thank You to alister For This Useful Post:
fpmurphy (05-05-2011), Praveen_218 (05-06-2011)
Sponsored Links
    #7  
Old 05-06-2011
Praveen_218 Praveen_218 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 26 April 2014, 8:36 AM EDT
Location: Linux Machines @Mumbai & Bangalore
Posts: 165
Thanks: 7
Thanked 28 Times in 26 Posts
Quote:
This will not take memory from heap. This would be allocated from stack because this is a local variable and you aren't using malloc() / any alloc() functions.
How come????

The arrary is going to be on the heap of the process memory only.
However, it's the pointer variable 'a' is going to be over the stack frame of the function using that as it's local.

---------- Post updated at 01:16 PM ---------- Previous update was at 12:51 PM ----------

@aliter ,
Thanks for the information, that's definitely going to be a direction, I'll need to investigate a bit further for my own shake.

To sum up, do you mean to say, in other words, that it's the implementation of heap which is going to be different in different systems?

That's what I'm aware of and most logical thing to conclude of when two operating systems are developed altogether differently and separately.

Also its never the compiler which actually allocates these heap related object/variables rather the OS memory manager allocates them at the run time only.

Its perfectly okey to have different implementations of the actual resident memory area of a heap based variables. The running process (which owns these variables) just treat them at a memory location that's always upper bound and assumes such memory area altogether grows outwards (the stack grows in the direction opposite to the heap, which is inwards).

The whole thing could also be implemented as to simulate this behavior and that's what many virtual memory implementations do.

Not only that, the whole process memory area could altogether be implemented differently (over different platforms) but they give the same feeling to the running process by exposing similar interfaces and response from the operating platform the process is executing on.

Your information was important to me more because of the fact you have clubbed FreeBSD & Linux implementations together.

Might be Linux code implementing these behaviors find it's root in BSD repository tree only, but I'm just speculating here.

Last edited by Praveen_218; 05-06-2011 at 03:55 AM..
The Following User Says Thank You to Praveen_218 For This Useful Post:
rupeshkp728 (05-06-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking heap memory size for java app nthiruvenkatam UNIX for Dummies Questions & Answers 0 07-16-2010 02:45 AM
how to find size of memory allocated to a pointer? Wkdunreal HP-UX 0 10-20-2009 01:02 AM
monitor jvm heap size rein UNIX for Advanced & Expert Users 0 07-21-2005 08:29 AM
heap size for JVM! i2admin Filesystems, Disks and Memory 1 05-09-2002 09:09 AM
heap size! i2admin Filesystems, Disks and Memory 2 05-08-2002 10:20 AM



All times are GMT -4. The time now is 08:10 PM.