Lifecycle of a custom plugin

Hello everyone,

I’m currently writing a plugin with a custom rule.
In this rule I load a lot of things in memory that are not freed automatically.

My question is: Writing a rule that extends the IssuableSubscriptionVisitor class, is there a place in my plugin where I’m sure the rule parsing is complete so I can free up memory?

The thing is that I noticed that there is the leaveFile method which launches when the analysis leaves the file but I’m afraid that each time I have to load my data then free it with the leaveFile, then recharge them etc.

Isn’t there a way or a method all at the end of my rule or all the rules to free up memory?

In addition, can someone explain me how the plugin lifecycle works during a scan? which files and/or methods come into play?

Best regards,

Elio.

Hello @Elio ,

Coming back to you a bit late.

For the Java analyzer and a traditional (see below) custom rules plugin, there is nothing publicly available in place to perform what you want to achieve. This is voluntary, as storing elements of parsing (trees, symbols) in rules classes can precisely destroy performance and lead to OutOfMemory errors if not very carefully cleaned.

So, unfortunately, no.

That’s where I believe there is maybe something to try. You can probably create an advanced custom rules plugin, with 2 components (sensors?) that would be executed at some special moments in the analysis. One would be executed before the execution of the Java sensor, and one after. If your data loader is a singleton, you could then theoretically trigger the load of the data before, then access it from your rule during the scan (driven by the Java sensor of the Java analyzer), and then free the memory after. To do so, you can explore the use of Phase annotations from the sonar API (playing with PRE and POST), as well as the @DependsUpon() and @DependedUpon() annotations.

I didn’t play much with it, so you might want to explore a bit here, but I think it can do the trick.

Hope this helps,
Michael

2 Likes

Hello @Michael,

Thank you very much for these answers. Even if they are negative, I have at least an answer to my questions. If ever I have other worries, I won’t hesitate to recreate a topic.

Have a nice day,

Elio.

2 Likes