-1

I'd like to know the total size of a map like this

Map<String, String> map = new HashMap<>();
map.put(string1, string1);
map.put(string2, string2);
//many more lines following until 1 million entries populated to map

as you can see, the key and value always points to the same string and each string is guaranteed to be 24 char long containing lowercase letters and numeric (0-9) only. is it feasible to store this map (in heap I assume) for long term lookup table? how big is the size of this map?

Thomas
  • 191
  • 2
  • 12

3 Answers3

0

Assuming your computer has enough free memory, yes. A hashmap can store over 1 billion entries (230 == 1073741824).

Will
  • 810
  • 6
  • 21
0

If you want to check out the approx size of an object in Java, you can check out these questions:

Calculate size of Object in Java

In Java, what is the best way to determine the size of an object?

You could also use a profiling tool to look at the size in runtime. VisualVM, Java Flight Recorder or Java Mission Control

Community
  • 1
  • 1
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
0

Hello if you are curious how much memory you java app is consuming, try java visual vm. If you have JDK its located in it's bin folder.

As for you question, this is very bad idea (if its not for testing purposes), it will store (size of the map)*(24 characters in each string) * (2 size of char), because you key and value are always the same and String is immutable (all equal string pointers point to the same object in memory).

Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46