Results 1 to 7 of 7
Thread: ArrayList pointer size java
- 05-19-2010, 06:53 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
ArrayList pointer size java
Where can I find out how large an arraylist entry is in bytes? Not the object it is pointing to, but the pointer itself. I've seen some computations for HashMap entries, but I'm not sure how much I trust them as they don't have sources. Else I might have to resort to creating small programs and heap dumping them. Ideas?
- 05-19-2010, 07:54 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Why do you care? You have gigabytes at your disposal. For what it's worth: most variables in your class or object take up four bytes except longs and doubles, they take eight bytes. When booleans, bytes, shorts or chars are stored in an array they take 1, 1, 2 and 2 bytes respectively. On 64 bit systems object refererences take eight bytes instead of four. Per object of any class there's about eight bytes of overhead. Compared to those gigabytes there's nothing to worry about.
kind regards,
Jos
- 05-20-2010, 12:35 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
Your assumption that there are 'gigabytes' to work with and awful. I'm clearly asking for a reason. We have a program that would be ideally stored in memory. Each of ~9 million unique strings contains at this point a set of 40-50 strings. So not only do we have to store 9 million strings (if they are interned), but we have 9x45 million references that need to be stored in memory. See how that adds up quickly? So the references/array list entries add up to be a lot of memory very quickly. Figuring out which data type to use could have a gigantic impact on the memory footprint.
- 05-20-2010, 12:51 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Here's how you can check (look up the actual APIs):
1. Run garbage collection. Note that System.gc() doesn't guarantee it actually runs. you may want to do something like
With this code, you keep trying to run garbage collection and watching the memory. Once the amount of available memory stops changing, we can assume things were fully collected.Java Code:while (true) { long memory = System.getAvailableMemory(); System.gc(); Thread.yield(); Thread.sleep(10); long newMemory = System.getAvailableMemory(); if(Math.abs(memory-newMemory) < 10000) { break; } }
2. Create 1,000 arrays of 1,000,000 length of ArrayLists pointers (or whatever the numbers).
3. Run garbage collection again using the algorithm above, and see the memory difference.
- 05-20-2010, 12:54 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
But note that the size of the pointer might be platform-dependent.
you should expect at least 4 bytes, more likely 8
- 05-20-2010, 08:06 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
- 05-20-2010, 09:44 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
As said, the references themselves will be 4 or 8 bytes, depending on whether it's a 32 or 64 bit system.
An ArrayList entry (presumably you mean the pointer to the object) willl therefore be 4 or 8 bytes. So an ArrayList will be approximately (on a 32 bit system) 4 bytes for the AL reference, 4 * size for the contents, plus some overhead. For large ALs the overhead will drop off to negligible pretty rapidly, so is largely irrelevant. I don't expect there to be an overhead for each entry.
ETA: But, as Jos says, why not use a db of some description?
Similar Threads
-
Null Pointer Exception when adding items to ArrayList
By ShadowCopy in forum New To JavaReplies: 3Last Post: 08-27-2009, 09:23 AM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
Java heap size
By jmdrocks in forum EclipseReplies: 2Last Post: 12-12-2008, 06:10 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Limiting size of ArrayList
By ravian in forum New To JavaReplies: 3Last Post: 01-29-2008, 06:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks