What if I try to malloc WAY too much memory?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what happens if I go for broke and I just try to allocate as much memory as I possibly can hey everybody welcome back today I want to do another what-if question and today's question focuses on memory but first off I just want to note that my channel actually recently hit a hundred thousand views which at this point proves to me that people watch this channel other than my mom thanks mom this started out as a hobby I posted a few videos for my students in my classes and it's grown and and it's really fun to be able to reach out to students that are outside of Clemson University get to know people on different continents in different countries and so thanks for watching so back to today's topic most programmers have allocated memory you call malloc you're passing the number of bytes you want and you get back a block of memory and you can put whatever you want in it integers float strings whatever but what happens when malloc says no to test this I want to write a simple program so in a loop I'm going to malloc a gigabyte of memory over and over again until malloc fails and returns null and just to see what's going on I'm going to count how many gigs that lets me have and then I'll print out the counter here okay pretty simple but before we run it when do you think it's gonna stop one hypothesis it may stop around six gigabytes because this machine has about six gigabytes of physical RAM I'm not really doing much with that memory so maybe once I use that all up around six gigabytes of RAM it's going to say no more maybe a little bit less so that's one possibility or maybe the OS has a fixed limit and when I hit that limit the OS is just gonna say nope you can't have any more memory so maybe it'll happen even before six gigabytes or maybe it will take advantage of the fact that we're using virtual memory and it will say hey I can stick stuff on disk if I run out of space and so maybe it will let me go beyond 6 gigabytes and use some of the disk ok enough suspense let's try it out and see so we compile it and then we run it Wow maybe you didn't expect that's 130,000 gigabytes or 130 terabytes which is at least 130 times the disk space that this machine currently has so what's going on so simply put the operating system is lying to you I'm serious when you ask for a gigabyte of memory the operating system says sure yeah go ahead you can have that gigabyte of memory let's see what he does with it but it doesn't actually set aside physical memory for it until I actually try to use it it's like when I was a kid and I told my dad that I wanted to build a 20 foot sailboat you didn't tell me no right off the bat he said that's an interesting idea that would be really fun why don't you drop a plan and see what you'd need and maybe find some books and and in the end the project ended up being a whole lot too big for me and I didn't even really get started on that sailboat maybe someday but the point is we didn't go out and buy a bunch of wood right up front and so it didn't really matter that my idea was crazy so the computers doing the same kind of thing you asked for memory and it says sure go ahead let's see let's see what they do with it because maybe you're one of those people that Malick's big to impress your friends but in the end most of that memory goes unused so what happens if we actually use all those gigabytes okay so let's move our malloc all out of the if statement and assign it to a variable that way I can actually use the memory and then I'm gonna add a mem set down here at the bottom now this if you haven't seen it before this is just going to set all of the bytes in the block we just allocate it to b1 okay just going to go through byte by byte and set each of them to one okay so the point is that now we're actually writing into that memory we asked for so we asked for a bunch of gigabytes and now we're actually going to write values into those gigabytes and now if we compile it oops sorry forgot a header and now if we compile it it's definitely a bit more sluggish than it was last time and it now fails at about 6 gigs which makes more sense now for the sake of completeness and while not letting well enough alone let's see if we can be sneaky and get around this so first what I'm gonna do is I'm going to allocate all the blocks so it's gonna let me have as basically as we saw before it's gonna let me have as many blocks as I asked for and then after I already have a lot of blocks allocated here we'll let's say 10,000 gigs right I've allocated 10,000 gigs now let's try to write to them and now if we compile and run ok so now our process gets killed after four gigs the moral of the story is don't try to do sneaky things and outsmart the OS and to giving your terabytes of memory it's not going to end well actually that's not the moral of the story but I think we've answered the question so thanks for watching I hope you enjoyed it and until next time I'll see you later [Music]
Info
Channel: Jacob Sorber
Views: 24,763
Rating: 4.9937725 out of 5
Keywords:
Id: Fq9chEBQMFE
Channel Id: undefined
Length: 4min 31sec (271 seconds)
Published: Tue Feb 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.