views:

462

answers:

7

Hi,

I wanted to know whether malloc/new returns memory blocks from Cache or RAM.

Thanks in advance.

+12  A: 

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.

Arkaitz Jimenez
That being said, if you need precise control over where your memory is allocated and what the OS will do with it (such as flagging it as “don't ever page out”) then you need to use the native API of the OS.
Joey
Well, memory paging involves harddisk<->RAM, I don't think you can signal an OS to keep something in cache always and not in memory. You can signal for not caching it though.
Arkaitz Jimenez
@Arkaitz,"You can signal for not caching it though" : Can you let me know how to do so. would be glad if you could let me know :).
mahesh
Usually there are various special cases which affect how the OS deals with pages in a process' virtual memory space. This includes things like memory-mapped files and at least on Windows you can also specify that specific pages are never paged out.
Joey
@mahesh - You have 2 options (on x86, I can't help in regards to other platforms, sadly) that I know of in regards to this. First, you can completely turn caching off, look up details on the x86 control registers on how to do this. Secondly, you can use the "non-temporal" memory instructions in the SSE instruction set extensions, these are instructions that bypass the cache-line fetch of a normal memory operation. You can look up a reference on the SSE instruction set for the non-temporal memory instructions.
Falaina
@Falaina : Thank you, liked your answer :)
mahesh
In C you have the keyword "register". "Register" is used when you want to hint to a compiler that a variable should be in a hardware register for fast access.
luizleroy
@luizleroy, but that's just a hint, most compilers will probably ignore it, as they do their own guesses about register variables.
Arkaitz Jimenez
+3  A: 

From virtual memory. OS will take care of bringing the required pages into the RAM whenever the process requires it.

Naveen
A: 

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.

grigy
A: 

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).

Paul Biggar
A: 

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

fortran
+1  A: 

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.

Charles Bailey
A: 

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.

Cook Schelling