- Home /
Am I wasting memory with this? Alternatives?
Hello everyone,
I was just wondering if what I'll try to explain is an innecesary waste of memory or is actually fine.
Let's say I have a class (we'll call it Information), which holds quite a lot of data (not that much, a couple of float values, a texture2D, strings...), and I want to add a pair of values int/Information, to a queue. Of course I can't do that, as the generic queue only accepts for one type to be added, so I was thinking on creating a new class Information_with_int, which containt an instance of the class Informtion, and an integer, then passing that new data type to the method.
So I was wondering, am I increasing the memory cost (i mean greatly) by doing this? When I create an instance for the new class will I waste the whole memory an Information instance takes? (plus the memory an integer takes which is quite neglectable at this point). Or as it's all done by references, it will only waste the little bit of memory is needed to hold those pointers?
I don't know many things which happen at low level so the only thing left is asking (and of course, search the web, but something like this isn't generating many valid search results, it will take a lot of time studying to figure out some basic doubt).
Thank you very much!
Answer by whydoidoit · Jan 20, 2013 at 03:36 PM
Your new class would just take the memory for a reference to the instance (4 bytes) plus an int (4 bytes). You might consider using a struct as it won't then need garbage collecting when you are done with the instance in the queue.
Thank you! that's what I was looking for.
And You're right, using an struct might be better, but as they are value types (not reference), I'll get a copy when passing to a method. Then, what would the copy actually be, a copy for the two refferences, o a copy for the whole class plus the integer?
I'd bet it's the first one, but I better be sure about it before jumping into the code.
Yes, just a copy of the reference and a copy of the int.
Thanks again, quick and simple! I've marked this as answered and voted up.