St.Antario
St.Antario

Reputation: 27455

Understanding memory allocation and GC in java

I'm trying to understand the working principle of grabage collection algorithms. I'm reading this article. As far as I understood each allocation is happening in Young generation. If there is not enough free space available Minor GC is triggered to clean the Young generation (Eden, S1, S2). But now imagine we have some class like:

public class TestYoungCrash{
    private long l1;
    private long l2;
    //...
    private long l100000000;
    //tons of other fields
}

So the object of the class does not fit to young generation even if the generation is completely clear.

What's gonna happen then? Is it standardized?

Upvotes: 3

Views: 87

Answers (1)

GhostCat
GhostCat

Reputation: 140641

It is not possible to have a single object that requires such an amount of memory. But not because of memory limits, but for a more practical reason - the JVM limits the number of fields per class, see here:

The number of fields that may be declared by a class or interface is limited to 65535 by the size of the fields_count item of the ClassFile structure (§4.1).

You can't have so many fields in your class that you would blow up memory. I am pretty sure: if you start a JVM with a heap so small that a single object containing those 65535 long fields would not fit in ... the JVM would most likely not even start.

In that sense, we could rephrase your question to something like: what happens when I create an array that is too large to fit into the heap space provided to the JVM? And then you are basically back to this question ... which says: OutOfMemoryError.

Upvotes: 5

Related Questions