How many object can be created for a class in java

Dani AI

Generated

Short answer: Java doesn’t impose a fixed per-class instance limit — practical limits come from the JVM, the OS, and how much heap (and native) memory you give the process. To clarify ’s point: there is one java.lang.Class object for a loaded class per ClassLoader (that’s the class metadata), but you can create as many instances of that class as memory and GC behavior allow.

Factors that determine how many instances you can keep live:

  • per-instance memory (fields + object header + alignment/padding), which is JVM-dependent (compressed oops, 32-bit vs 64-bit, etc.);
  • any extra structures that hold references (arrays, collections) add their own per-entry overhead;
  • GC ergonomics and pause times: millions of long-lived objects push the old gen and will make full-GCs expensive;
  • OS/address space limits and your JVM -Xmx setting.

Quick, practical way to estimate for your JVM:

  1. Measure baseline used heap.
  2. Create N retained instances (keep references) and measure used heap again.
  3. avgBytesPerInstance = (after - before) / N.
  4. estimatedMax = (maxHeapBytes - baselineUsed) / avgBytesPerInstance.

Example measurement snippet:

long before = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
List<MyClass> list = new ArrayList<>(N);
for (int i = 0; i < N; i++) list.add(new MyClass());
long after = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
double avg = (double)(after - before) / N;

Practical advice: if you truly need tens of millions of items, avoid per-object overhead (use primitive arrays, packed structs, off‑heap buffers or memory-mapped files, streaming or databases), tune heap/GC, and profile with tools (VisualVM/jcmd/jmap, heap dumps). Use -XX:+HeapDumpOnOutOfMemoryError to capture state when you hit limits.

Recommended Answers

All 3 Replies

In practice it's limited by the available memory, but if each instance does not have big data members you can create tens of millions of instances

commented: The technical term in "gazillions"... :-) +12
commented: :) +0

technically, only 1 class instance is created for any class per classloader.
After that, any number of instance of that class can also be created, as needed, discarded, reused, serialised, deserialised, etc. etc.

Just for fun I created a small class (one int as its members)

class X{
   int i=0;
}

and an array to keep them in

X[] instances = new X[100_000_000];

then looped creating instances until it ran out of memory

for (int i = 0; i < instances.length; i++) {
    instances[i] = new X();
    if (i % 1_000_000 == 0) {
        System.out.println(i / 1_000_000 + " million");
    }
}

Using NetBeans with its default memory settings (2 Gig on my 8 Gig machine)it ran for 1 1/2 minutes before crashing with an out of memory exception, having created and kept over 54 million instances.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.