openframeworks / openframeworks/openFrameworks
ofBuffer clear() removes data but retains RAM
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
ofBuffer may lead to memory hogging in non obvious ways. I've just spent two hours looking for what I thought to be a memory leak that wasn't.
It turns out ofBuffer internally uses a std::vector<T> to fit data. std::vector<T> defaults to a greedy behavior in repsect of memory use; once a vector has been "grown" to a certain size, even if you clear its data back to zero, its internal data structure keeps the memory it took to fill it in the past in reserve, despite reporting a size() of zero. They call this the vector capacity().
I have a project with lots of ofBuffers that continiously load and release large amounts of data, and even when they were all empty (with clear()), the RAM usage of the app was still massive. I guess this behavior is ok for most cases, but it did bite me. I see no way to "shrink" the ofBuffer back to a capacity size relative to its current contents. This means that if, let's say, I load 1Gb of data into that ofBufffer, even if I clear() that ofBuffer, that 1GB of RAM is still claimed by the ofBuffer and there's no way to claim it back (unless I destroy the ofBuffer object alltogehter).
C++'s std::vector has a method named shrink_to_fit() that would be useful here; I would propose to either expose the internal vector<T> member ( buffer ) in the API, or wrap the shrink_to_fit()method and expose that?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in libs/openFrameworks/utils/ofFileUtils.h at the ofBuffer implementation and its internal buffer member. Read how clear() currently affects the underlying std::vector, then determine the smallest API change that allows released capacity to be reclaimed. Done means an empty ofBuffer can relinquish previously allocated memory without destroying the object.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- performance
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100