Stack and Heap memory [closed] - stack

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My question here is
What is stack and heap memory
Why we need both of these memories
what are the pros and cons of each

In a nutshell:
The stack - The memory the program uses to actually run the program. This contains local variables, call-back data (for example when you call a function, the stack stores the state and place you were in the code before you entered the new function), and some other little things of that nature. You usually don't control the stack directly, the variables and data are destroyed, created when you move in and out function scopes.
The heap - The "dynamic" memory of the program. Each time you create a new object or variable dynamically, it is stored on the heap. This memory is controlled by the programmer directly, you are supposed to take care of the creation AND deletion of the objects there.

In C / C++ language memory allocated onto the stack is automatically free when the allocating scope ends, memory on the heap has to be free with some policy ( free(), delete ... or some garbage collector ). Memory allocated on the heap is visible among different function scope. In the stack we can't allocate big chunk of memory so heap is also useful when tou need to allocate big space for data.

I am not sure in which context you are asking but i can answer from their use in memory allocation. Both these data structures are required my platforms like .NET for Garbage collection. Remember all value types are stored on stack and all reference type on heap. This help runtime environment to create an object graph and keep track of what all objects are not in use and can be considered for garbage collection.

Related

MIPS/Pipeline regarding unique Data & Instruction memory [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How does having unique Data & Instructions memory affects us to the standard 5-stage Pipeline?What about with & without Forwarding?
What's the advantage of having a different memory for each?
Regardless of having Forwarding, if you only have one port to access memory (e.g. unique Data & Instruction memory bus) and to simplify let's say there is no cache in the system (so every memory access needs to use the memory unit) then every instruction that needs the MEM stage to use the memory bus will generate a structural hazard, as the CPU won't be able to perform the FETCH and MEM stages in parallel because they both need to access memory.
If instead you have two ports to access memory (e.g, one for Instructions and another for data), then the structural hazard noted above will be avoided as each memory-access stage will use its own bus+memory.

What is "Memory Management" in iOS? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have been given these 2 questions in my interview.
1. What is **MEMORY MANAGEMENT** in iOS.
2. What is reference counting?
Can any one explain this to me? I am new to iOS. Please help me out.
Thanks in advance!
Memory management is important to development of any type. Mobile devices in general have less memory to work with than full sized computers, and so it is even more critical that you manage memory wisely when working with them. This means ensuring that you do not hold on to memory any longer than you need to, and that you are careful about the amount of memory that you allocate.
Luckily in iOS it is no longer necessary to deal directly with reference counting because it is handled automatically by ARC (Automatic Reference Counting), but it is still good to know what it is. Whenever an object is allocated it gets a reference count of 1. That reference count can be increased by calling retain, or decreased by calling release. When the reference count hits 0, the object is deallocated.
Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well-written program uses as little memory as possible. In Objective-C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed.
Reference counting

CUDA : why are we using so many kinds of memories? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've learned the CUDA programming and I went into some problem. The major one is in CUDA "why do we use so many kinds of memories(Global, local, shared, constant, texture, caches,registers)?" unlike in CPU where we have only three main memory(Ram, caches, hd etc).
The main reasons for having multiple kinds of memory are explained in this article: Wikipedia: Memory Hierarchy
To summarize it, it a very simplified form:
It is usually the case that the larger the memory is, the slower it is
Memory can be read and written faster when it is "closer" to the processor.
As mentioned in the comment: On the CPU, you also have several layers of memory: The main memory, and several levels of caches. These caches are much smaller than main memory, but much faster. These caches are managed by the hardware, so as a software developer, you do not directly notice that these caches exist at all. All the data seems to be in the main memory.
On the GPU, you have to manage this memory manually (althogh in newer CUDA versions, you can also declare the shared memory as "cache", and let CUDA take care of the data management).
For example, reading some data from the shared memory in CUDA may be done within a few NANOseconds. Reading data from global memory may take a few MICROseconds. One of the keys to high performance in CUDA is thus data locality: You should try to keep the data that you are working on in local or shared memory, and avoid reading/writing data in global memory.
(P.S.: The "Close" votes that mark this question as "Primarily Opinion Based" are somewhat ridiculous. The question may show a lack of own research, but is a reasonable question that can clearly be answered here)

How is heap memory represented? [duplicate]

This question already has answers here:
What data structure is used to implement the dynamic memory allocation heap?
(2 answers)
Closed 8 years ago.
As per the question What and where are the stack and heap?, I more or less have an understanding of stack memory vs heap memory. Another question comes to mind, however:
Most sources I've read describe stack memory as being represented by an actual stack data structure in the memory. Is it the same with heap memory? Is heap memory represented/abstracted by a min/max heap data structure? If not, then what data structure is used to implement heap memory?
It all depends on the programming language you are using, but stack and heap are traditionally implemented as a stack and tree. The stack may be used to store function calls and scopes because you can return to the calling function by popping. But this and the actual implementation can be different per language so there isn't a general answer to this question.

Tools for checking memory fragmentation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have recently read topics about memory fragmentation:
How to solve Memory Fragmentation and What is memory fragmentation?
I want to see some memory allocation map such as author in these article http://pavlovdotnet.wordpress.com/2007/11/10/memory-fragmentation/
Could you recomend some tools to get memory allocation map like that, so I could see if the memory is fragmented and what is the biggest free space available.
I'm on Windows so I would prefer tools working on this system.
Here is a tool that visualizes GC memory and heap usage, also the source code is provided. Another similar app is linked in the comments there as well.
If you need to be able to profile memory usage for a .NET solution, you could check out ANTS Memory Profiler, it can run alongside a project in Visual Studio and keep tabs on how processes and objects are using memory.
There is indirect solution to the problem. I have developing server application for a few years. Initially we are doing the allocation on demand and as a result after a running for few weeks the performance of the server degraded. As a workaround we followed this approach -
Suppose you have user defined classes X,Y,Z, .. which you need to allocate from heap at runtime. Allocate n number of objects X at startup. Put all these objects in free pool list. On demand , take each object of x and provide it to your app. When in use, put it in busy pool list.
When app wants to release it, put it back to the free pool list. Follow this startegy for Y. Z etc.
Since you are allocating all the needed objects at startup and never releasing back to the OS memory manger until your program exits, you will not face the performance degradation caused by memory fragmentation.

Resources