Priyanka Mishra
Priyanka Mishra

Reputation: 10728

Does malloc/new return memory blocks from Cache or RAM?

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

Thanks in advance.

Upvotes: 4

Views: 4703

Answers (7)

Yantao Xie
Yantao Xie

Reputation: 12906

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.

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 792497

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.

Upvotes: 2

fortran
fortran

Reputation: 76077

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_hierarchy

Upvotes: 0

Paul Biggar
Paul Biggar

Reputation: 28769

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

Upvotes: 0

grigy
grigy

Reputation: 6836

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.

Upvotes: 0

Naveen
Naveen

Reputation: 73473

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

Upvotes: 7

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

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.

Upvotes: 17

Related Questions