Permlink Replies: 7 - Pages: 1 - Last Post: 26 May 21, 15:59 Last Post By: davidekholm Threads: [ Previous | Next ]
ctwist

Posts: 636
Registered: 27-Sep-2003
Post-generation clean-up
Posted: 25 May 21, 05:12
  Click to reply to this thread Reply
My skin's init.bsh creates 3 objects:
MgAlbum mgAlbum = new MgAlbum(engine);
Index index = new Index(engine);
Slide slide = new Slide(engine);
A few years ago, I added this to finally.bsh, to nullify the objects that are no longer needed.
slide = null;
index = null;
I don't remember whether or not the skin was misbehaving; maybe I just wanted to be sure that I was releasing all unneeded resources.

However, during current testing, I discovered that an array in mgAlbum, which was populated in the previous album generation, persisted to the next album generation. My solution was to add
mgAlbum = null;
to finally.bsh; this fixed it.

Java runs garbage collection occasionally. The aforementioned objects are deleted when garbage collection runs next time. To accelerate this, I added
System.gc();
to finally.bsh.

So finally.bsh contains
slide = null;
index = null;
mgAlbum = null;
System.gc();
I don't know when jAlbum releases objects that are created by the skin, so maybe finally.bsh just needs to contain
System.gc();
I tried to test this theory, but I couldn't reproduce my original problem. Garbage collection is processed when Java decides that it is time, so maybe I just couldn't replicate the bad timing.

Maybe jAlbum should run
System.gc();
immediately after finally.bsh?

Of course I use compiled Java. If you use BeanShell or Groovy, YMMV.

Anyway, if you have had any problems with an object persisting from one album generation to the next, maybe this message will help.
davidekholm

Posts: 3,676
Registered: 18-Oct-2002
Re: Post-generation clean-up
Posted: 25 May 21, 16:19   in response to: ctwist in response to: ctwist
  Click to reply to this thread Reply
I don't recommend that you do any explicit System.gc() calls. They will be done when needed anyway, and doing explicit System.gc() calls just empties caches that will slow down performance in general. If you declare an object within init.bsh, then it shouldn't persist on your next album build, but nulling some references when you know that you no longer have need for them is not a bad practice. I do the same with many references internally in the AlbumBean after a completed album build.
ctwist

Posts: 636
Registered: 27-Sep-2003
Re: Post-generation clean-up
Posted: 25 May 21, 16:30   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
OK, I will just nullify the objects that are no longer needed.

However, as I said, an object from the previous generation was reused. There is a method that verifies that the array does not contain duplicates. After the first make, it reported no errors. I made it again and it reported that every entry was duplicated. I made again and it reported that every entry was duplicated twice. Each time, I made the album as soon as the previous make finished.

I was surprised to see this behaviour. Anyway, it seems that nullifying the object fixes it, so I won't investigate any further.
davidekholm

Posts: 3,676
Registered: 18-Oct-2002
Re: Post-generation clean-up
Posted: 25 May 21, 18:06   in response to: ctwist in response to: ctwist
  Click to reply to this thread Reply
Ok, but it shouldn't behave that way so if you can produce a minimal example that reproduces the problem, I'm happy to look into it.
ctwist

Posts: 636
Registered: 27-Sep-2003
Re: Post-generation clean-up
Posted: 26 May 21, 06:07   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Attachment Minimalist.zip (80.5 KB)
I have done some more investigation.

The attached skin Minimalist is based on Minimal 7.2.
Changes:
  • New class MinAlbum. This saves each AlbumObject in an ArrayList, and outputs the ArrayList size to System Console. This is done before iteration starts.
  • init.bsh instantiates MinAlbum as minAlbum.
  • Finally.bsh sets minAlbum to null.

When I make the album, the ArrayList size 53 is shown in System Console.
If I make the album again, a count 106 is shown.
Again: 159.
Etc.

So, when Finally.bsh sets minAlbum to null, this does not nullify the ArrayList.

Alternative approach:
  • Instead of initializing the ArrayList when it is declared, initialize it in the MinAlbum constructor.
  • This works; the ArrayList count is 53 each time the album is made.

Questions:

  • Is this expected behaviour? If I set minAlbum to null, its ArrayList persists!
  • Should jAlbum remove the ArrayList? My expectation is that all objects from the previous album generation should be removed.
davidekholm

Posts: 3,676
Registered: 18-Oct-2002
Re: Post-generation clean-up
Posted: 26 May 21, 13:01   in response to: ctwist in response to: ctwist
  Click to reply to this thread Reply
I need not look further than to this:
public class MinAlbum extends CompiledBase
{
  static ArrayList<AlbumObject> vHiddenAOs = new ArrayList();
, you're using a static array, but shouldn't. "static" variables should be used very rarely as they have a very long lifetime. They survive album builds. They usually live throughout the runtime session. When you declare a variable as "static" it's not stored along with instances of that class, so nulling instances for garbage collection doesn't affect static variables. They are instead stored in the static section of RAM memory where class code is stored and there is only one instance of these variables per class.
ctwist

Posts: 636
Registered: 27-Sep-2003
Re: Post-generation clean-up
Posted: 26 May 21, 15:25   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Ah, yes. I should have spotted that. Problem solved.
davidekholm

Posts: 3,676
Registered: 18-Oct-2002
Re: Post-generation clean-up
Posted: 26 May 21, 15:59   in response to: ctwist in response to: ctwist
  Click to reply to this thread Reply
:-)
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums