ksm001
ksm001

Reputation: 4022

Why does my program produce correct output when my vector< vector< vector<int> > > is larger than the RAM?

I have implemented a system which takes as an input some data, and produces as a result a very big vector< vector< vector<int> > > which I then output to a file.

After calculating the result I decided to count the total amount of numbers inside this final 3d vector and it was: 1386502951

that means that the total amount of memory needed to store this vector is at least 1386502951*4 bytes = 5.16512599 GB

however my RAM memory is only 4 GB.

Can someone explain to me how this is possible?

Thank you in advance!

Upvotes: 3

Views: 124

Answers (2)

Captain Obvlious
Captain Obvlious

Reputation: 20063

Your application is able to allocate more memory than is physically installed on your computer because it supports virtual memory. Allocation, paging and releasing virtual memory is handled by the operating system to allow your application to run without having to worry about exhausting physical memory.

Keep in mind that releasing the allocated memory may not occur immediately when your application frees memory from it's heap. This has a tendency to make people think their application is at times using more memory that it should.

There are a good number of websites that discuss the topic of virtual and physical and any API's provided by the operating that allow you to have some control over it. A quick Google Dance should provide you with a significant number of links on the subject.

Upvotes: 4

Sellorio
Sellorio

Reputation: 1950

There could be a number of reason that this does not fail.

As stated in comments, the most likely is that windows is putting a large chunk of it in Virtual Memory (not RAM).

Another option (under some circumstances) is that the optimiser is cleaning up your list (maybe?)

The best way to see exactly what is happening is to open task manager and switch to the Performance tab and look at the RAM monitors it uses.

Upvotes: 2

Related Questions