No, when you program starts up it uses virtual memory and will continue
to do so until it terminates (provided that you run the program on an OS
which uses virtual memory, which most non-embedded do).
If there is not enough physical memory free the OS will swap some out to
disk, but which memory (and from what program) is impossible to tell.
I suppose you could use mmap() or similar and tell the OS not to swap
the memory and then create your own allocator which uses this memory.
Then add functionality so when the memory is full you manually write it
to file, unmap the memory and map in new memory resident memory. The
problem is to trap the segmentation-fault which will occur when you try
to access any of the out-swapped memory and then map it and read it from
the file, but it might be possible.
Of course that would only apply for dynamic memory, your automatic
memory is still under the OS control.
I doubt it.
The real question is why anyone would ever want to do something like
that, virtual memory is one of the best thing invented for us
programmers, since we do not have to worry about this stuff.
Thank you, Erik!
I'm trying to implement a information retrieval algorithm,
Single-pass in-memory indexing.
It' used to construct inverted index from document collections.
SPIMI-INVERT(token_stream)
1 output_file = NEWFILE()
2 dictionary = NEWHASH()
3 while (free memory available) <--
4 do token ↠next(token_stream)
5 if term(token) /∈ dictionary
6 then postings_list = ADDTODICTIONARY(dictionary, term(token))
7 else postings_list = GETPOSTINGSLIST(dictionary, term(token))
8 if full(postings_list)
9 then postings_list = DOUBLEPOSTINGSLIST(dictionary, term(token))
10 ADDTOPOSTINGSLIST(postings_list, docID(token))
11 sorted_terms ↠SORTTERMS(dictionary)
12 WRITEBLOCKTODISK(sorted_terms, dictionary, output_file)
13 return output_file
What the algorithm do is:
1. Construct index in memory until the memory is used up.
2. Write the index to a file, release memory, then continue,
until all documents are precessed
3. Merge all the files
So if I just let the program use virtual memory,
and I have a huge document collection,
it is possible that the swap partition will be used up.
And the machine will become very slow
due to a lot of I/O operations, right?
If so, this algorithm will become useless.
That's why I think if there is no physical memory available,
I should take some actions.
I know this is a strange requirement,
and maybe this is not the right way.
I'll try your method. From your description,
I find this is NOT a easy task.