пожалуйста, возвращайтесь позднее
пожалуйста, возвращайтесь позднее
So far, we've been talking about running time. Now we have to talk about the memory requirements over our programs as well. Well, the basics are we want to know how many bits the program use or bytes, eight bits at a time. And actually, we'll be talking in terms of millions of bits or billions of bits and actually surprisingly there is a controversy about even these basic definitions. Computer scientists think of a million bits is two^20 and a billion is two^30 because that's a number of possible things that you can fit into 30 bits and everything is consistent with our calculations. Other scientists stick to one million or one billion for a lot of reasons we'll usually use two^20, I mean, a megabyte. Now an old computer we used to for many years, we use a 32-bit machine so that pointers were four bytes. Just in recent years we've mostly switched to a model where machines are 64-bits and pointers are eight bytes. That allows us to address much more memory but pointers use much more space and actually this transition caused a lot of problems initially because programs were using way more space than people thought they should. You're not going to have to go through this kind of transition the way that we did because 64 bits is definitely enough to address anything that you might need to address, two^64 is really a huge number. So in terms of bytes we have to start out with typical memory usage. Now, again, this is very dependent on machine and implementation but these numbers are reasonable and are found on typical implementations. So a boolean, it will be nice of a boolean just took a bit cuz that's just true or false but actually, usually we have to count for a byte for a boolean. All byte is a byte. Character nowadays is two byte, 16-bit characters. Not that a long ago we used eight bit for chars. Integer regular int is four bytes or 32 bits and a float is also four bytes long int is eight and a double is eight. Usually, we use double for floating point and ints for integers in most applications. So, that's for primitive types. And then for arrays there's a certain amount of overhead for making an array and then if there's n items, it's whatever the cost of the primitive type times n so an array of doubles is say 8n + 24. And two-dimensional array then well, we can go ahead and compute the exact thing but now, now, it's time to use, the tilde notation. And then for arrays we could say a double is tilde 8n for one-dimensional. For two-dimensional, two-dimensional array of doubles is tilde 8mn. And there's extra terms for the over head but for large m and n that's going to be pretty accurate. So, that's our basic usage for primitive types and arrays in a typical Java implementation. Now, a lot of our programs and objects like link list and so forth. So, we have to also factor in object overhead to crossover reference and also there's padding built in, in typical implementations to make it so that each object has used a multiple of eight bytes. So, for example if you have a date object that had three int instance variables then that object would take a total of 32 bytes. Each int takes four bytes, object overhead is sixteen bytes. It needs four bytes for padding so it's a total of 32 bytes.