The java object that takes the least memory

This is a stupid question, but here it goes.

I have a multithreaded program and a "global" collection of unique elements. I rejected synchronized implementations of Set due to performance for ConcurrentHashMap. I really don't need the Value of Map part, so I wanted to use the smallest Object in java in terms of memory usage. I solved this problem differently (one Boolean object was referenced several times in Map), but I'm still wondering what the smallest object in Java is. I always thought it was logical, but it is not. I think ( Java is a Boolean primitive type - size , Primitive data types )

+7
source share
5 answers

In fact, it does not matter, since part of the value of each association is fixed as a link. You can even use null as the value here, but any other (fixed) object reference should be nice (and more convenient sometimes). I would prefer Boolean.TRUE (or a similar "well known" singleton). Then you can check your membership through

 if (myMap.get(someKey) != null) { ... } 

in addition to

 if (myMap.containsKey(someKey)) { ... } 
+4
source

If you want a Set<K> that is supported by ConcurrentHashMap , you should use Collections.newSetFromMap , for example

 final Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>()); 

Now, if you really want to invent a wheel and care about memory usage, I suggest you just use a simple Object as your value. Since every object in Java inherits from Object (a universal base class), the size of any object in memory must be greater than or equal to the size of a simple Object . You cannot use primitives, since the general type arguments must be Object s.

EDIT: actually allocating a specific object to use as your value here will require more memory than using a pre-existing object, which is likely to be allocated for any paths. You can simply use a reference to an object that will more or less always be allocated during VM initialization, for example. Object.class . I really suggest you use only the first solution.

+4
source

The size of the object consists of:

  • the size of the instance variables it contains
  • 8 or 16 byte header (depending on the Hotspot virtual machine (32/64 bit))
  • padding: its size is always padded in multiples of 8 bytes.

For example (assuming a 32-bit JVM):

 public MyBoolObject { boolean flag; } 

takes 16 bytes : 8bytes (header) + 1byte (instance variable) + 7bytes (padding).
Since you are not interested in map values, you can set them to null. This consumes 4 or 8 bytes of memory from the stack (32/64 bits).

You can also check out this good list for cost / elements of well-known Java data structures: http://code.google.com/p/memory-measurer/wiki/ElementCostInDataStructures

+2
source

Primitive data types are not objects.

Since all objects in java should inherit from super class object. Then the smallest conceivable object in java will be the class that you define, which has no members. Such a class would be useless.

0
source

The Object class is an instance, and its instances are by far the smallest objects in Java. However, many other objects are exactly the same size, Integer and Boolean are examples on 64-bit virtual machines. This is due to heap memory alignment.

0
source

All Articles