Showing posts with label vmalloc. Show all posts
Showing posts with label vmalloc. Show all posts

Thursday, October 9, 2014

What is the difference between malloc and kmalloc?

1. Kmalloc returns physically contiguous memory whereas malloc returns only virtual contiguous memory.

2. Memory allocated by Kmalloc can't be swapped as it is reserved and locked.

malloc is used in user space.
kmalloc is used in kernel space, vmalloc can also be used here.

kmalloc and vmalloc are allowed in kernel space whereas malloc is allowed in user space.

Sunday, February 2, 2014

STACK AND HEAP

I felt that these two terms are used a lot while discussing linux so a quick look at these two topics would be good.

The stacks and heaps are the part of virtual address space that consists of code;data;heap(growing upwards) and stack portion(growing downwards).( from bottom to top).

STACK:

  • This is the memory area used by our thread of execution.
  • When a function is called the local variables and return addresses are stored here.
  • The space becomes available when the function  exits.
  • The user doesn't need to take care of the memory allocation and deallocation( happens automatically).

HEAP:

  • It is the memory set aside for dynamic allocation.
  • Generally, an application is given a separate heap memory and it allocates the memory from the given heap on need basis.
  • WE can allocate and free heap anytime.
  • User must take care of deallocation of memory when he doesn't need it.
  • To get heap memory we use malloc and calloc if we are in user space and if we are in kernel space we use kmalloc, vmalloc.
  • We must free the memory allocated by using free, kfree and vfree respectively.