MAG
MAG

Reputation: 3075

Can i ensure vector elements are always in memory

I am working on a heavy weight application where the application is the only application runing on the system. On a recent performance run it was found that we were taking a lot of time in derefereing a very large vector of pointers . I found that we have large swapin/out as well giving me an indication that may be the vector element memory was released from ram. Can I ensure that my elements pointed to by pointers which are contained in my vector never go out of ram . Gcc 4.8 and Not c++11 . Red hat v6.5 . We cant use c++ 11 as per management dicission .

Upvotes: 0

Views: 284

Answers (1)

Nils Pipenbrinck
Nils Pipenbrinck

Reputation: 86353

You can't solve this problem using any C++ language features. What you need is support from the operation system.

Luckily Linux offers an API that lets you mark memory pages that should not get swapped out. You'll find these in mman.h: mlock man page

So if you want your vector to always be present in RAM you should:

  • Allocate a large enough chunk of memory.
  • Mark as unswappable using mlock
  • Write a custom allocator for your vector that takes memory from your unswappable memory block instead from the heap.

Keep in mind: The OS will not swap out memory for no reason. If part of your vector reside on the harddisk, something else you've accessed recently was more important. If you start experimenting with unswappable memory be ready for some performance surprises. The OS already tries to do it's best.

Upvotes: 3

Related Questions