Home > tech > What if you don’t like JVM garbage collection?

What if you don’t like JVM garbage collection?

At a technical interview recently, I was asked:

What if you don’t like Java’s garbage collection, what would you do? How would you manage memory?

You can carve a large array of bytes on the JVM heap, implement your own memory on that array and manage it yourself, kind of a virtual-machine-over-virtual-machine system; but I don’t see very good reasons to do that. Well-informed engineers have worked over a decade to get the JVM garbage collection right – so if you have to dump all that work and cook up your own scheme, you better have very good reasons. I told as much to the interviewer – I would imagine that it can be done, but I don’t know of any situation where I would recommend it.

Dear readers, do you know a good answer to this question? Do you know any situation where you would recommend this? If you come across such a situation, what would your solution be?

Categories: tech
  1. April 25, 2007 at 10:38 pm

    If you don’t like the JVM garbage collector, move to C# and use the .NET garbage collector πŸ˜€

    Seriously, if you need to make a change for the better you need to figure what is that you don’t like about the existing condition. What is that about the JVM garbage collector that you don’t like ?

  2. April 26, 2007 at 3:52 am

    I don’t know – this was an interview question. πŸ™‚ I can’t think of a reason, and my question to the readers is whether they know of any.

    If you don’t like the JVM garbage collector, move to C# and use the .NET garbage collector

    .. and enroll into to MASOCHISTS ANONYMOUS πŸ™‚

  3. April 27, 2007 at 10:14 pm

    Let us consider a situation where we need large chunks of memory for a small period in a cyclic manner. For eg, some imaging applications like IrfanView provides an option for batch processing of hundreds of images to a different format. Some scientific images can be as large as a GB, so memory usage should be carefully thought out.

    Solution 1:
    We could load each image file and create Image objects which are processed. This would be an object oriented solution which would make it simple to use. Memory is used up for each image object.
    We do something like this:

    Image img = new Image(path);

    This is simple but memory usage is far from ideal.

    Solution 2:
    Do manual memory management. Create a single image object (or maybe just allocate an array) and copy the image data values into the buffer of the object. Process the image. Load next image file. Copy image data to same buffer. Do processing.

    Image img = new Image(); //your only image object

    Stream s = File.Open(path);
    img.Buffer[i++] = stream.ReadByte();

    Solution 3:
    Create a memory manager (our own Garbage Collector) which would take care of where the actual memory for the image objects are allocated. The memory manager would allocate each image into the same memory.

    Image img = MemoryManager.CreateImage(path);

    The object creation can be generalized :
    Image img = (Image)MemoryManager.CreateObjectInPlace(img, typeof(Image), constructorParameter);

    or using Generics:

    MemoryManager manager = new MemoryManager();

    Image img = manager.CreateObjectInPlace(img, constructorParameter);

    Solution 3 is as concise as Solution 1, but uses only constant memory footprint with the help of a memory manager which can exist on top of any virtual execution mechanism like JVM or CLR.

  4. April 27, 2007 at 10:17 pm

    WordPress took out my Generics tag so I will use [] instead of “”.

    MemoryManager[Image] manager = new MemoryManager[Image]();

  5. April 27, 2007 at 10:17 pm

    Shoot.. WordPress does not allow angle brackets 😦

  6. Zenil
    May 22, 2007 at 6:55 am

    Consider a application that pulls out millions of rows from a database and allows it to be iterated as a resultset by a client…
    Consider also that the requirement this application can only be run with a max jvm memory setting of 512mb/1g…

    Consider also that the application may have to execute multiple million order queries from multiple databases and do a join on them within the application and produce a resultset!!

    The application can’t store ALL the rows for all the queries in memory as eventually its gonna run out of memory .

    The only way to manage this kind of situation is to have a memory management layer that sits on top of the jvm.

    SO u will do things like allocate blocks of rows(say 65000) from the memmanager,once its iterated,its immediately serialized to the disk,the corresponding block is freed and is gced by the jvm…

  7. May 23, 2007 at 11:57 pm


    From what I understand you want to read the results like a stream – materializing only portions of it at a time. I haven’t tried it, but I think this can be done in Java using a regular arrays and the regular GC should still work find in this case. Or did I misunderstand you?


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: