How to perform cache operations in C ++?

I want to run my C ++ program after clearing the cache. Before starting my program, I do not know what is in the cache. Is there any other way in C ++ on unbuntu through which I can clear my cache before running my program.

EDIT: The motive for clearing the cache ... is that every time I run my program, I don't want my existing data structures to be in the cache ... I mean, I need a cold cache. access is from the disk ... one way to achieve this is to restart the computer ... but given the number of experiments that I need to perform, this is not possible for me. So, someone can be nice to guide me ... how can I achieve this.

-4
source share
1 answer

You do not need to hide the cache from your user mode program (not the kernel). The OS (Linux, in the case of ubuntu) provides your application with a new virtual address space without any “remaining things” from other programs. Without making special OS system calls, your program cannot even access the memory used for other applications. So, in terms of cache, your application starts from scratch, as far as it goes. There are cacheflush () system calls (the syntax is different from the OS), but if you are not doing something unusual for regular applications in user mode, you can just forget that the cache exists; it’s just to speed up your program, and the OS controls it through the MMU processor, your application does not need to be managed.

You may also have heard of a “memory leak” (memory allocated for your application that your application forgets to free / delete that is “permanently lost” as soon as the application forgets about it). If you are writing a (potentially) long-term program, a memory leak is definitely a concern. But a memory leak is only a problem for an application that leaks it; in modern virtual memory environments, if application A loses memory, it does not affect application B. And when application A shuts down, the OS clears its virtual address space, and any leaked memory is restored by the system at that moment, and no longer consumes any system resources . In many cases, programmers specifically choose NOT to free / delete the memory allocation, knowing that the OS will automatically restore the entire memory when the application exits. There is nothing wrong with this strategy if the program does not continue to do this on a repetitive basis, exhausting its virtual address space.

+1
source

Source: https://habr.com/ru/post/893063/


All Articles