2

So I need to understand how swing allocates memory for buffering screen rendering. Obviously there are duplicates if you have double/tripple/etc buffering. However I would need to know when swing allocates memory and how much of it. Very helpful to know if I have multiple windows open (launched from the same jvm) how much memory is needed depending on windows being maximized to one screen, multiple screens (I need it to go up to 6 screens), etc.

Does anyone know of any good readings or maybe have answers for how Java Swing/AWT allocate memory for rendering buffers.

End of the day, I am looking for a definitive formula so that if I have a number of windows opened, number of buffers in each window, location of windows, and size of each window I can get an exact byte count required to render the application (just the buffering part, the rest of the memory is another problem)

I was assuming it was (single buffered) x by y of each window = 1 buffer, add those together and you have all memory requirements, but profiling the data this appears to be far from the truth, some buffers are weak/soft references, some strong, and I cannot determine the way to calculate (yet :)).

Edit: I am using JFrame objects (for better or worse) to do my top-level stuff.

Dmitriy Likhten
  • 5,076
  • 7
  • 36
  • 47

3 Answers3

3

I'd recommend JConsole and the Swing Source code fro this kind of precision.

I assume you realize this will be extremely tedious to calculate since you have to consider every object created somewhere in the process, which of course will depend on the controls involved in the UI.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thanks. I don't need exact numbers. I much more need to approximate it with high enough fidelity than get exact numbers. Even being 10 megs off would be OK if the answer is between 50 and 60 megs. I just need to understand approximately how much ram to allocate for this whole mess and be as minimalistic as possible, but not small enough to make the app unusable on large monitors (or at least know when to increase the ram requirements) – Dmitriy Likhten Apr 25 '11 at 18:49
  • 1
    Looks like some of these are from the non-open source part of the JDK. – Dmitriy Likhten Apr 25 '11 at 18:58
3

Double buffering is a convenient feature of JPanel, but there will always be a significant platform-dependent contribution: Every visible JComponent belongs to a heavyweight peer whose memory is otherwise inaccessible to the JVM.

If you're trying to avoid running out of memory, pick a reasonable value for the startup parameters and instruct the user how to change them. ImageJ is a good example.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • The problem is I need to use as little ram as possible. If I can save 50m on ram requirements, it would be very useful. A client may not have a spare 50m of ram. Or we want to ensure there is no paging going on if at all possible. I might disable double-buffering in my application, but I still would like to be able to approximate how much memory I should expect to be used by the rendering part of my application. – Dmitriy Likhten Apr 25 '11 at 19:01
  • Sorry, no first-hand experience, but some implementations can use the video card's RAM. – trashgod Apr 25 '11 at 19:10
  • We specifically turned off Java 2D hardware rendering because it caused problems (I was not part of that decision, but they had good reasons due to performance problems). – Dmitriy Likhten Apr 25 '11 at 19:12
1

I am not aware of any automatic screen buffering support in Swing. If you need double buffering, you need to implement it yourself in which case you will know better how to calculate memory requirements :-)

See this answer: Java: how to do double-buffering in Swing? for more information and good pointers.

Community
  • 1
  • 1
Sasha O
  • 3,710
  • 2
  • 35
  • 45
  • 4
    Swing does provide some double buffering. http://java.sun.com/products/jfc/tsc/articles/painting/#db – jzd Apr 25 '11 at 18:55
  • See also [`public JPanel(boolean isDoubleBuffered)`](http://download.oracle.com/javase/6/docs/api/javax/swing/JPanel.html#JPanel%28boolean%29). – trashgod Apr 25 '11 at 18:59
  • A Window has the ability to set up how many buffers to use with `.createBufferStrategy(numberOfBuffers)`... I am making a data app, not a 2d game or anything. – Dmitriy Likhten Apr 25 '11 at 19:00
  • 1
    @Dmitriy Likhten: `JPanel` inherits its two `BufferStrategy`s from `Component`. – trashgod Apr 25 '11 at 19:03
  • @trashgod: Yea sorry, I spaced out on the typing. I was looking in Component on how it builds the buffer strategies. Still does not help me since I need the impl details of when what sized buffers are built. I am just going to use `SingleBufferStrategy` to keep things simple while I try to understand it all. – Dmitriy Likhten Apr 25 '11 at 19:09
  • @Dmitriy Likhten: No problem; I point it out in case you want to look at how `JPanel` implements the buffering. – trashgod Apr 25 '11 at 19:12