[Closed] Does empty array indexes use memory?
Hi, just a bit curious, if I create an empty array then add data to lets say index 1000 will the 999 undefined spots use memory or would it use the same amount as if I added it to index 1?
Example:
theArray = #()
theArray[1000] = 8
--vs
theArray = #()
theArray[1] = 8
Thanks.
Trying this code:
m = heapfree
theArray = #()
theArray[100000] = 8
format "Memory usage= %
" (m-heapfree)
m = heapfree
theArray = #()
theArray[1000] = 8
format "Memory usage= %
" (m-heapfree)
m = heapfree
theArray = #()
--theArray[1000] = 8
format "Memory usage= %
" (m-heapfree)
m = heapfree
theArray = #()
theArray[100000] = 8
theArray[10000] = 8
format "Memory usage= %
" (m-heapfree)
you get this results:
Memory usage= 1352L
Memory usage= 1352L
Memory usage= 1104L
Memory usage= 1632L
That seems to mean that:
- Initialize an array: 1104L
- Initialize an element as integer, doesn’t matter the index: 250L-300L (aprox.)
So it seems the lower index elements aren’t initialized, just the defined index.
That’s not accurate. Maxscript will not report it as memory usage, but check your process memory in task manager. Each empty element will cost you the size of a pointer.
It cannot be only the size of a pointer either. undefined is a Maxscript value instance by itself, each one of them already requires the allocated ( nonstatic) memory size for the base class and all other superclasses ( class Value : public Collectable etc… ) plus the class Undefined : public Value instance itself . I guess an instance of Undefined will be the one maxscript value instance that requires the least amount of memory though.
Here’s the definition for the Undefined class in the max SDK headers ( maxscript\kernel\value.h )
class Undefined : public Value
{
public:
Undefined() { tag = &Undefined_class; }
classof_methods (Undefined, Value);
void collect();
ScripterExport void sprin1(CharStream* s);
Value* copy_vf(Value** arg_list, int count) { return this; }
// scene I/O
IOResult Save(ISave* isave);
static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
Mtl* to_mtl() { return NULL; } // undefined is a NULL material
void to_fpvalue(FPValue& v);
};
Right after writing my post, it came to my mind that array handling might indeed be more intelligent and not use a single “undefined” Value instance for each unassigned entry. So maybe it’s indeed only the size of a pointer. This would be a nice code analysis challenge starting with include\maxscript\foundation\arrays.h
Looking at the headers for the array class, we see that the data is a Value** (pointer to pointer of Value).
I would assume that it allocates as may pointers as needed. If you then query an index which contains a null pointer, you will probably get a dynamically allocated ‘undefined’ value. Again, just guesswork.
Do you think it’s the same for appData ID?
I mean, if I create for a node an appData with ID= 1000000, it will need to hold 1 million pointers?
That wouldn’t be nice at all for some of my scripts…
I doubt it, that is almost certainly implemented as a map and not an array.
Very interesting responses,
If I try :
theArray = #()
theArray[1000000000] = 8
3ds max in task manager goes from 700mb to 8.5GB so yeah no good.
Good that I found out because I was just about to write some pretty un-optimized code, thinking surely it will only create a pointer for the added values.