dtuttle1@gmail.com
7/17/2007 12:05:00 AM
On Jul 16, 4:31 pm, Travis D Warlick Jr <warli...@operissystems.com>
wrote:
> dtutt...@gmail.com wrote:
> > Hi - I have a memory and cpu-constrained embedded app. There's a long-
> > running loop with a local object (buffer) which eats up memory. A
> > GC.start on every iteration will reclaim it, but that eats the cpu.
>
> > Is there a way to deallocate the memory used by the temporary objects?
>
> > file = File.new file_path
> > while buffer = file.read(512)
> > stream_host_connection.write buffer
> > stream_host_connection.flush
> > # GC.start - too expensive
> > end
>
> Someone please correct me if I'm wrong, but...
>
> You don't need to deallocate buffer every time because it is not getting
> duplicated at the beginning of every iteration -- it is being
> overwritten, so the memory usage for buffer will always stay at 512
> bytes (plus the static overhead for the object).
>
> Deallocating it every time would actually increase CPU usage because you
> would be deleting and recreating the buffer object at the beginning of
> every iteration.
>
> If you're really constrained on memory, you might want to consider
> decreasing the amount that you read at every iteration. However,
> remember that you increase your CPU usage by the same factor that you
> decrease your memory usage in this case.
>
> --
> Travis Warlick
>
> "Programming in Java is like dealing with your mom --
> it's kind, forgiving, and gently chastising.
> Programming in C++ is like dealing with a disgruntled
> girlfriend -- it's cold, unforgiving, and doesn't tell
> you what you've done wrong."
The object is being overwritten, but the old copies aren't being
garbage collected. I know this because the memory does stay constant
if I call GC.start on every iteration.
I think I'll go with a c extension for this small part of the code.
Thanks, Dave