• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How Java Stores Data in Memory: Writing Efficient Code with Primitives and Non-Primitives

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
How Java Stores Data in Memory — Primitives vs Non-Primitives


Java is a statically typed language, which means every variable’s type must be known at compile time. This allows the JVM (Java Virtual Machine) to allocate memory efficiently and enforce type safety during execution.

Primitive Types in Java


Primitive types are the building blocks of data in Java. They have fixed memory sizes and are stored in the stack. The value of a primitive can change, but its size does not.

TypeSizeDescription
byte1 byteSmallest integer (–128 to 127)
short2 bytesShort integer
int4 bytesDefault integer type
long8 bytesLarge-range integer
float4 bytesSingle-precision float
double8 bytesDouble-precision float
char2 bytesUnicode character
boolean1 bit*True or False (handled as 1 byte internally)
Note: Although boolean is conceptually 1 bit, it is usually stored as **1 byte* in memory due to alignment and practical implementation constraints.*
Non-Primitive (Reference) Types


Non-primitives, also called reference types, are more complex and stored in the heap, while their references (pointers) are kept in the stack. These types include:

TypeDescription
StringSequence of characters (immutable)
ArraysOrdered collection of elements (of any type, including primitives)
ClassCustom-defined objects
InterfaceAbstract types that other classes implement
EnumA fixed set of constants (internally class-like)
WrapperObject representations of primitive types (e.g., Integer, Double)
CollectionsData structures like List, Set, Map, etc. (part of Java Collections Framework)
How Java Stores Data in Memory


Java abstracts memory management with automatic garbage collection, but here’s how it works under the hood:

Stack Memory

  • Stores method calls, local variables, and primitive types.
  • Fast access due to LIFO structure.
  • Memory is freed automatically once a method exits.
Heap Memory

  • Stores objects and non-primitive values.
  • Accessed via references stored in the stack.
  • Managed by Garbage Collector (GC).
  • Larger and slower than stack but can store dynamic and complex structures.
How Primitive Types Are Accessed

  • Stored directly in the stack.
  • Access and modification are fast and straightforward.
  • Since they are fixed-size, the JVM knows exactly how much memory to allocate and retrieve.
How Non-Primitives Are Accessed

  1. When a non-primitive is created, the reference (memory address) is stored in the stack.
  2. The actual object is stored in the heap.
  3. Accessing the object involves following the reference from stack to heap.
Example:


String name = "John";
  • name (stack) → points to → "John" (heap)

If we change name = "Jack";, now name points to a new "Jack" in heap (because String is immutable).

Memory Alignment and JVM Optimization


Java is designed with the principle: “Write once, run anywhere”, which means JVM must work on both 32-bit and 64-bit CPUs.

JVM Alignment Strategy:

  • Regardless of declared type, JVM aligns memory into 4-byte or 8-byte chunks depending on CPU.
  • Operations on smaller types (byte, short) are converted to int internally.
Why is that?

  • byte takes 25% of a 4-byte block; short takes 50%.
  • CPU needs extra steps to calculate offsets and align the memory block.
  • int consumes the entire 4-byte block, making storage and retrieval faster.

This is why:

JVM promotes byte and short to int during calculations and requires explicit casting to store results back.
Misconception: Using byte/short Saves Memory?

Why byte and short are less efficient:

  • JVM treats them as int internally.
  • Adds extra overhead during arithmetic and access.
  • Introduces hidden conversions (widening and narrowing).
  • Slower due to memory misalignment and CPU inefficiency.
For performance and alignment, use int as default for integers.
What About Wrapper Classes?


Wrapper classes (Integer, Double, etc.) are object versions of primitive types.

  • Stored in heap like any object.
  • Used in Collections (e.g., List<Integer>, not List<int>)
  • Include utility methods like parseInt, compareTo, etc.
Note: Auto-boxing and unboxing happen automatically:
int a = 10;
Integer b = a; // auto-boxing
int c = b; // unboxing

Boxing adds memory and performance overhead, so avoid unnecessary boxing in critical code paths.

Key Takeaways

  • Primitive types are fixed-size and fast, stored in stack.
  • Non-primitives are stored in heap with reference in stack.
  • JVM uses int as the baseline for all smaller integral operations.
  • Avoid using byte and short for optimization, it backfires due to hidden processing.
  • Wrapper classes add object overhead; prefer primitives for performance-critical paths.
  • Java abstracts memory handling, but understanding this helps you write more efficient code.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу