pf » Determining the size of objects in memory
Determining the size of objects in memory

Someone asked for a method to find out how much memory their cached queries, and components are using on the cf-talk mailing list today. In CFMX and in java there are no build in methods for determining the size of an object. The actual size of objects are a lot less important to Java and ColdFusion developers because the JVM handles all of the memory management for us. In languages like C the developer has to do all this work.
To find the size of an object you simply find the size of each object that it contains. That may sound recursive, and it is, but the base case (the values we know) are the sizes of primitive data types (such as int, char, float, double, etc). Primitive data types are always the same size on any platform in java (unlike some other languages). So to find the size of a string (there aren't really any primitives in CF, many types are just strings) we can just take the length of it and multiply it by the number of bytes per char, 2. Granted that is an estimate, there could be other data members inside the String object, and that's not counting things like the sizes of memory references.
Here's the function I came up with to determine the size of a query in ColdFusion:
<cffunction name="sizeOfQuery" returntype="numeric">
<cfargument name="query" type="query" required="true">
<cfset chars = 0>
<cfoutput query="arguments.query">
<cfloop list="#arguments.query.columnlist#" index="column">
<cfset chars = chars +
Len(Evaluate("arguments.query.#column#"))>
</cfloop>
</cfoutput>
<!--- multiply by two since chars in java are double bytes --->
<cfreturn chars * 2>
</cffunction>
Finding the size of a Component is a bit more complicated, I will leave that as an exercise for the reader...
Related Entries
- Null Java References in CF 6 vs 7 - January 10, 2006
- Thread Priority, and Yielding - April 6, 2005
- Checking your JDBC Driver Version - March 31, 2004
- How to make ColdFusion MX go to sleep - October 14, 2002
- Serializing CFC's in ColdFusion 8 - August 6, 2007
my $0.02
- Mastering CFQUERYPARAM
- Google Code Search for ColdFusion
- Speaking at CFUNITED 2008
- Getting ColdFusion SQL Statements from SQL Server Trace
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in










