Sublime Forum

Plugin stays in memory after each call

#1

Hello everybody! Can anyone tell me how to solve this problem? I wrote a plugin that searches for a selected word in an offline dictionary and displays its translation in a popup window. This is very convenient when translating texts, allows you to find the translation of unfamiliar words without the Internet and wasting time. The problem is that with each use, the dictionary remains hanging in RAM and the plugin host begins to devour more and more memory, eventually I close sublime and open it again. How can I unload the plugin from memory after closing the popup window? I tried sys.exit(), but it closes the python interpreter completely. Unfortunately, I couldn’t find an answer in the manual, so I’m writing here.

plugin source code

0 Likes

#2

You don’t unload and reload plugins; they’re supposed to be always available.

It sounds like your problem is that you’re not properly releasing resources that you’re obtaining while your plugin is actively running. Without seeing what it is or how it’s working it’s hard to say why that would be the case though.

0 Likes

#3

Just a casual try:

import sublime
import sublime_plugin
import os
import sys

script_dir = os.path.dirname(os.path.abspath(__file__))

dict_path = os.path.join(script_dir, "full-eng-rus")
dict_obj = None

sys.path.insert(0, script_dir)
from pystardict import Dictionary


class OfflineTranslate(sublime_plugin.TextCommand):
    def run(self, edit):
        global dict_obj

        v = self.view
        word = v.substr(v.sel()[0])

        if dict_obj is None:
            dict_obj = Dictionary(dict_path, in_memory=True)

        try:
            translate = dict_obj[word.lower()]
        except KeyError:
            self.view.show_popup("I can't find this word", location=-1, on_navigate=print)
            raise ValueError("I can't find this word")

        self.view.show_popup(translate, location=-1, max_width=700, on_navigate=print)
3 Likes

#4

Thank you very much! Now everything works as it should!

0 Likes