2

I have a created hash map which is basically collection of nested hash maps :-

Map<String, Map<KeyPOJO,ValuesPOJO>> customMap= new HashMap<String, Map<KeyPOJO,ValuesPOJO>>();

I am inserting 10,000 records in it and want to determine its memory usage i.e. its memory size after its full with 10,000 records.

Please tell me how do i get that via program without using profiling tools and 3rd party libraries.

Timothy Drisdelle
  • 395
  • 3
  • 5
  • 14

3 Answers3

4

want to determine its memory usage i.e. its memory size

Call Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory() before and after creating and filling the map.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
  • That will not give exact memory usage of hash map it will also include memory of intermediate POJO's , variables, iterators etc.. what i need is memory occupied by hash map only – Timothy Drisdelle Dec 27 '13 at 10:01
  • @TimothyDrisdelle Yes, that is true, as long as these objects have references and therefore cannot be garbage collected. – Adam Siemion Dec 27 '13 at 10:03
  • so Runtime.getRuntime().totalMemory() wont work for me :) – Timothy Drisdelle Dec 27 '13 at 10:05
  • @TimothyDrisdelle What you can do is, create and fill your map like you do already and then call `totalMemory()`, create a new map and copy the content of the first map to this new map, call `totalMemory()` again. – Adam Siemion Dec 27 '13 at 10:06
  • 1
    There is a more severe reason why you can't easily use the `totalMemory` technique: thread-local allocation buffers. An allocation of a single `Object` can leave an 8K-footprint on `totalMemory-freeMemory` – Marko Topolnik Dec 27 '13 at 10:07
  • Also not that relying on just `totalMemory` instead of the difference `totalMemory-freeMemory` is double-wrong because it doesn't account for heap resizing. – Marko Topolnik Dec 27 '13 at 10:08
  • @MarkoTopolnik Thanks for the insightful comments. Updated the answer. Heap resizing can be prevented when -Xmx is equal to -Xms, right? – Adam Siemion Dec 27 '13 at 10:14
  • Yes, and TLABs can be disabled with `-XX:-UseTLAB` (or a similar option). But generally, it's a tricky thing, highly specific to HotSpot, its precise version, and so on. The best advice is to experiment, do some simple cases, convince yourself you have a reliable technique, and then actually measure. – Marko Topolnik Dec 27 '13 at 10:16
  • Can anyone then please tell me how will i come to know whats the size of hash map whenits fill with 10K entires.. Also what i feel is that simply size of hash map wont suffice because if we are storing POJO's in hash maps then those POJO's will also have memory size, so in all we need t calculate RETAINED HEAP size of this hash map . Please correct me if i am wrong. – Timothy Drisdelle Dec 27 '13 at 14:45
0

If you would just like to know how much memory is being used in your JVM, and how much is free, you could try something like this:

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();

// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
0
// Total number of processors or cores available to the JVM
long processors=Runtime.getRuntime().availableProcessors();

// Total amount of free memory available to the JVM 
long freeMemory=Runtime.getRuntime().freeMemory();

// This will return Long.MAX_VALUE if there is no preset limit 
long maxMemory = Runtime.getRuntime().maxMemory();

// Total memory currently in use by the JVM 
long totalMemory=Runtime.getRuntime().totalMemory();
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48