Solved have object survive reload

Discussion in 'Plugin Development' started by MrAwellstein, May 16, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MrAwellstein

    I need to have an object survive and be reusable after a reload (not a restart). The reason why is because it can take anywhere from 1-10 seconds to create this object and my plugin will rely heavily on it. I can not store it in a file (I can but I'd prefer to avoid that at all cost). Any ideas?
     
  2. Offline

    cfil360

    MrAwellstein it is impossible without storing it in a file. All stored data is removed unless it was saved.
     
  3. Offline

    MrAwellstein


    There are ways. I know for 100%
     
  4. MrAwellstein It might be best to tell us more about this slow to initialise but somehow fine to use object.
     
  5. Offline

    GotRice

    MrAwellstein All objects initialized by your java code are stored in RAM (unless you write it to a file), because it is constantly being used and RAM allows for fast read/write speeds. When you reload the server, all the RAM is cleared and loaded again (hence the word "reload").

    This is similar to how RAM works on your computer, RAM is used by the computer to store temporary things, and deleted when you restart your computer. This is why we have 2 separate storage solutions: RAM and Hard Drives/SSDs.

    I see no general solution to your problem except writing all those objects into a flat file or if your ambitious SQL.
     
  6. Offline

    Shevchik

    That is wrong. Server only reloads plugins and configuration on reload.
     
  7. Offline

    Polaris29

    MrAwellstein
    I'd just let it take a while to load after reloading, that's a price server owners have to pay for reloading. You could also try optimizing that object, what would it be doing that it takes 1-10 seconds to load? But if you really want to you could make that object not part of your plugin meaning that the object's class is not loaded by a plugin classloader.

    Shevchik
    The plugins are unloaded then loaded with a new ClassLoader upon reloading. The plugin's objects before the reload are in a different ClassLoader than which the plugin is using after the reload. That means the objects aren't very accessible and may be garbage collected.
     
    MrAwellstein likes this.
  8. Offline

    RawCode

    post your code and i will tell it is possible to save object or not
     
  9. Offline

    xTrollxDudex

    Shevchik
    You need to learn how Java works.
     
    MrAwellstein likes this.
  10. Offline

    Shevchik

    I know how it works. And it works not like GotRice described it.
    Reload does not reload the whole RAM dedicated to process, not even whole ram dedicated to plugin.
    You still have manually created threads, system properties, running processes and tons of other stuff which is not being reloaded on plugin reload.

    So you can just start separate thread on first plugin load which just sleeps all the time and get the object from here on next plugin load. Or just make some random server field hold your object, but hte thread aprroach will be a lot easier.
     
  11. Offline

    RawCode

    many people do not understand difference between reload and restart...

    reading source code of reload code will tell you how to store data via bukkit API only, it directly state howto
     
  12. Offline

    coasterman10

    When a server reloads, all the plugins' classes are unloaded, then reloaded under a new class loader. To my knowledge, there is no way to access variables between class loaders, not even static ones. Hence, programmatic methods would require some sort of flaky or hacky memory manipulation that is not supported by plain Java and thus indicates you are probably doing something that could be done better by some other method.

    If your object takes a very long time to create because it requires a large amount of calculations, make it implement Serializable, and then serialize/deserialize using a file. This will completely avoid any heavy calculations.

    It would still help to tell us what is being done in the 1-10 seconds of creation that causes the usage of files to be unfavorable.
     
  13. Offline

    RawCode

    coasterman10

    Holding valid reference to *old* classloader allow to get all data from classes loaded by it.
    You shoud carefully read javadocs about classloader and how java garbagecollection works.
     
  14. Offline

    coasterman10

    RawCode And how would you propose to keep a reference to said classloader?
     
  15. Offline

    RawCode

    coasterman10
    reading source code of reload code will tell you how to store data via bukkit API only, it directly state howto

    also you can read posts of other users in this thread and use search over forum, i answered similar question two days ago.
     
  16. Offline

    MrAwellstein

    One of the way of doing this that I could think would be with Threading. Threading allows things to be exempt from GC, so if there was anyway to recover information from a live thread then that would be my way of going. Anyways for those who asked, Im generating a public and private Key (I don't want to have a key that is static so thats why I don't have it load from a file)

    Edit:
    I know threading holds information past a reload. Thats how CloudConsole used to work.
     
  17. Offline

    LucasEmanuel

    Having the data as static will stop the garbagecollector from removing it from RAM. This would allow you to access it after a reload. But this is really really really bad practice.
     
  18. Offline

    xTrollxDudex

    MrAwellstein
    Threading is a very bad way to circumvent the GC. If you choose to do it, make sure to handle it properly.

    I believe the overhead cost in getting the thread from the active ones in the JVM and the cost of reflection access to get the Runnable object cast without Unsafe would be beyond that of fileIO.
     
  19. Offline

    MrAwellstein


    You're Right. I've decided to just have the data in my plugin load from a thread, and just have it so that no one can access anything inside of it until it loads correctly. Since it doesn't have anything to do with high leveled ingame alterations such as chest protection, I don't really need to have the server wait until my program is ready, but just have the access to my program wait until it is ready. (I really should have thought of that considering the main part of the program doesn't even start on the Main Thread).

    Despite solving this, I thought of a cheat way of doing it, which is with ServerSockets. The idea is to start a thread on onDisable that tries to connect to a random port on LocalHost, and then on onEnable, if the thread is alive, then start a ServerSocket and allow the thread to send you the object before dying. This can be accomplished without issues of much difficulty and without any port being open (unless a firewall prevents localhosted connection), although a few issues are if there are programs already listening on the ports chosen. But yeah I'll just have to generate a key each time it reloads and just deal with it. Thanks for the help everyone.
     
  20. Offline

    Minnymin3

    This is a bit delayed but I stumbled upon this thread while looking for something else. One way to store data over reloads (its a little bit hacky) is to store bits of data in block metadata (which is saved over reloads). You can put almost any type of data into block metadata (as far as I know at least) and retrieve it when your plugin re-enables. If its not there then the server is starting and you need to create a new map to fill or whatever you are doing
     
Thread Status:
Not open for further replies.

Share This Page