Any way to get a Type Count?
BlitzMax Forums/BlitzMax Beginners Area/Any way to get a Type Count?
| ||
Hi. Lets say I have a Type Type Test End Type Is there a way to get the totaly amount of created Types of this Type? Something Like Test.Count ? tnx.. |
| ||
Question: do you want to count the objects created or how many classes (type) you have in the source code? In the first case just use a var to get track of created object (but not in the New method I think) In the second case I think reflection can offer a solution ( I m not at the computer atm and I can't remember the right methods/function) Sure reflection can be used to find the number of instanced object of a certain type also. |
| ||
degac.. I want to know in general on the whole process how many of a specific type exists. This is so i can track whether my application has too many of a specific Type. because is see memory increase while app is running and I want to make sure that my app doesn't have overload of a specific type. |
| ||
Create a global counter for the type. In New(), increment the counter. In Delete(), decrement the counter. Of course, types that you don't currently reference are still "counted" until they are GC'd. Type TMyType Global count:Int Method New() count :+ 1 End Method Method Delete() count :- 1 End Method End Type However, I'd suggest you have a more serious problem if you don't understand why you would have "too many" objects, and should consider revisiting your design. |
| ||
@Brucey Consider Elements not getting correctly dereferenced and therefore referencing their own and individual imageCache or other "memory intensive data". While this of course is a flaw in your design, it is a neat way to detect such things. @Hardcoal Pay attention that "new" is run for ancestors too SuperStrict Type A Method New() print "A" End Method End Type Type B extends A End Type Type C extends A Method New() print "C" End Method End Type new A new B new C Output is A A A C So this means: if you create "C" that custom "New" is called but also the "New" from Type A. So take care not to increase the counter in each "New" but only the basic one. bye Ron |
| ||
Any way to get a Type Count? No easy way of getting the internal reference count of a type AFAIK, but you can get any objects reference count with: print byte ptr(obj)[-4] -Henri |
| ||
but you can get any objects reference count with.. I really don't recommend using undocumented functionality, even if it does appear to work. |
| ||
Only way I see a need for this is for debugging purposes. But as said, theoratically the behaviour could change, allthough not likely. -Henri |
| ||
But as said, theoratically the behaviour could change, allthough not likely. Well, for example, in my WIP project (which allows for 64-bit BlitzMax apps, amongst other things), I dropped the ref counter on the object because my GC tracks it differently. As I say, it's better to stick with documented functionality :o) |
| ||
Offcourse i was about to use it for debug pupose and for my final version |
| ||
What I meant with "debugging purposes" was to use it to find memory leaks and such. As I say, it's better to stick with documented functionality :o) I'm not arquing against that :-). Don't know what the current status of Blitzmax documentation is, but it used to be a bit lacking. -Henri |