1

I am trying to reduce the amount of memory footprint a huge data table (array) occupies during execution.

For that I am using JCMD to print the histogram.

However I found something funny here:

 num     #instances         #bytes  class name
----------------------------------------------
   1:      28409956      454559296  java.lang.Float
   2:       3156662      227280584  [Ljava.lang.Integer;
   3:       3156660      227279520  [Ljava.io.Serializable;
   4:        526110       21044400  [[Ljava.io.Serializable;
   5:        233936       17436808  [C
   6:        241719        5801256  java.lang.String
   7:         54517        3684696  [Ljava.util.HashMap$Node;
   8:         93867        3003744  java.util.HashMap$Node
   9:         74535        2981400  java.util.LinkedHashMap$Entry
  10:         38269        2143064  java.util.LinkedHashMap
  11:           190        2108240  [[[Ljava.io.Serializable;
  12:         29416        1411968  java.util.HashMap
  13:          5345        1400312  [B

You see that I have 28409956 instances of Float, and that the total RAM it takes is 454559296 bytes. If it divide the bytes / instances, I will get 16 bytes per Float...! This means each Float occupies 128bits!?

What am I missing?

How can I further reduce the memory these Floats (and the Integers -- I can't use Short) take?

Or better, how can I force Kotlin to use primitives (I only found out how to do it in arrays with IntArray, FloatArray, etc)?

Thanks!

PedroD
  • 5,670
  • 12
  • 46
  • 84

1 Answers1

1

java.lang.Float is a wrapper type not primitive. It does take more memory.

Here is kotlin code and its java equivalent:

    fun m1(f: Float) = f

    fun m2(f: Float?) = f
   public final float m1(float f) {
      return f;
   }

   @Nullable
   public final Float m2(@Nullable Float f) {
      return f;
   }
Kiryl
  • 111
  • 4