Oren
Oren

Reputation: 2807

How to check if an object is in the CPU cache?

Is there a way in java to check if a specific object is in the CPU cache? Is there a way to test if reading/writing one of its fields will make a cache miss?

I wrote java programs in the past, but not complex ones, and now I have to do some academic research in java.

If this is not possible, is there a generic way to simulate this kind of thing? And is this possible in low-level programming, such as C?

Upvotes: 0

Views: 1174

Answers (2)

TeaOverflow
TeaOverflow

Reputation: 2578

As others already said: no.

And even if you could query the cache for specific data, theres no telling if the whole object is in the cache, because there is no such notion at such a low level.

Upvotes: 0

Itay Maman
Itay Maman

Reputation: 30733

TL;DR - No

In Java the JVM shields you from the CPU so this kind of low-level information is, by design, not exposed to the program.

Moreover, even if you go lower-level languages (e.g., C) and used some assembly-level hacks, I can hardly imagine where you could get meaningful data about cache status, for the following reason: the cache is dynamic - new data is continuously loaded into it and (older) data is continuously flushed from it. In each given moment you have several processes running on top of your CPU each one of them spawning at least one thread (probably more than that) - the combined activity of all these threads affects the cache and the data stored in it.

Thus, even if you could get a Y/N answer to the question is object X in cache then this answer will become obsolete the minute you get it.

Upvotes: 7

Related Questions