Re: please confirm something (jdbc)
- From: Thomas Kellerer <FJIFALSDGVAF@xxxxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 20:26:16 +0100
Lew wrote on 29.11.2007 16:01:
Thomas Kellerer wrote:Valid points, indeed, especially the one about the array beeing GC'ed pretty soon. But still, it will make a difference if I hold 20 or 40MB in memory.Note that using ByteArrayOutputStream.toByteArray() will create a copy of its internal buffer so that will double the amount of memory that you need. That's the reason I wrote my own dynamic byte array (where I can access the byte array without copying it around)
Using ByteArrayOutputStream.toByteArray() will also guarantee that "the valid contents of the buffer have been copied into [the resultant array]", and that the resultant array is the right size. I find these guarantees to be worth the copy overhead for reasonable-sized objects (under a couple MB, as is typical for, say, graphic images, one use case for BLOBs).
Given all the memory re-allocation that goes on inside the ByteArrayOutputStream anyway, I'm not sure that one more allocation is all that much extra overhead. If you do it right, the internal array of the ByteArrayOutputStream will immediately become eligible for GC right away anyway, so the "efficiency" you gain from avoiding that one copy might not be worth the extra risk and effort of maintaining a custom class.
It depends on how L your BLOB is, naturally. I can see how a large enough object would require striped processing and other tricks, but like most optimization this strikes me as one of those "Don't do it 'til you have to, and don't think you have to until you've measured" scenarios.
Well I am processing BLOB where I don't know the size (and the number) in advance, and they could potentially be quite big. My own ByteBuffer does basically the same as ByteArrayOutputStream with the exception that I expose access to the array (which is also guaranteed to have the right size).
I only partially agree with your optimization strategy though. If I have two ways of coding something, and I know that one will definitely use less memory (or be quicker for whatever reason) then I will choose the one "better" one. I think one reason for slow software is the attitude "I'll fix the performance issue later", because "later" you'll have so many places to fix that it will probably be hard to nail down the real cause.
Cheers
Thomas
.
- References:
- please confirm something (jdbc)
- From: stc
- Re: please confirm something (jdbc)
- From: Mark Jeffcoat
- Re: please confirm something (jdbc)
- From: Thomas Kellerer
- Re: please confirm something (jdbc)
- From: Mark Jeffcoat
- Re: please confirm something (jdbc)
- From: Thomas Kellerer
- Re: please confirm something (jdbc)
- From: stc
- Re: please confirm something (jdbc)
- From: stc
- Re: please confirm something (jdbc)
- From: Thomas Kellerer
- Re: please confirm something (jdbc)
- From: Lew
- please confirm something (jdbc)
- Prev by Date: Re: Auto serialize/unserialize on JPA
- Next by Date: Re: please confirm something (jdbc)
- Previous by thread: Re: please confirm something (jdbc)
- Next by thread: Re: please confirm something (jdbc)
- Index(es):
Relevant Pages
|