Can't keep up and skips ticks

Discussion in 'Plugin Development' started by spy85, Feb 25, 2014.

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

    spy85

    I receive the following error before a crash how can I fix this?

    Code:
    [18:45:14] [Server thread/WARN]: Can't keep up! Did the system time change, or is the server overloaded? Running 29916ms behind, skipping 598 tick(s)
    I have BukkitTasks constantly running at very fast paces if this has anything to do with it. Thanks!
     
  2. Offline

    xize

    spy85
    could I may ask what you try to do in those tasks??

    a good example of getting this overload could be when you try to connect to mysql, or InputStreams connected to other hosts/sites or iterators (in some cases).
     
  3. Offline

    NathanWolf

    You're doing something that's stalling the server for 30s (!!).

    If you're letting synchronous tasks pile up, that's definitely bad.
     
  4. Offline

    spy85

    Well every time a task executes I make sure it runs cancel() on itself to make sure tasks do not pile up. I am wondering how I could be stalling the server for 30 seconds though?

    Pretty simple just countdowns, checks such as checking server RAM and TPS and performing certain tasks depending on those variables. I don't think there is really anything harmful. I did at one point have wait(long) in a task because I was being stupid but I removed that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    xize

    spy85
    yea using wait() freezes the server because mostly everything is in a single thread.
    does your plugin use any connections such like mysql, Rss feed crawlbots or just site connecters?? most of them needs to be done in a seperated thread or being async.
     
  6. Offline

    spy85

    No but it does use serialization if that matters.
     
  7. Offline

    xTigerRebornx

    spy85 Can we see the code that is in those tasks that you schedule?
     
  8. Offline

    spy85

    I have tons of BukkitRunnable's on different tasks in several different plugins I have created so I can't say it would be the easiest thing to show you every one of them but I will show you some or most of the ones I can say for sure are running at all times throughout the game.
     
  9. Offline

    xTigerRebornx

    spy85 If any of them have anything that would be considered as a "heavy load"on the server, such as complex math calculations or rapid task times (more then once per second), show those
     
  10. Offline

    spy85

    Ok I have a lot of tasks that run quicker than per seconds probably around 5 for some.
     
  11. Offline

    spy85

    Here is a task that repeats throughout the entire time:

    Code:java
    1. package Memory;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.LivingEntity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.Plugin;
    10. import OverlappingObjects.BukkitRunnable;
    11.  
    12. import Interfaces.ClassObjsInter;
    13.  
    14. public class MemoryManagement implements ClassObjsInter {
    15.  
    16. private static Plugin plugin;
    17. private static int ticksCheck = 10;
    18. private static TPS ticksPS;
    19. private static boolean isStart = true;
    20.  
    21. public static void start(Plugin plugin){
    22. if(MemoryManagement.plugin == null){
    23. MemoryManagement.plugin = plugin;
    24. }
    25. start();
    26. }
    27. public static void start(){
    28. if(isStart){
    29. ticksPS = new TPS(plugin);
    30. plugin.getServer().getPluginManager().registerEvents(ticksPS, plugin);
    31. new MemoryChecker().runTaskLater(plugin, 20*6);
    32. }else{
    33. new MemoryChecker().runTaskLater(plugin, ticksCheck);
    34. }
    35. isStart = false;
    36. }
    37.  
    38. private static int failTimes = 0;
    39. private static class MemoryChecker extends BukkitRunnable {
    40. public void go(){
    41. try{
    42. long fmb = Runtime.getRuntime().freeMemory()/1048576;
    43. float tps = ticksPS.getServerTPS();
    44. if(fmb < 550 || tps <= 11){
    45. LagManager.clearEntities();
    46. for(Player p : plugin.getServer().getOnlinePlayers()){
    47. p.kickPlayer(ChatColor.RED+"(LowMemory)"+ChatColor.AQUA+" A new game is beginning!");
    48. }
    49. stopServerSoon();
    50. }
    51. else if(fmb < 900 || tps <= 16.75){
    52. ticksCheck = 1;
    53. ArrayList<LivingEntity> mobs = co.startEndGame.getCurrentRound().getMobs();
    54. for(int x=0; x<6; x++){
    55. mobs.get(x).remove();
    56. mobs.remove(x);
    57. }
    58.  
    59. if(failTimes >= 7 || tps <= 15){
    60. List<LivingEntity> aes = plugin.getServer().getWorld(plugin.getConfig().getString("waypoints.world")).getLivingEntities();
    61. for(int x=0; x<7; x++){
    62. aes.get(x).remove();
    63. aes.remove(x);
    64. }
    65. }
    66. if(++failTimes >= 12 || tps <= 13.75){
    67. LagManager.clearEntities();
    68. }
    69. }else{
    70. ticksCheck = 10;
    71. failTimes = 0;
    72. }
    73. }finally{
    74. start();
    75. cancel();
    76. }
    77. }
    78. }
    79.  
    80. public static void stopServerSoon(){
    81. new StopServerTimed().runTaskLater(plugin, 40);
    82. }
    83. private static class StopServerTimed extends BukkitRunnable {
    84. public void go(){
    85. plugin.getServer().shutdown();
    86. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "wwzcancel");
    87. cancel();
    88. }
    89. }
    90.  
    91. }
    92.  


    Here is another that handles match uptime for certain uses:
    Code:java
    1. package net.strikecraft;
    2.  
    3. import org.bukkit.plugin.Plugin;
    4. import OverlappingObjects.BukkitRunnable;
    5. import org.bukkit.scheduler.BukkitTask;
    6.  
    7. public class MatchUptime {
    8.  
    9. private static Plugin plugin;
    10. public MatchUptime(Plugin pl){
    11. plugin = pl;
    12. }
    13.  
    14. private boolean canKeepGoing = true;
    15.  
    16. private static int totalTicks = 0;
    17. public int getTotalTicks(){
    18. return totalTicks;
    19. }
    20.  
    21. public void restart(){
    22. if(canKeepGoing){
    23. timing.cancel();
    24. }
    25. repeatTiming();
    26. }
    27. public void stop(){
    28. canKeepGoing = false;
    29. }
    30. public void start(){
    31. canKeepGoing = true;
    32. timing = new Timing().runTaskLater(plugin, 20);
    33. }
    34.  
    35. BukkitTask timing;
    36. private void repeatTiming(){
    37. timing = new Timing().runTaskLater(plugin, 20);
    38. }
    39. private class Timing extends BukkitRunnable {
    40. public void go(){
    41. if(canKeepGoing){
    42. totalTicks++;
    43. repeatTiming();
    44. }
    45. cancel();
    46. }
    47. }
    48.  
    49. }
    50.  
     
  12. Offline

    BRADLAAAAA

    This is a problem with vanilla as it is crashing the server whenever i go through a nether portal!
     
  13. Offline

    NathanWolf

    ^ wow that seems like a totally random comment? XD

    spy85 if Plugin.getConfig is actually reloading the config file each time, that could be the problem.

    If you have so many plugins with so many schedules tasks, you may need to just do the work here yourself. Like run a local server with one plugin at a time until you find then one that's causing the trouble. Then start disabling the tasks in that one plugin until you find the culprit.

    Otherwise, asking us to sort of guess what the problem could be is a bit difficult! Generally make sure you aren't doing any heavy I/O (sockets, files, Db connections, etc) in those scheduled tasks, or anything crazy like chunk loading or searching through tons of blocks.
     
  14. Offline

    spy85

    So basically I need to make sure I have no tasks that don't take any time to execute but start and finish executing immediately so that this way the server does not get behind? I will go through each take and make sure each one is working completely perfectly without and stutter. I'll let you know how it goes thanks so much I believe this is the exact problem.

    Also, what about spawning a lot of mobs at quick paces consistently in tasks is that going to be bad? Maybe I should spawn the mobs with like 10 at a time, then wait 10 ticks and do it again till it has spawned the amount needed?

    What the crap? Haha man if you need help with something just start a Thread in the correct forum alright? If you need help doing so let me know.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    NathanWolf likes this.
  15. Offline

    NathanWolf

    Sounds like you have the right idea! Good luck, it can be a pain to track stuff like this down.
     
Thread Status:
Not open for further replies.

Share This Page