We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,586 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java Hash Table Issue with Object Refference

I have a Hash Table like,

HashTable ht = { (1, 1), (2, 1), (3, 1) }

Now, I implement it like, Integer foo = Integer(1) and declare hash table like,

HashTable ht = { (foo, foo), (2, foo), (3, foo) }

Now, as per I understood from this, it will reduce heap space used by JVM. Is this correct ?

Another point is that, in C, I usually use structure like,

HashTable ht = { (1, mem), (2, mem), (3, mem) }
{ where mem is memory location (say 10) of 1 }

And then use the location to access the value. Now, if mem value is less than Int (say Byte), I can save space. However, I don't understand how to implement this in Java. Or is there any other way to reduce space of the hash table ? (means by reducing repeatedly storing same object in Java's way). Any idea ? Thanks in advance.

2
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
Arpssss
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

In Java there are primitives (int, char, float, byte, boolean) and there are Objects.
A HashTable can only contain references to Objects. No primitives and no Objects, just references to Objects. If you try to put an int into a HashTable the compiler automatically creates an Integer Object for you (that's an optimised ptocess b.t.w.) and puts a reference to that Object into the HashTable. If you put the same Object multiple times into a HashTable the table will just contain multiple references to that one Object.

The space taken by a reference is something private to the particular JVM you are running on at the time. There is no reason to think that a reference on a cellphone JVM will be the same size as a reference on a 64 bit server JVM.

Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?

JamesCherrill
... trying to help
Moderator
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33

In Java there are primitives (int, char, float, byte, boolean) and there are Objects.
A HashTable can only contain references to Objects. No primitives and no Objects, just references to Objects. If you try to put an int into a HashTable the compiler automatically creates an Integer Object for you (that's an optimised ptocess b.t.w.) and puts a reference to that Object into the HashTable. If you put the same Object multiple times into a HashTable the table will just contain multiple references to that one Object.

The space taken by a reference is something private to the particular JVM you are running on at the time. There is no reason to think that a reference on a cellphone JVM will be the same size as a reference on a 64 bit server JVM.

Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?

Thanks.

"Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?" == No. I have faced this issue while Coding. I have original structure like this,
key => value1 => value 2 (HashMultiMap- Key with Multiple Values). And same value, I have to store for both key and value like

key 1 --> values 1 , 2, 3
key 2 ---> values 2, 9, 10

So, I am thinking to reduce space by removing repeated objects example,

key 1 --> values 1 , A, 3
key 2 ---> values A, 9, 10

where A is such a procedure (less than long) that reduces the Space for example As I use in C, Pointer concept.

Actually this is because of Guava's HashMultiMap. It says after some insertions (say for example 2/3 million URL string, in 8GB RAM) that Heap Memory is full. I used some URL part as key and others as value. I don't understand why Heap Space is full (because it should store more according to some result-set. That's why I am searching for some basic easy way to solve Heap Memory issue). Memory Space is also an issue to me because I have to store more key value pairs. Thanks.

Arpssss
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

OK, so
1. Since the map only contains references, never Objects, your approach is based on a misunderstanding. You cannot store an object in a hashmap. If you try to add the same object twice you will always get two references to the same object.
2. The JVM won't use all available 8Meg by default - you have to play with the memory options on the java or javaw command to allocate very large amounts.
3. If this is really a problem then maybe look at alternative structures such as having a simple array of URLs and using the hashmap to store arrays of ints as its values, where the ints are indexes into the URL array. My guess, however, is that you won't gain much (except complexity).
Remember - don't confuse objects with references in Java.

JamesCherrill
... trying to help
Moderator
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0640 seconds using 2.7MB