dtuttle1 / 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."