1

On a recent project im working on in C, im using a lot of memory since i will need to creat structs with a lot of data and have it on memory for a short ammount of time.

The thing is: i could use Trees/Linked List, which would allow me to have everything spread across the memory, but i used arrays. That got me thinking that it would need a lot of continous available space of memory, which most likely i wouldn't have. But the program didn't crash or have any problem at all. Is it using Virtual memory?

If so, would it affect the performace of the program? In this project the time it takes to load everything to memory and do some querys must be the lowest possible, so would that affect anything?

Pedro Lima
  • 387
  • 2
  • 14
  • 3
    My crystal ball tells me that your "a lot of" is not really that much. – Eugene Sh. Apr 11 '18 at 21:47
  • 3
    what do you mean by virtual memory? if you're using modern OS - then you always use only virtual memory, because they work in protected mode – Iłya Bursov Apr 11 '18 at 21:48
  • 1
    If it is a 32bit app, then running out of available address space is not that hard, and address space fragmentation (not having large enough continuous space) can be a problem in long running apps that allocate/deallocate a lot. Fragmentation is not that big a deal on startup. With a 64bit app that's much less likely to happen -- you'll definitely feel it when the physical RAM runs out and pages spill into a page file, and how far you can go depends on how the system is configured. – Dan Mašek Apr 11 '18 at 21:53
  • @EugeneSh. 4-12GB which to me seem like a lot to have on memory. Yes i know that is the OS who controls the memory, that's why i was wondering if there was any way possible so i could guarantee that everything is on memory and isnt moved to the disk, which would take a lot more time to read and use. – Pedro Lima Apr 11 '18 at 22:04
  • I would suggest using trees and linked list using dynamic memory. I wonder, how many entries do you need most of the time? – Ṃųỻịgǻňạcểơửṩ Apr 11 '18 at 22:06
  • 1
    Virtual memory is an area which the computer places less-recently used memory to the hard disk. Virtual memory involves swapping data back and forth between main memory and the *much slower* hard disk. Excessive usage of virtual memory is called **thrashing**, and will make the computer feel incredibly slow. – Ṃųỻịgǻňạcểơửṩ Apr 11 '18 at 22:11
  • Probably worth reading https://stackoverflow.com/questions/578137/can-i-tell-linux-not-to-swap-out-a-particular-processes-memory – Iłya Bursov Apr 11 '18 at 22:11
  • 1
    @Mulliganaceous what you've described is usually called swapping or paging, but not virtual memory, virtual memory is address mapping mechanism, it has almost nothing to do with hard drive usage, except that implementation in OS can decide to map some virtual address to file on disk – Iłya Bursov Apr 11 '18 at 22:13
  • 1
    Well if your machine does not have 12Gb of the physical RAM what choice does it have? Since the memory manager is OS specific so is the means by which you can lock memory to prevent swapping. For example in Windows : [VirtiualLock](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366895(v=vs.85).aspx), but second guessing the OS memory management is fraught with problems, and you can as easily screw-up performance as improve it. – Clifford Apr 11 '18 at 22:13
  • 1
    @IlyaBursov : The misuse of the terms swapping and virtual-memory is common, primarily because the MMU's virtual mapping is the enabling technology for disk-swapping. Read access to an unmapped address, causes a page fault, the fault handler maps a page containing the address, and loads it from disk, and for write access a page is written to the swapfile, and then ummapped. – Clifford Apr 11 '18 at 22:23
  • @Clifford you're right, my main objection was about `Virtual memory is an area which the computer places less-recently used memory to the hard disk` which is wrong, because **all** memory accessible to user level application is virtual one, heap/stack/whatever, some parts of this memory address space could be mapped to RAM, some to HDD (of course I'm talking about win/*nix) – Iłya Bursov Apr 11 '18 at 22:45

0 Answers0