Determining the size of objects in memory

December 10, 2003

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 +
    <!--- multiply by two since chars in java are double bytes --->
    <cfreturn chars * 2>

Finding the size of a Component is a bit more complicated, I will leave that as an exercise for the reader...

Related Entries

4 people found this page useful, what do you think?


Well the author of this article is obviously missing the point. What the real issue is being able to determine how much does it cost to cache a query from a server-memory perspective (and to be able to fine tune the cached resultset as much as possible). The solution given here is simplistic since its calculating just the literal length of the dataset in question. What remains a mystery is how much overhead is involved for the server to be able to do this (a) how it is indexed in memory (hence how much does the server store in order to keep track of the apporpriate resultset). my $0.02
deepblue, I should have made it more clear that this is just an estimate. The actual size of the object is pretty much always going to be bigger than the function returns. Most objects do store private meta data, and there is no way for us to determine that size.
hello, I have a requiremnt like this. I want to find the length of the string . I am working in tomcat. Actually every string is terminated with null byte string so the string take some extra memeory for that one. I don't want that memory. Could you help me. PLzzzzzzz
well the author of this artical is trying to explain much better but not clearly. could you plz tell me the actual size of an java object.
another thing to keep in mind is that Java strings are usually interned.

meaning that if the string "Java Programmer" appears in the query 100 times, the memory allocation for "Java Programmer" only occurs once, and there are 100 pointers to it (in a 32bit JVM that would be 4 bytes each).
This function you've written for getting the size of a ColdFusion Query in bytes has been very helpful to me. Thanks Pete!

Post a Comment


Spell Checker by Foundeo

Recent Entries


did you hack my cf?