简体   繁体   中英

Program not allocating more memory after reaching a limit

So I was taught the concept of memory allocation in the class and I thought why not to develop a program which can hog the resources well enough. The program is extremely simple. The source code is below - Environment - MS Visual Studio 2013 OS - Windows 8 x64

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
char i;
char *worm;
for (i = 120; i < 130; i++)
{
    worm = (char*)malloc(sizeof(i));
}

return 0;
}

Now, my laptop has got 5 GB of RAM but I see that after reaching a value of around 2 GB , the processor keeps on working but the memory usage does not increase no matter what. So is there any way that complete memory is hogged and the PC is rendered almost useless?

Please Note that I am doing this for educational purposes only :|

Edit :- One more thing I noted from the Task Manager is that the entire free memory gets allocated to the program, but the program is not able to get more memory because the OS has reserved it because it contains cached programs.

So, is there any way to address complete memory and make the computer crash or even very slow?

Check the return value of malloc() and you'll see that at some point is starts returning NULL , meaning the allocation has failed.

This happens after you've exhausted some memory resource, probably the virtual address space of your process, which is many 32-bit environments is limited to roughly 2GB. For information about Windows, see Memory Limits for Windows Releases .

It is possible that your RAM size is 5GB, but most of it is already in use by other running processes or by your operating system, that's why the available RAM size is around 2GB and you are unable to allocate more. Good luck :)

Operating systems these days are intelligent. Especially the new ones. I think visual studio has a way of working with memory leaks. From my personal experience, I once wrote a game in SDL, tried to allocate a surface in a loop that kept allocating new memory chunks with every iteration. I ran the process manager and saw it increase up til 100MB and stop after that. But I ran the same program in XP, the program ran for 20 minutes and hung. I had to forcefully plug the power off and restart it to make it work again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM