如何在C ++中执行缓存操作? [英] How to perform cache operations in C++?

查看:153
本文介绍了如何在C ++中执行缓存操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在刷新缓存后运行我的C ++程序,在运行我的程序之前,我不知道缓存中有什么。在unbuntu的C ++中有一些其他的方式,我可以在运行我的程序之前刷新我的缓存。

I want to run my C++ program after flushing the cache, Before running my program I do not know what is there in the cache. Is there some other manner in C++ on unbuntu via which I may flush my cache before running my program.

编辑:刷新缓存的动机是...每个时间我运行我的程序我不希望我的现有数据结构在缓存中...我的意思是我想要一个冷缓存。其中所有的访问都是从磁盘进行的...实现这一点的一种方法是重新启动计算机...但考虑到我必须运行的实验次数,这对我来说是不可行的。

The motive for flushing the cache is...that each time I run my program I do not want my present data structures to be there in the cache...I mean I want a cold cache..whereby all the accesses are made from the disk...one way of achieving this is to restart the computer...but considering the number of experiments that I have to run, this is not feasible for me. So, can anyone be kind enough to guide me...as to how I can achieve this.

推荐答案

你没有必要以从用户模式(非内核模式)程序刷新高速缓存。操作系统(Linux,在ubuntu的情况下)为您的应用程序提供了一个新的虚拟地址空间,没有其他程序的剩下的东西。没有执行特殊的OS系统调用,你的程序甚至不能得到用于其他应用程序的内存。所以从缓存的角度来看,你的应用程序从一个干净的板块开始,就其涉及。有cacheflush()系统调用(语法因操作系统而异),但是除非你对典型的用户模式应用程序做一些不寻常的事情,你可以忘记缓存甚至存在;它只是在那里加快你的程序,而操作系统通过CPU的MMU管理它,你的应用程序不需要管理它。

You have no need to flush the cache from your user-mode (non-kernel-mode) program. The OS (Linux, in the case of ubuntu) provides your application with a fresh virtual address space, with no "leftover stuff" from other programs. Without executing special OS system calls, your program can't even get to memory that's used for other applications. So from a cache perspective, your application start from a clean slate, as far as it's concerned. There are cacheflush() system calls (syntax differs by OS), but unless you're doing something out-of-the-ordinary for typical user-mode applications, you can just forget that the cache even exists; it's just there to speed up your program, and the OS manages it via the CPU's MMU, your app does not need to manage it.

你可能也听说过内存泄漏(内存分配给您的应用程序,您的应用程序忘记释放/删除,这是永远丢失一旦你的应用程序忘记它)。如果你正在编写一个(可能)长期运行的程序,泄漏的内存肯定是一个问题。但是泄漏的内存只是泄漏它的应用程序的一个问题;在现代虚拟内存环境中,如果应用程序A泄漏内存,它不会影响应用程序B.当应用程序A退出时,操作系统清除其虚拟地址空间,并且任何泄漏的内存在系统回收的时候,更长时间消耗任何系统资源。在许多情况下,程序员特别选择不释放/删除内存分配,知道操作系统将在应用程序退出时自动回收整个内存量。这个策略没有什么问题,只要程序不会在重复的基础上继续这样做,耗尽其虚拟地址空间。

You may have also heard about "memory leaks" (memory allocated to your application that your application forgets to free/delete, which is "lost forever" once your application forgets about it). If you're writing a (potentially) long-running program, leaked memory is definitely a concern. But leaked memory is only an issue for the application that leaks it; in modern virtual-memory environments, if application A leaks memory, it doesn't affect application B. And when application A exits, the OS clears out its virtual address space, and any leaked memory is at that point reclaimed by the system and no longer consumes any system resources whatsoever. In many cases, programmers specifically choose to NOT free/delete a memory allocation, knowing that the OS will automatically reclaim the entire amount of the memory when the application exits. There's nothing wrong with that strategy, as long as the program doesn't keep doing that on a repeating basis, exhausting its virtual address space.

这篇关于如何在C ++中执行缓存操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆