Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class size in PermGen space

There are lots of Q&A's about the size of a Java object, which is quite straightforward to understand. But I'm wondering about the size of a Java class in the PermGen space.

The reason I wonder about this is because I'm writing a code generator, generating a lot of classes. Essentially, I'm generating two classes for every table/view in a database. Now I also want to model foreign key relationships. Instead of maintaining a complex, serialisable object-structure (think about a table having a unique key being referenced by several foreign keys belonging to other tables having other foreign keys, etc), I'd prefer to generate one class per UNIQUE KEY and one class per FOREIGN KEY.

Here are my questions:

  1. How much overhead on the classloader and the PermGen space will I create with this?
  2. Is there a difference between public classes, static classes and private member classes?
  3. Do you see a better way to generate foreign key information in source code?
like image 684
Lukas Eder Avatar asked May 02 '11 18:05

Lukas Eder


1 Answers

I found a different solution, not wasting as much memory as generating one class per KEY. I generate a single class that roughly looks like this:

public class References {

    // First, initialise all unique keys
    public static final UniqueKey<TAuthorRecord> SysPk_14655 = 
        createUniqueKey(TAuthor.T_AUTHOR, TAuthor.ID);


    // Then initialise all foreign keys
    public static final Reference<TBookRecord, TAuthorRecord> SysFk_14666 = 
        createReference(SysPk_14655, TBook.T_BOOK, TBook.AUTHOR_ID);
    public static final Reference<TBookRecord, TAuthorRecord> SysFk_14667 = 
        createReference(SysPk_14655, TBook.T_BOOK, TBook.CO_AUTHOR_ID);


    // Factory method for unique keys
    protected static <R extends Record> UniqueKey<R> 
    createUniqueKey(Table<R> table, TableField<R, ?>... fields) {

    // Factory method for foreign keys referencing unique keys
    protected static <R extends Record, U extends Record> Reference<R, U> 
    createReference(UniqueKey<U> key, Table<R> table, TableField<R, ?>... fields) {

}

The actual tables from the generated table classes can then reference and use the above keys. I looked into JPA annotations as suggested by BobG in one of his comments. But I didn't find them very useful to describe:

  • Multi-field keys (@IdClass needs a type as parameter, and I want to avoid that type)
  • Multi-field references (how to do it?)
  • Multiple references from one table to another table using different keys
  • Unique keys, which share many properties with the primary key.

Some of the comments mentioned why I should create such a generator, because there are lots of established frameworks. I'm doing this for http://www.jooq.org. And I feel jOOQ is filling a gap in today's database abstraction possibilities.

like image 112
Lukas Eder Avatar answered Oct 13 '22 11:10

Lukas Eder