I noticed the size of each Runnable object is around 600KB. I am using the BlockingQueue with fix size of 10000. From the logging message, I saw all the 9864 objects had put into the queue. I did not see any the memory usage drastically increased. Should the queue be consuming 6GB (600KB x 9864) of memory?
Asked
Active
Viewed 1,799 times
2
-
3That seems awfully high for a single `Runnable` object. What are you putting in there? Show us where the `Runnable` is coming from. – Louis Wasserman Dec 14 '15 at 23:28
-
I used the "instrument" library. Basically follow the comment in http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java – user3842642 Dec 14 '15 at 23:58
-
I didn't ask how you measured, I asked where the Runnables came from. My suspicion is that they're all referring to the same chunk of memory so its just one block of 600kb. – Louis Wasserman Dec 15 '15 at 00:14
-
Thanks for helping. Please see my comment below see if it explain well. – user3842642 Dec 15 '15 at 00:21
-
It'd be much better to see the actual code of the Runnables. – Louis Wasserman Dec 15 '15 at 00:22
1 Answers
2
- First of all, BlockingQueue has only reference, so just BlockingQueue be consuming a very small memory.
- Secondly, important not how much objects in Queue, but how much unique objects (Queue may have a million references to one object).
- And at last, how you callculate size of each Runnable object? Also, every Runnable object has a lot of links to another objects, however some of this object can be shared between diferent Runnable object, so if size of the first Runnable objects is around 600KB, it isn't mean that other Runnable objects also will be such big.

Slava Vedenin
- 58,326
- 13
- 40
- 59
-
Very reasonable explanation. I agree the only the reference will inserted to the BlockQueue, but each Runnable object still consumes memory spaces. Basically each Runnable is working on a row object (ORM framework), pull a row of data from DB, do some actions, and then put it back. A new object will be created when a row is pulled from DB, aren't they considered "unique ojbects"? As I mentioned in the comment above, I calculate the object by following http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java – user3842642 Dec 15 '15 at 00:18