Try this: http://martin.nobilitas.com/java/sizeof.html
The empirical formula derived there indicates that string memory is
38+/-2 + 2*(string length) bytes. In my own tests on JDK 1.4.2_09 I got
something similar: 40 + 2*(string length) bytes when length>2. For
length 0 to 2, the size works out to just 40 bytes.

That said, as Nick mentions, if all that you're trying to do is get an
idea of the relative memory occupied by different strings, rather than
the actual physical memory (which, as discussed in the link above, can
be determined only empirically, not precisely), you might be best off
just checking the length.

-----Original Message-----
From: Nick de Voil [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, April 19, 2006 9:47 PM
To: CF-Talk
Subject: Re: Get String Byte Size

> Anyone have any quick code to retrieve the number of bytes in a string

> /without/ writing the string to a file first?  I'm trying to do a 
> little debugging and I'd like to know the size of a string that is 
> being returned to the browser.

The number of bytes occupied in the application's memory by the Java
String object is probably not the same as the number of bytes occupied
by the same string in the HTTP response.

I could be wrong on some points below but I'm sure others will correct
me if so (Paul?)

As I understand it, a Java program such as CF always stores characters
internally using UCS-2 encoding, i.e. 2 bytes per character. In
addition, the String object will include 20 or 30 extra bytes for
storing the length of the string etc.

I believe that CF's default behaviour is to encode HTTP responses using
UTF-8 encoding, i.e. 1 byte per character if you're only using ASCII
characters, and of course the extra bytes used by the String object
won't be there either.

So let's say your string is "Rob".

- In CF the Len() function gives you 3.

- The size of the Java object - even if you could work it out, which is
next to impossible in Java - would be 6 + the extra bytes, maybe 40 or
more.

- But in the HTTP response it would probably be 3.

So, if I've understood your question correctly and it's the HTTP
response you're interested in, just using Len() in CF will give you the
best answer.

Nick







~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:238247
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to