I've heard rumors about how memory allocation is slow and leads to memory fragmentation and other bad things. |
Well, we can delve into the hardware side. When you allocate memory for a variable, you do so in your RAM. RAM doesn't have fragmentation issues or anything like that, it's very quick memory where the information is physically stored on it doesn't really affect how fast you get the info off it. It's constantly written on and isn't likely to fail from being written on "too much". IF you don't have enough RAM to supplement your memory allocation needs, then your operating system likely has
virtual ram, which is space in your main disk drive that will be used as if it were RAM. This isn't very likely to happen though, even high RAM demanding video games rarely will need over 8gb of RAM (I have 16gb just to be safe). Tomb Raider is an exception, that game eats RAM like I'd eat a cheese cake. Mmmm...
Are you using an SSD or HDD? I assume an HDD, which are often used as scratch disks as needed. They use properties of magnetism to store information, and this can be done a lot of times. A hard drive is more likely to fail from being broken rather than because it's written too much! Memory fragmentation occurs when data for one thing is spread out over the hard drive, making the drive head spin all over to collect the needed data. Your operating system defrags the hard drive automatically. When used as RAM, the data likely wont be defraged, it's simulating RAM and will be deleted when not needed anymore.
An SSD, on the other hand, has a physical number (though usually very large) of times you can write to the drive. Never recommended as a scratch disk, you want that expensive SSD to live healthy until you upgrade. The more you write to an SSD, the slightly more the cells in it will slow and eventually fail - but that takes A LOT of writes for you to see a noticeable difference in performance. And there's no fragmentation on SSDs, the concept doesn't matter. All data can be accessed equally no matter their physical location on the drive. In fact, the information is usually spread out all over the cells in order to keep any particular cell from being written on too much and die faster than the other ones.
I don't know how malloc and such work behind the scenes, but an array stores information in an orderly fashion in your RAM. It's why an array can be used like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main()
{
int *arr;
int num;
std::cout << "HOW LONG DO YOU WANT UR INT ARRAY?: ";
std::cin >> num;
arr = (int*)calloc(num, sizeof(int));
for (int i = 0; i < num; i++)
std::cout << (&arr+i) << '\n'; //Will Output Each Memory Space For The Array
return 0;
}
|
Each element in the array will hold 4 bytes in your RAM, so the output of the program will be the memory location of each element, separated 4 bytes apart. It's in order, there's no fragmentation (or more like the term doesn't apply).
So overall, I wouldn't worry about it.
EDIT: Everyone else's answer seems to imply that
there is some issue with malloc and such when dealing with memory, there might be something about this I'm missing?
Can I just malloc() a lot of tiny arrays and let the memory manager handle it, or should I be trying to group these together into larger pages? |
The only issue I can imagine with RAM fragmentation is having your RAM almost full, but still with enough space for your program. The space, however, may end up not being physically in order on the drive itself, having free space here and there that were freed up, but other programs using blocks in-between.
In this case, there would still be no way around it yourself, since there's nothing you could do about that situation - the OS will be able to handle it better. I'm not sure how the operating system will handle such a situation (would the code above work? Or would part of the array be somewhere else in the RAM? Or would the OS simply not allocate any memory at all?), but I doubt anything will BREAK.
What you should know is that there are 1 Million kilobytes in a gigabyte - 1 Billion Bytes (around the same amount in a gibibyte if you wanna get technical about storage). You are very unlikely to run into any of these issues.