2

I am confused as to how Android's memory is structured and managed. I want to understand the way memory is divided and shared between applications.

From what I have understood so far, every application is allocated its own Dalvik heap of memory which has a strict upper limit. This is managed using a concurrent mark-and-sweep garbage collection mechanism.

1) My question is, are only 'heaps' used in applications, or does each application have a 'stack' as well? I read here that there is a stack as well as a heap for each process (and copying GC works on the heap). Where is this stack and what is it used for?

2) The link also says that CMS is used on the stack, which does not make sense to me, because doesn't going through the stack elements sequentially kill the point of LIFO? And wouldn't compaction be required in this case?

3) Also, when processes share memory (ashmem) is it implemented as a shared heap or a shared stack?

trincot
  • 317,000
  • 35
  • 244
  • 286
Kajal
  • 581
  • 11
  • 24

1 Answers1

3

I'm sorry I don't understand your 3rd question but I can answer the first and second:

1) Every android application has a Stack and a Heap for memory allocation,and Objects such as String are allocated on the Heap while Primitive variables such as int,char are allocated on the Stack.

2) CMS is a Garbage collecting algorithm that is used for automatically managing memory on the Heap.So it cannot be used for the Stack.

SamMao
  • 181
  • 12
  • Thanks for your answer. My third question is about shared memory. I wanted to know if two applications sharing memory are able to use any portion of the shared memory as required (like in a heap) or there are restrictions with applications pushing or popping variables and using them only in the particular order the variables were pushed in (like in a stack). – Kajal Nov 06 '15 at 08:43
  • My third question was more of an afterthought, anyway, I mainly wanted answers for the first two. Do post if you have an answer for it. But, I'll accept your answer. Thank you! – Kajal Nov 06 '15 at 08:46
  • 1
    I think the 3rd question is a much more complex topic than the others,and you can read sth about Android IPC,Binder,Linux kernel to know more about how ashmem works.I'm sorry I don't know much about ashmem either. – SamMao Nov 06 '15 at 09:00
  • my fourth question, what is difference between heap memory and main memory? – Kaustubh Mar 12 '17 at 15:31