In C# or Java we find that the objects are stored on heap and their reference vars are stored on stack. But at run time where is the class definition stored to be used as a template for creating objects?

  • 3
    Where does class, object, reference variable get stored in java? ... See also blog.codecentric.de/en/2010/01/… : "The method area is responsible for storing class information." – Robert Harvey Jan 18 '16 at 18:06
  • Starting with Smalltalk, in most OO programming languages (Java included), a class is an object---an instance of a class named Class. – Solomon Slow Jan 18 '16 at 18:12
  • 1
    @JacquesB, P.S., I am speaking of the Java language as it is specified, and not of any particular implementation of the Java language. It may be that some particular JVM implementation segregates Class instances from other instances for performance or, to simplify some design issue; but that doesn't matter to a Java application developer. As far as the application developer is concerned, a Class behaves just like any other instance. – Solomon Slow Jan 18 '16 at 18:57
  • 2
    That paragraph doesn't say much. It says a little about how Oracle's JVM actually is implemented, but nothing about how somebody else's JVM must be implemented. The phrase "method area" hardly appears anywhere else in the whole document. There's nothing that says how to get a "handle" on the method area, how to inspect it, manage it, etc. To me, it says nothing more than, "Oh, by the way, our JVM stores byte-codes in a special memory region..." They don't even bother to tell us why they do that. If you deleted that section from the spec, nobody would miss it. – Solomon Slow Jan 18 '16 at 19:36
  • 1
    The idea that in C# value types go on the stack (or, as you put it, references to objects go on the stack) is, at best, only sometimes true. See Eric Lippert's blog about that: blogs.msdn.microsoft.com/ericlippert/2010/09/30/… – Jim Mischel Jan 19 '16 at 16:08

Class definitions are stored in a separate area (neither stack nor heap) called the method area. In .net the corresponding area is called the Loader Heap. Data in the method area is written by the class loader, and it is never garbage collected and cannot be deleted.

  • Where's the method area located? Is it itself put on some sort of heap? – Panzercrisis Jan 18 '16 at 20:22
  • 2
    If it's never collected, doesn't that imply that dynamically loaded classes/assemblies are effectively leaked when they're no longer referenced? – DeadMG Jan 18 '16 at 20:29
  • @DeadMG: Yeah, basically. That said, the JVM spec allows garbage collection of the method area, it just doesn't mandate it. But I'm not sure when it would be safe to collect a class. – JacquesB Jan 18 '16 at 20:55
  • @DeadMG thats a classic "permgen out of space" issue with class loaders in development servers (where we're always updating the code and reloading the class... but the old one is still there) – user40980 Jan 18 '16 at 21:51

A class is not a monolithic template. It is an abstract concept. It has methods, which are executable code. That code will be in the "code segment". The template you refer to would be a structure that contains initial values for data members and possibly a virtual method table. Those would be in the "data segment". That is, once everything is native code loaded into memory, ready to be executed. Before that (while it is IL or Java byte code), it is all data loaded into allocated memory. So that would be on the heap.

"Method area" as mentioned in a different answer is a Common Language Infrastructure level answer. That is software. Heap and stack are CPU level concepts, as are code segment and data segment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.