0

I have a Java object with 2 properties. One of this objects properties is a String. Virtually every instance of the object will have the same value (or one of 2 values)

For example:

public record Car (String modelId, String brand) {}

If I have millions of these objects, each with a different modelId, but with a brand = either Ford or Volkswagen, how does Java manage the brand property in terms of memory?

I think(?) there is just one instance of the Ford, and Volkswagen String objects, and each Car will have a pointer or reference to those Strings - is this correct?

My main question: Given we only have a pointer to the brand property, are the memory savings from removing the brand property fairly small? i.e. if I remove the String brand property from the Car I'm not going to save much in terms of memory despite having millions of Car instances?

bonus point: how much memory might I save by removing (?1 million) pointers from the memory stack?

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
  • you may group your cars by brand before processing them ? – stephane brun Jan 17 '22 at 17:05
  • 2
    If you have only 2 options as brand, why not use ENUM? About instances of "Ford", it depends on how you create Car Object, if you pass it as "Ford" each time, A new instance gets created – Deepanshu Rathi Jan 17 '22 at 17:05
  • Consider using a "chunk size" instead of managing 1million objects at once. You can also using an enumeration for brands ? – stephane brun Jan 17 '22 at 17:06
  • 1
    The code in the example is hypothetical, to illustrate a question regarding memory management. The question is about how Java manages the String properties. I’m not looking for advice on how to improve the Java in the example, I want to understand the implications for memory. – robjwilkins Jan 17 '22 at 17:19
  • 1
    depends how the strings are being created - literals are automatically interned, so there is only one instance... strings from user input or database, or files are mostly not interned, so each is an own instance, but you can call the `intern()` method of the string yourself (using an `enum` would be more appropriate IMHO) – user16320675 Jan 17 '22 at 17:33
  • @user16320675 could you add an answer please? this is helpful – robjwilkins Jan 17 '22 at 18:37

1 Answers1

2

You can use string interning to ensure that each duplicate string points to the same memory.

Otherwise, if you create 1,000,000 Strings and set them to the value "Ford", you have created 1,000,000 copies of the bytes composing "Ford" in memory.

user229044
  • 232,980
  • 40
  • 330
  • 338