Hi,
I wanted to know whether malloc/new returns memory blocks from Cache or RAM.
Thanks in advance.
Hi,
I wanted to know whether malloc/new returns memory blocks from Cache or RAM.
Thanks in advance.
You are abstracted of all that when living as a process in the OS, you only get memory.
You shouldn't worry ever about that, the OS will manage all that for you and the memory unit will move things from one to another. But you still see a single virtual memory layout.
From virtual memory. OS will take care of bringing the required pages into the RAM whenever the process requires it.
As already said you can't know. The cache/RAM/hard disk is abstracted as virtual memory. But I think if you can measure the access time you may get an idea whether the RAM or cache is being accessed. But after the first access to RAM the memory block will be copied to the cache and subsequent accesses will be served from the cache.
It very much depends. At the start of your program, the memory that the OS gives you will probably not be paged in (at least, not on Linux). However, if you free some stuff, then get a new allocation, the memory could possibly be in the cache.
In there is a constructor which touches the memory, then it will certainly be in the cache after it's constructed.
If you're programming in Java, then you'll likely have a really cool memory allocator, and much more likely to be given memory thats in the cache.
(When I say cache, I mean L2. L1 is unlikely, but you might get lucky, esp. for small programs).
You cannot address the processor cache directly, the processor manages it (almost) transparently... At most you can invalidate or prefetch a cache line; but you access memory addresses (usually virtual if you're not in real mode), and the processor will be feed always data and instructions from its internal memory (if the data it's not already present, then it needs to be fetched).
Read this article for further info: http://en.wikipedia.org/wiki/Memory%5Fhierarchy
malloc
and operator new
will give you a chunk of address space.
The operating system will back this chunk of address space with some physical storage. The storage could be system memory or a chunk of a page file and the actual storage location can be moved between between the various physical storage devices and this is handled transparently from the application point of view. In addition the CPU and memory controller (on board or otherwise) may cache system memory but this is usually (largely) transparent to the operating system.
At first, the memory allocated for application is a virtual memory, whose address is located in the virtual space. Secondly, such as L1 and L2 cache will not be allocated for you, which is managed by system. In fact, if cache are allocated for you ,it's hard for the system to dispatch tasks.