Skip to main content

setting the maximum heap size automatically

7 replies [Last post]
uckelman
Offline
Joined: 2007-11-21

I'm working on a program which, as one of its functions, displays some (potentially very large) images contained in a ZIP archive. The amount of heap space used by other objects is almost always insignificant in comparison with the amount of heap space used by these images, and the actual heap requirements can vary wildly depending on the particular images the user is loading.

The architecture of the program is such that one part of it (the "manager") launches child JVM instances for doing the work. This makes it possible for us to set the maximum heap size independently for each child. Presently, the max heap size is set in a preferences dialog. There's a default, which can be overridden individually for each input file. This would be a fine solution for us, except that most of our users don't understand what the maximum heap size is, nor have we ever found any way of explaining how they should set it which seems to make sense for them. As a result, we spend an inordinate amount of time providing user support on this issue.

My gut feeling is that this is a problem that the user should never be confronted with in the first place---that the manager program should be able to determine how to set the max heap for its children without any input form the user. At this point, we hit a wall: We have no clue how to estimate what a reasonable max heap size will be before the child is started, as this seems to depend on how the user will use the archive opened by the child. Maybe the user will need one image open at a time, maybe five. It occurred to me that something adaptive might work---if we run out of memory this time, then we increase the max heap somewhat for next time---but, among other things, I have no intuitions about what the increase should be like or on what it should depend.

Has anyone else here faced this problem and found a solution for it? I have a hard time imaging that I'm the first person to encounter this, but I turned up nothing relevant on the web after multiple tries.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
christiaan_se
Offline
Joined: 2006-07-13

"My gut feeling is that this is a problem that the user should never be confronted with in the first place---that the manager program should be able to determine how to set the max heap for its children without any input form the user"

Ideally you want java to use up memory resources as long as they are available, which is requested in this issue:
http://bugs.sun.com/view_bug.do?bug_id=4408373

My experience is that asking users what maximum number of images they want to see and what the maximum size is of those pictures is probably also something they can't answer. I think your best approach is to make it as scalable as possible, for instance, does everything need to be completely loaded into memory or can certain parts be minimized by saving the state to disk. I can imagine, that only one image is fully visible at a time and you use thumbnails to display other "active" images.

Another thing is that when an OutOfMemoryException is thrown, you catch this exception and inform the user how to increase the memory setting and suggest to close some images. This way the user is only confronted with the memory setting when he runs into problems.

kind regards,
Christiaan

uckelman
Offline
Joined: 2007-11-21

>
> Ideally you want java to use up memory resources as
> long as they are available, which is requested in
> this issue:
> http://bugs.sun.com/view_bug.do?bug_id=4408373

Actually, no, that's not what I want. Something I hadn't mentioned previously is that we're storing image tiles in a cache using SoftReferences. If the VM never experiences memory pressure, then no image tiles will ever be cleared from the cache.

> My experience is that asking users what maximum
> number of images they want to see and what the
> maximum size is of those pictures is probably also
> something they can't answer.

I agree. And anyway, it's not exactly a matter of user choice in this case. I see now that I haven't provided sufficient context. These image archives are for board games. The number and size of playing surfaces will vary from game to game. What the user is choosing is what game to play. It's possible for Game X to have much larger images than Game Y, for example.

> I think your best
> approach is to make it as scalable as possible, for
> instance, does everything need to be completely
> loaded into memory or can certain parts be minimized
> by saving the state to disk. I can imagine, that only
> one image is fully visible at a time and you use
> thumbnails to display other "active" images.

We already do this.

> Another thing is that when an OutOfMemoryException is
> thrown, you catch this exception and inform the user
> how to increase the memory setting and suggest to
> close some images. This way the user is only
> confronted with the memory setting when he runs into
> problems.

We do the former already---ask the user to increase his max heap---but not the latter, because it's not possible in the context in which we're working. Telling the user not to use the game map isn't feasible.

christiaan_se
Offline
Joined: 2006-07-13

I am a bit lost what your problem is and what kind of solution you are looking for. You mention you don't want java to dynamically use up available resources if needed as long as they are available, but you do want to be able to determine the maximum amount of memory java can reserve yourself? Seems contradictory to me. And really, limiting the cache size would be your simplest solution in case you want to limit the memory usage.

Anyway, I don't think there is an (easy) way of determining what the maximum heap size is, since this is depending on the machine configuration. For instance on windows it is depending on the dll's that have been loaded into memory.

uckelman
Offline
Joined: 2007-11-21

> I am a bit lost what your problem is and what kind of
> solution you are looking for. You mention you don't
> want java to dynamically use up available resources
> if needed as long as they are available, but you do
> want to be able to determine the maximum amount of
> memory java can reserve yourself? Seems contradictory
> to me. And really, limiting the cache size would be
> your simplest solution in case you want to limit the
> memory usage.

I don't understand your reply at all. We're already limiting the cache size: The max heap size limits the size of the cache. What I'm looking for is a way to determine what that limit should be, without user intervention.

cowwoc
Offline
Joined: 2003-08-24

Silly question: why not use the maximum possible heap? I believe that Java will run partial and full GC cycles that will keep the heap small...

uckelman
Offline
Joined: 2007-11-21

> Silly question: why not use the maximum possible
> heap? I believe that Java will run partial and full
> GC cycles that will keep the heap small...

What would the maximum possible heap be? Is there any way to get that number other than by attempting to start JVMs with increasing max heap sizes until you find you can't any more (or, working your way down, decreasing the max heap size until you find that you can successfully start a JVM)?

Message was edited by: uckelman

aldo_gg
Offline
Joined: 2005-07-06

I think its a good Idea to ask your users:
How many images your user will need to see open at the same time?
Which is the maximun size in KB of the image he needs to work with?
And with that information:
make both rules work and enforce in application code
calculate the heap size based on A and B yourself