Memory Allocation Query


 
Thread Tools Search this Thread
Top Forums Programming Memory Allocation Query
# 1  
Old 10-24-2011
Memory Allocation Query

When we dynamically allocate the memory say 100 integers say

int *x = new int(1000);

then does entire chunk of memory gets allocated at once after the completion of the statement?

I mean will the the concept of page fault come into picture over here?

Last edited by rupeshkp728; 10-24-2011 at 12:24 PM.. Reason: Improve question
# 2  
Old 10-24-2011
Although 100*sizeof(int) = 400 bytes (on a 32 bit system) is not a big chunk of memory; If you do through malloc();

Code:
int *x = (int *) malloc(100 * sizeof(int));

Modern day OS memory manager just looks at its allocation table before it returns you an address of logical contigous memory and do an appropriate marking to reflect the allocation to the process in its table. That's your allocation, almost immediately.

However, when the area is greater that a page size usually 4k on a 32-bit system and 8k over 64-bit systems; this allocation to actual physical memory is delayed till you actually write data on them. This is because the COW feature (Copy On Write). However the malloc() would return almost immediate with just a virtual memory address only.


This is how such allocations are tracked:

struct task_struct has a field like struct mm_struct *mm, *active_mm;

Code:
 //which on expanding under file include/linux/mm_types.h, looks like:
struct mm_struct {
        struct vm_area_struct * mmap;           /* list of VMAs */
        struct rb_root mm_rb;
        struct vm_area_struct * mmap_cache;     /* last find_vma result */
        unsigned long (*get_unmapped_area) (struct file *filp,
                                unsigned long addr, unsigned long len,
                                unsigned long pgoff, unsigned long flags);
        void (*unmap_area) (struct mm_struct *mm, unsigned long addr);
        unsigned long mmap_base;                /* base of mmap area */
        unsigned long task_size;                /* size of task vm space */
        unsigned long cached_hole_size;         /* if non-zero, the largest hole below free_area_cache */
        unsigned long free_area_cache;          /* first hole of size cached_hole_size or larger */
        pgd_t * pgd;
        atomic_t mm_users;                      /* How many users with user space? */
        atomic_t mm_count;                      /* How many references to "struct mm_struct" (users count as 1) */
        int map_count;                          /* number of VMAs */
        struct rw_semaphore mmap_sem;
        spinlock_t page_table_lock;             /* Protects page tables and some counters */

        struct list_head mmlist;                /* List of maybe swapped mm's.  These are globally strung
                                                 * together off init_mm.mmlist, and are protected
                                                 * by mmlist_lock
                                                 */

        /* Special counters, in some configurations protected by the
         * page_table_lock, in other configurations by being atomic.
         */
        mm_counter_t _file_rss;
        mm_counter_t _anon_rss;

        unsigned long hiwater_rss;      /* High-watermark of RSS usage */
        unsigned long hiwater_vm;       /* High-water virtual memory usage */

        unsigned long total_vm, locked_vm, shared_vm, exec_vm;
...
...
}


The field struct vm_area_struct * mmap; /* list of VMAs */
keeps track of the process memory area all the times.
This User Gave Thanks to Praveen_218 For This Post:
# 3  
Old 10-24-2011
Quote:
Originally Posted by rupeshkp728
When we dynamically allocate the memory say 100 integers say

int *x = new int(1000);

then does entire chunk of memory gets allocated at once after the completion of the statement?
That's 1000 integers, not 100.

But yes. You get the entire chunk at once.

If you've freed memory before, it's possible you'll get a recycled chunk instead of a brand-new one.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-24-2011
Well explained

Quote:
Originally Posted by Praveen_218
Although 100*sizeof(int) = 400 bytes (on a 32 bit system) is not a big chunk of memory; If you do through malloc();

Code:
int *x = (int *) malloc(100 * sizeof(int));

Modern day OS memory manager just looks at its allocation table before it returns you an address of logical contigous memory and do an appropriate marking to reflect the allocation to the process in its table. That's your allocation, almost immediately.

However, when the area is greater that a page size usually 4k on a 32-bit system and 8k over 64-bit systems; this allocation to actual physical memory is delayed till you actually write data on them. This is because the COW feature (Copy On Write). However the malloc() would return almost immediate with just a virtual memory address only.


This is how such allocations are tracked:

struct task_struct has a field like struct mm_struct *mm, *active_mm;

Code:
 //which on expanding under file include/linux/mm_types.h, looks like:
struct mm_struct {
        struct vm_area_struct * mmap;           /* list of VMAs */
        struct rb_root mm_rb;
        struct vm_area_struct * mmap_cache;     /* last find_vma result */
        unsigned long (*get_unmapped_area) (struct file *filp,
                                unsigned long addr, unsigned long len,
                                unsigned long pgoff, unsigned long flags);
        void (*unmap_area) (struct mm_struct *mm, unsigned long addr);
        unsigned long mmap_base;                /* base of mmap area */
        unsigned long task_size;                /* size of task vm space */
        unsigned long cached_hole_size;         /* if non-zero, the largest hole below free_area_cache */
        unsigned long free_area_cache;          /* first hole of size cached_hole_size or larger */
        pgd_t * pgd;
        atomic_t mm_users;                      /* How many users with user space? */
        atomic_t mm_count;                      /* How many references to "struct mm_struct" (users count as 1) */
        int map_count;                          /* number of VMAs */
        struct rw_semaphore mmap_sem;
        spinlock_t page_table_lock;             /* Protects page tables and some counters */

        struct list_head mmlist;                /* List of maybe swapped mm's.  These are globally strung
                                                 * together off init_mm.mmlist, and are protected
                                                 * by mmlist_lock
                                                 */

        /* Special counters, in some configurations protected by the
         * page_table_lock, in other configurations by being atomic.
         */
        mm_counter_t _file_rss;
        mm_counter_t _anon_rss;

        unsigned long hiwater_rss;      /* High-watermark of RSS usage */
        unsigned long hiwater_vm;       /* High-water virtual memory usage */

        unsigned long total_vm, locked_vm, shared_vm, exec_vm;
...
...
}


The field struct vm_area_struct * mmap; /* list of VMAs */
keeps track of the process memory area all the times.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

memory allocation for string in C

hi in the following code, how the memory is allocated for a1 which holds the values of a2 after cpy function call. #include <stdio.h> #include <string.h> void cpy(char* d, const char* s){ while(*d++=*s++); } main(){ char* a1; char* a2="done"; cpy(a1,a2); ... (3 Replies)
Discussion started by: mprakasheee
3 Replies

2. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

3. Programming

Dynamic Memory Allocation

Hello Guys I have a small confusion in the dynamic memory allocation concept. If we declare a pointer say a char pointer, we need to allocate adequate memory space. char* str = (char*)malloc(20*sizeof(char)); str = "This is a string"; But this will also work. char* str = "This... (2 Replies)
Discussion started by: tene
2 Replies

4. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

5. Programming

Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active. The client will be in active state always i.e, it should always search for an active server until it gets one.I... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

6. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

7. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

8. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 Replies

9. UNIX for Advanced & Expert Users

threads and memory allocation

Hello! First of all, forgive me for bad English. When I starts new thread (pthread_create), system allocates some memory for it (for example, for thread's stack). I wonder when does it deallocate this memory? The problem is that I have a program which sometimes creates new threads and sometimes... (3 Replies)
Discussion started by: prankster
3 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question