From time to time, one of my apps experiences a delay of 2-10sec whilst dragging an object in a complex view. In some cases, the delay is sufficient to cause the wait cursor (spinning pizza of death) to appear.
Time Profiler reveals nothing very remarkable -- just the expected drawing calls to update the view as the object is dragged. (This is a lot of computation, but I see no obvious difference in the profile during pauses and between pauses.)
Memory profiler reveals nothing special about the pauses. Allocation seems flat throughout the drag. Leaks is clean. (My initial assumption was that I was spinning off too many autoreleased objects, but that doesn't seem to be the issue.)
Activity Monitor indicates that during the drag I'm pretty much using up one core for all the redrawing. That's what I expect.
Any ideas of where to look next?
Hmm. Well maybe you could try running Instruments.app with 'Record Waiting Threads' enabled. This will show you where the code is waiting as well as running. You might be sitting in a semaphore or doing some IO- that will show up in this mode.
Related
I have a few widgets on my main view, but when I add a new one, it exceeds the "limit" of widgets until it starts lagging. I've tried using different goroutines but I think that in a gui application that's not the case.
Here is a screenshot of before it lags:
and after:
I'm not an expert in this type of problems regarding performance but I don't know why this happens, it seems like there is a sort of limit of widgets before it starts lagging.
I tried even removing the functions from the buttons leaving them with no use, but the program still lags.
If I hide those three buttons though, it doesn't lag anymore.
Anyone is aware of the problem?
My app has a lot of API calls and image downloads, that's why i'm using Cache Manager package.
I noticed a lot of content is re downloaded and my app sends API requests without me doing anything but scrolling the screen.
I started checking and the build() method does gets rebuilt any time, but it has nothing to do with any setState() calls whatsoever!
Is there a chance the garbage collector or cache issues have something to do with that?
I get a lot of debug prints like Background concurrent copying GC freed 368(32KB) AllocSpace objects, 18(1632KB) LOS objects, 56% free, 1202KB/2MB, paused 25.610ms total 107.349ms.
If not what can cause it?
Well i deleted the question but thought about it and un-deleted it to answer with the solution if anyone else will ever encounter such a behavior:
It is very weird but seems like the problem was having my widget as a child of a RefreshIndicator widget.
I'm sure i didn't even scroll to the position it should indeed activate my refresh method, but scrolling any direction too far (even horizontally while my refresh indicator was a regular vertical one) activated it for some reason.
Sorry for not providing any example while asking the question, the app was too complicated and i didn't even know what is relevant and what's not...
Edit:
Now it's just a real mystery, i added a print('Refresh') line to my onRefresh method, and it never got printed, even when the widget got rebuilt!
If anyone have some kind of explanation i would really like to hear it.
I have a cocoa application window (NSWindow) which position on the screen should be updated frequently (depending on some calculation). As noticed in the documentation, UI changes should be made on the main thread:
void calculationThread()
{
while(true)
{
calculatePosition();
if(positionChanged)
{
dispatch_async(dispatch_get_main_queue(), ^{ setWindowPos(); });
}
}
}
void setWindowPos()
{
[window setFrame:_newFrame display:YES];
}
Now the problem I have is that the window movement is very slow and delayed. After making some profiling I see that the calculation process takes about 40mSec, meaning that I'm queueing up a backlog of UI updates 25 times a second.
I've read here that this might be faster than they can be processed and timer should be used to fire the changes every tenth of a second or so. But, wouldn't it be too slow for the human eye (I mean, in that case the movement wouldn't be delayed but would be lagged causing pretty much the same affect).
I will appreciate some knowledge sharing on this. Actually my main 2 questions are:
Are 25-30 UI updates per second really to much?
If yes, what is the recommended UI changes frequency?
The frequency at which a window can be moved around onscreen without problems will of course depend upon the speed of the user's machine, the video card they have, the size of the window, and probably a bunch of other factors. There is no single good answer to this. However, if you just drag a window around on your screen, you will notice that it can probably be moved very smoothly (unless your machine is very busy or very low on memory or something); I would not expect 25 times per second to produce a problem on a modern Mac. Not even close, in fact.
#RobNapier's points about Core Animation etc. are fine, but overstated I think; there is nothing inherently wrong with changing your UI using a timer or other periodic update if that is what you actually want to do. CoreAnimation is a toolkit for making some types of animation easier; using it is not required, and it is not suited to every problem. Similarly, if you want to make changes that are actually synched to screen refresh then CVDisplayLink is useful, but it doesn't really sound like that's what you want to do.
For your purposes, your basic approach seems fine, although I would suggest adding an NSDate check in order to skip updates if the previous update was less than, say, 1/60th of a second previous. After all, the calculation appears to take 40mSec on your machine, but it might be much faster on some other machine; you want to throttle your drawing to a reasonable rate just to be a good citizen.
So what is the problem, then? I suspect the issue might actually be your call [window setFrame:_newFrame display:YES]. If you look at Apple's docs for that method, they state "When YES the window sends a displayIfNeeded message down its view hierarchy, thus redrawing all views." Each time you call that method, then, you are not only moving your window (which I gather is your intention); you are redrawing all of the contents of the window, too, and that is slow. If you don't need to do that, then that is the overhead you need to eliminate. Call setFrameOrigin: or setFrameTopLeftPoint: instead (which make the semantics clear, that you are moving the window without resizing it or redrawing it), or perhaps just setFrame:display: passing NO instead of YES, and I'm guessing your performance problem will vanish.
If you do in fact need to redraw the window contents every time, then please edit the problem description to reflect that. In that case, the solution will have to involve profiling why your window drawing is slow, and figuring out ways to optimize that, which is an entirely different problem.
As you've discovered, you should never try to drive the UI from a tight loop. You should let the UI drive you. There are three primary tools for that.
For simple problems, AppKit is capable of moving windows around the screen. Just call [NSWindow setFrame:display:animate:]. You can override animationResizeTime: to modify the timing.
In many cases AppKit doesn't give enough control. In those case, the best tool is almost always Core Animation. You should tell the system using Core Animation how you where you want UI elements to wind up, and over what period and path, and let it do the work of getting them there. See the Core Animation Programming Guide for extensive documentation on how to use that. It focuses on animating CALayer, but the techniques are similar for NSWindow. You'll use [NSWindow setAnimations:] to add your animation. Look at the NSAnimatablePropertyContainer protocol (which NSWindow conforms to) for more information. For a simple sample project of animating NSWindow, see Just Say No from CIMGF.
In a few cases, you really do need to update the screen manually at the screen update frequency. I must stress how rare this situation is. In almost all cases, Core Animation is the correct tool. But in those rare case (some kinds of video for instance), you can use a CVDisplayLink to handle this. That will call you each time the screen would like to refresh, giving you an opportunity to update your content to match.
I would like to use the debugger but I have two problems
I am using SDL to create a graphics window, when I run my program in Debug mode I can no longer see what is going on in the window.
When you are simulating physics the time elapsed affects the behaviour of the program but when you are debugging the time elapsed and the code are no longer in sync so the program cannot behave normally. What strategies can I use to overcome this?
Redraw the window more frequently in the places that you want to debug, for example do one step code line to draw the line then next step line to redraw the window. Use 2 monitors so that debugger is in one and your program in the other. If you don't have 2 monitors then it will be a little inconvenient to debug but you can make your debugger window small and place it in one side of the screen and have your program running on the other side of the screen.
I must admit I don't know anything about simulating physics. If that's something where you use many threads and the order of execution matters between them then you can place breakpoints in each thread and then switching between threads you can step over and decide which thread and how far should progress. If that something within single thread then you may want to add a code which will be altering the time elapsed. Perhaps you could implement kind of simulator which would make sure the elapsed time in the given place of the code is as you would normally expect.
I am currently updating my app, and I have been facing a very strange and complex problem for the last few days. The part of the application that is problematic is made of one UITableViewController that is a list of news, and (after you click on a news) a detail view which is in fact a UICollectionView with as many details CollectionViewCells as there are news.
Each of these can have an infinite amount of elements, and are loaded 20 by 20 when the user scrolls to the bottom of the TableView (or to cell that is at the furthest position right on the CollectionView). Also, inside a DetailsCollectionViewCell, there can be another UICollectionView, containing images.
My problem is that after scrolling a few details views, after behaving normally (ie memory is allocated when I change the page, then stabilize until I change the page again, and so on), the memory allocation start to ramp up slowly but steadily, even if I stop doing anything at all. Also, the CPU usage will go to 100-120% and stay there, whatever I do, even, again, if I don't touch anything. After a while, the UICollectionView and the UITableView will not render any animation anymore, thus loosing the paging, and the inertia when scrolling, and overall resulting in a very poor user experience.
The strange thing is, I can observe these behaviours via XCode 5's Debug Navigator, but when I try to use instrument to find the source of the leaks/allocations, the allocation graph is normal, and I get 40-60 MB mem usage, no more, despite still observing the animations/paging problems.
Has anyone already met such a strange behaviour, and can someone help me in finding the cause of all this fuss ? (maybe by fixing Instruments ?)
Thank you all in advance, don't hesitate to ask me for more infos if needed !
I found the solution to my problems in the meantime.
About the difference in Memory usage between Xcode 5's Debug Navigator and Instruments, it was caused by my use of NSZombies. I have the habit of always setting them on, and that just flew off my mind... To remove them : Product > Scheme > Edit Scheme > Diagnostics > Enable Zombie Objects (just unmark it).
The cause of my CPU usage was an animation that was going on indefinitely in the background on multiple pages. The solution was to first of all stop it as soon as it is not seen/useful anymore, the optimise it by changing my approach (I was using CAAnimation and moved on to using UIView's animate function).
I think I might have pulled the trigger a bit too fast here, but hey... if this can help someone later, then it will not be a waste !