I have a slider used to change the font size of the text in a UITextView. Analyzer and Leaks report no memory leak. However, the memory grows each time I change the font size by moving the slider. Eventually the app gets an out of memory warning. The code is:
mainText.font = [UIFont systemFontOfSize:mainSlider.value];
If I replace that code with mainText.font = [UIFont systemFontOfSize:40.0];, memory stays the same no matter how many times I move the slider. I searched this and many other sites looking for info on a possible UIFont bug. No success. I see people using the same code I am using and not mentioning increasing memory. Please help.
There is probably a font cache in place to avoid regenerating the same fonts over and over again (a bit like [UIImage imageNamed:#""])
It is not explicitly stated in the docs but reading
You do not create UIFont objects using
the alloc and init methods. Instead,
you use class methods of UIFont to
look up and retrieve the desired font
object. These methods check for an
existing font object with the
specified characteristics and return
it if it exists. Otherwise, they
create a new font object based on the
desired font characteristics.
could make one believe there is a cache
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIFont_Class/Reference/Reference.html
Also, what are you calling an "out of memory warning"? I presume it's just a "memory warning level=1", and not an application crash because of out-of-memory?
Related
I have developed a sample for a potential problem in map.remove().
See: https://jsfiddle.net/moricio/e2wvwgu8/1/
This sample will choose a random lat/lng and will display a map centered at that point. It will wait 10 seconds before removing the map and generating a new position. It does this over and over.
If you open this sample in Chrome and check the Chrome task administrator(Shift-Esc) you will notice at each remove/add pair that the memory used by the tab will increase until your system crawls after a few hours.
Bug or my mistake?
This is a known issue in Leaflet - see https://github.com/Leaflet/Leaflet/issues/5263 and https://github.com/Leaflet/Leaflet/pull/5265 . Before that change, any L.Canvas or L.SVG (including the default one) were leaking quite a lot of memory. In Leaflet releases after that change, you should only experience a leak in one (hard to locate) circular reference to an instance of L.Draggable, which shouldn't be a big problem.
Here I've taken two heap snapshots in chrome dev tools and switched it to "comparison mode" so I can see the deltas between the two snapshots:
Notice that the first heap snap shot is 92.3mb and the second is 275mb (as indicated under "Snapshot 1" and "Snapshot 2" on the left). This is a size increase of 182.7mb.
In the right-most column, the size deltas for each constructor are listed (in bytes). If we sum these up we get a net increase of about 500kb.
How could this be? Shouldn't the size deltas sum be the same as the size difference between the snapshots? If you look at the chrome heap snapshots tutorial in the "Comparison View" section, there's a screenshot that shows what you'd expect: the size deltas sum to the change is snapshot size.
Note: I'm actually using the --inspect option of nodejs, so this isn't the regular chrome devtools, but it looks like it's essentially the same software (and maintained by the same team), just slightly adapted to nodejs, so I'm keeping it general by not tagging nodejs, etc.
Edit 1: I followed #wOxxOm's advice from the comments (click garbage bin icon before snapshot) and got a 60mb snapshot vs a 300mb snapshot, but when I clicked the mode dropdown and selected "comparison" to get the deltas I got this message (you'll probably need to open the image in a new tab to read it):
I'm beginning to think this may have to do with nodejs, and it seems like it's not just something I misunderstood about DevTools, so I'll add some extra details which might be clues as to what's going on:
I'm using node-webworker-threads which implements the WebWorker API in nodejs using native threads.
I'm passing these "workers" 10mb chunks of text to be processed and then have metadata returned to the main thread and aggregated. The text is being pumped into the pool of workers at about 5mb/s.
When I clicked the garbage bin icon as #wOxxOm suggested, my computer's RAM usage didn't seem to drop.
On this page it says "Only reachable objects are included in snapshots. Also, taking a snapshot always starts with a garbage collection." Could it be that the snapshot size is taking into account the RAM used by the workers, but the actual table of constructors + deltas only lists "reachable objects" in the main thread?
Here's a pie graph of the memory usage.
What are some examples of developer created memory leaks written in the 4D programming language?
By developer created memory leak, i am referring to a memory leak created by bad programming that could have been avoided with better programming.
32 bit
When ran in a 32 bit application it should eventually crash once it attempts to allocate more than 2^32 bytes (4 GB) of memory. If on the Mac OS X platform, the bottom of the crash report beneath the VM Region Summary should show a memory value around 3.7 GB:
TOTAL 3.7G
64 bit
When ran in a 64 bit application the code will continue to raise the amount of memory allocated and will not plateau, in that situation the OS will eventually complain that it has ran out of memory:
Overview
There are many ways that a developer can create there own memory leaks. Most of what you want to avoid is listed here.
use CLEAR VARIABLE when done using a variable
use CLEAR SET when done using a set
use CLEAR NAMED SELECTION when done using a named selection
use CLEAR LIST when done using a list
re-size your BLOBs to 0 with SET BLOB SIZE when done using the BLOB or use CLEAR VARIABLE
re-size your arrays to 0 when done using the array or use CLEAR VARIABLE
don't forget to close any open XML trees such as XML, DOM, SVG, etc (DOM CLOSE XML, SVG_CLEAR)
if using ODBC always remember to free the connection using ODBC_SQLFreeConnect
make sure to cleanup any offscreen areas used
Examples
Here are two specific examples of developer created memory leaks:
Forgetting to close XML
Bad code:
Repeat
$xmlRef:=DOM Create XML Ref("root")
Until (<>crashed_or_quit)
The code snippet above will leak memory because each call to DOM CREATE XML REF will create a new reference to a memory location, while the developer of this code has neglected to include a call to free the memory. Running this in a loop in a 32 bit host application will eventually cause a crash.
Fixed code:
This code can be easily fixed by calling DOM CLOSE XML when finished with the XML reference.
Repeat
$xmlRef:=DOM Create XML Ref("root")
DOM CLOSE XML($xmlRef)
Until (<>crashed_or_quit)
Forgetting to clear a list
Bad code:
Repeat
$listRef:=New list
Until (<>crashed_or_quit)
The code snippet above will leak memory because each time NEW LIST is called a reference to a new location in memory is returned. The developer is supposed to clear the the memory at the referenced location by using the CLEAR LIST($listRef) command. As a bonus, if the list has any sublists attached, the sublists can be cleared by passing the * parameter like CLEAR LIST($listRef;*).
Fixed code:
This can be easily fixed by calling CLEAR LIST($listRef;*) as seen in the following fixed example:
Repeat
$listRef:=New list
CLEAR LIST($listRef;*)
Until (<>crashed_or_quit)
I am not sure if I am posting to the right StackOverFlow forum but here goes.
I have a C# desktop app. It receives images from 4 analogue cameras and it tries to detect motion and if so it saves it.
When I leave the app running say over a 24hr cycle I notice the Private Working Set has climbed by almost 500% in Task manager.
Now, I know using Task Manager is not a good idea but it does give me an indication if something is wrong.
To that end I purchase dotMemory profiler from JetBrains.
I have used its tools to determine that the Heap Generation 2 increases a lot in size. Then to a lesser degree the Large Object Heap as well.
The latter is a surprise as the image size is 360x238 and the byte array size is always less than 20K.
So, my issues are:
Should I explicitly call GC.Collect(2) for instance?
Should I be worried that my app is somehow responsible for this?
Andrew, my recommendation is to take memory snapshot in dotMemory, than explore it to find what retains most of the memory. This video will help you. If you not sure about GC.Collect, you can just tap "Force GC" button it will collect all available garbage in your app.
I'm writing a MIDlet using LWUIT and images seem to eat up incredible amounts of memory. All the images I use are PNGs and are packed inside the JAR file. I load them using the standard Image.createImage(URL) method. The application has a number of forms and each has a couple of labels an buttons, however I am fairly certain that only the active form is kept in memory (I know it isn't very trustworthy, but Runtime.freeMemory() seems to confirm this).
The application has worked well in 240x320 resolution, but moving it to 480x640 and using appropriately larger images for UI started causing out of memory errors to show up. What the application does, among other things, is download remote images. The application seems to work fine until it gets to this point. After downloading a couple of PNGs and returning to the main menu, the out of memory error is encountered. Naturally, I looked into the amount of memory the main menu uses and it was pretty shocking. It's just two labels with images and four buttons. Each button has three images used for style.setIcon, setPressedIcon and setRolloverIcon. Images range in size from 15 to 25KB but removing two of the three images used for every button (so 8 images in total), Runtime.freeMemory() showed a stunning 1MB decrease in memory usage.
The way I see it, I either have a whole lot of memory leaks (which I don't think I do, but memory leaks aren't exactly known to be easily tracked down), I am doing something terribly wrong with image handling or there's really no problem involved and I just need to scale down.
If anyone has any insight to offer, I would greatly appreciate it.
Mobile devices are usually very low on memory. So you have to use some tricks to conserve and use memory.
We had the same problem at a project of ours and we solved it like this.
for downloaded images:
Make a cache where you put your images. If you need an image, check if it is in the cachemap, if it isn't download it and put it there, if it is, use it. if memory is full, remove the oldest image in the cachemap and try again.
for other resource images:
keep them in memory only for as long as you can see them, if you can't see them, break the reference and the gc will do the cleanup for you.
Hope this helps.
There are a few things that might be happening here:
You might have seen the memory used before garbage collection, which doesn't correspond to the actual memory used by your app.
Some third party code you are running might be pooling some internal datastructures to minimize allocation. While pooling is a viable strategy, sometimes it does look like a leak. In that case, look if there is API to 'close' or 'dispose' the objects you don't need.
Finally, you might really have a leak. In this case you need to get more details on what's going on in the emulator VM (though keep in mind that it is not necessarily the same as the phone VM).
Make sure that your emulator uses JRE 1.6 as backing JVM. If you need it to use the runtime libraries from erlyer JDK, use -Xbootclasspath:<path-to-rt.jar>.
Then, after your application gets in the state you want to see, do %JAVA_HOME%\bin\jmap -dump:format=b,file=heap.bin <pid> (if you don't know the id of your process, use jps)
Now you've got a dump of the JVM heap. You can analyze it with jhat (comes with the JDK, a bit difficult to use) or some third party profilers (my preference is YourKit - it's commercial, but they have time-limited eval licenses)
I had a similar problem with LWUIT at Java DTV. Did you try flushing the images when you don't need them anymore (getAWTImage().flush())?
Use EncodedImage and resource files when possible (resource files use EncodedImage by default. Read the javadoc for such. Other comments are also correct that you need to actually observe the amount of memory, even high RAM Android/iOS devices run out of memory pretty fast with multiple images.
Avoid scaling which effectively eliminates the EncodedImage.
Did you think of the fact, that maybe loading the same image from JAR, many times, is causing many separate image objects (with identical contents) to be created instead of reusing one instance per-individual-image? This is my first guess.