3

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?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

5

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • We can resize the Young Generationg, can't we? – St.Antario Oct 11 '17 at 09:12
  • Sounds reasonable, actually. Thank you. – St.Antario Oct 11 '17 at 09:16
  • 1
    Well, actually, large arrays may bypass the young generation and get directly allocated in the old generation. Resizing the young generation just for the sake of a few or even a single array instance would not be a good strategy. Further, the bypassing eliminates the costs of copying if the array survives. See [this Q&A](https://stackoverflow.com/q/24618467/2711488) for a discussion. By the way, it’s imaginable that even non-array objects could be that big. A single class file is limited to 65535 fields (actually even less), but classes can *extend* other classes… – Holger Oct 11 '17 at 14:19
  • Fair enough. Is there a limit regarding the length of the inheritance tree?! :-) – GhostCat Oct 11 '17 at 15:54