Collection intermediate operations library in Java7 - collections

I Like the idea of intermediate operations of Java8, Where all operations will applied once when a terminal operation is reached.
I am asking is there library that I can use with Java 7 that allow me to achieve such behaviour.
Note:
I am using commons-collections4 for collection operations, like forAllDo, So it is possible to use it for such case? (intermediate vs terminal operations)

Guava
As your [Guava] tag suggests, most Guava collection operations are lazy - they are applied only once needed. For example:
List<String> strings = Lists.newArrayList("1", "2", "3");
List<Integer> integers = Lists.transform(strings, new Function<String, Integer>() {
#Override
public Integer apply(String input) {
System.out.println(input);
return Integer.valueOf(input);
}
});
This code seems to convert a List<String> to a List<Integer> while also writing the strings to the output. But if you actually run it, it doesn't do anything. Let's add some more code:
for (Integer i : integers) {
// nothing to do
}
Now it writes the inputs out!
That's because the Lists.transform() method doesn't actually do the transforming, but returns a specially crafted class which only computes the values when they are needed.
Bonus proof that it all works nicely: If we removed the empty loop and replaced it with e.g. just integers.get(1);, it would actually only output the number 2.
If you'd like to chain multiple methods together, there is always FluentIterable. That basically allows you to code in the Java 8 Stream-like style.
Goldman Sachs Collections
While Guava usually does the right thing by default and works with JDK classes, sometimes you need something more complex. That's where Goldman Sachs collections come in. GS collections give you far more flexibility and power by having a complete drop-in collections framework with everything you might dream of. Laziness is not theer by default, but can be easily achieved:
FastList<String> strings = FastList.newListWith("1", "2", "3");
LazyIterable<Integer> integers = strings.asLazy().collect(new Function<String, Integer>() {
#Override
public Integer valueOf(String string) {
System.out.println(string);
return Integer.valueOf(string);
}
});
Again, doesn't do anything. But:
for (Integer i : integers) {
// nothing to do
}
suddenly outputs everything.

Related

Is there a way to chain traversals in tinkerpop ? (other than .flatMap())

We've one photo sharing application and I'm using tinkerpop 3.4.3 java library and AWS Neptune graph. In our application, we're already using .flatMap() step to chain the traversals from the other methods. Our current code looks like this.
public boolean isViewable(String photoId, String userId) {
return graph.V(photoId).hasLabel("photo")
.flatMap(getDirection(userId))
.otherV().hasId(userId).hasNext();
}
Based on the userId, we retrieve the correct direction/relation information from the other systems and use it here for the result.
Unfortunately we're facing marginal performance issue when using the .flatMap() step when the number of edges of the photoId are high (100K edges).
...flatMap(inE()).otherV().profile() results in ~5000 milli seconds but the same query without .flatMap results in less than 200 milli seconds.
To avoid this, we've modified our current code like the below.
public boolean isViewable(String photoId, String userId) {
GraphTraversal<Vertex, Vertex> traversal = graph.V(photoId).hasLabel("photo");
applyDirection(traversal, userId);
traversal.otherV().hasId(userId).hasNext();
}
private void applyDirection(GraphTraversal<Vertex, Vertex> traversal, String userId) {
if(condition) {
traversal.inE();
} else {
traversal.outE();
}
}
But code looks complex without the chaining. Is there any other steps are available to chain the traversals ?
I don't think your code without the chaining is all that complex or hard to read. It's quite common to take that approach when dynamically building a traversal. If you really dislike it you could build a DSL to make a custom step to encapsulate that logic:
graph.V(photoId).hasLabel("photo")
.eitherInOrOut(userId)
.otherV().hasId(userId).hasNext();
If your logic is truly that simple for determining the Direction you could also use the little known to() step:
graph.V(photoId).hasLabel("photo")
.toE(getDirection(userId))
.otherV().hasId(userId).hasNext();

default Stream<E> stream() vs static<T> Stream<T> of(T t)

default Stream<E> stream() is added to Collection interface to support streams in Java 8. Similar functionality is supported by public static<T> Stream<T> of(T t) in Stream class. What different purpose is static of() method solving ?
The method stream() of the Collection interface can be called on an existing collection. Being an interface method, it can be overridden by actual collection implementations to return a Stream adapted to the specific collection type.
The standard collections of the JRE don’t take this opportunity, as the default implementation’s strategy of delegating to spliterator(), which is also an overridable method, suits their needs. But the documentation even mentions scenarios in which the collection should override stream():
This method should be overridden when the spliterator() method cannot return a spliterator that is IMMUTABLE, CONCURRENT, or late-binding.
In contrast, the static factory method(s) Stream.of(…) are designed for the case when you have a fixed number of elements without a specific collection. There is no need to create a temporary Collection when all you want is a single Stream over the elements you can enumerate.
Without collections of potentially different type, there is no need for overridable behavior, hence, a static factory method is sufficient.
Note that even if you don’t have a enumerable fixed number of elements, there is an optimized solution for the task of creating a single stream, when no reusable collection is needed:
Stream.Builder<MyType> builder=Stream.builder();
builder.add(A);
if(condition) builder.add(B);
builder.add(C);
builder.build()./* stream operations */
As far as I can tell, of is just an utility method to create Streams on the fly, without the need to wrap your elements inside a collection first.
Generally static factory methods as of are provided to skip the array creation because of var-args. For example java-9 immutable collections provide many overloads of these methods like:
Set.of()
Set.of(E)
Set.of(E e1, E e2)
.... so on until 11
Set.of(E... elem)
Even the description of those methods is:
While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls
Since there are only two methods in Stream:
Stream.of(T t)
Streamm.of(T ... values)
I consider that small utility methods that can create Streams from var-args.
But they still provide a method that creates a Stream with a single element (instead of leaving just the var-args method), so for a single element this is already optimized.
A interface can add static methods/default methods since jdk-8. and you can see some examples of applying patterns on interface in this question.
First, they are both calling StreamSupport.stream to create a stream.
// Collection.stream()
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
// Stream.of(E)
public static<T> Stream<T> of(T t) {
return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
}
stream() added to Collection interface is a good example to applying Template-Method Pattern in interface by default methods. and you can see the source code that stream method call a method spliterator which implements by default in Collection interface.
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
AND class derived from Collection can be override spliterator implements different algorithm that will using highest performance algorithm instead. for example spliterator in ArrayList:
public Spliterator<E> spliterator() {
return new ArrayListSpliterator<>(this, 0, -1, 0);
}
Finally, Stream.of() method is a good example to applying Factory Method in interface by static method. it's a factory method for create a stream from an object instance.
The other answers clearly explain the differences between Collection.stream and Stream.of, when to use one or the other, which design patterns are being applied, etc. #Holger goes even further and shows a sample usage of Stream.Builder, which I think is highly under-used.
Here I want to complement the other answers by showing a mixed usage of both Stream.of and Collection.stream methods. I hope that this example would be clear enough to show that even if both Stream.of and Collection.stream are completely different methods, they can also be used together to fulfil a more complex requirement.
Suppose you have N lists, all of them containing elements of the same type:
List<A> list1 = ...;
List<A> list2 = ...;
...
List<A> listN = ...;
And you want to create one stream with the elements of all lists.
You could create a new empty list and add the elements of all the lists into this new list:
int newListSize = list1.size() + list2.size() + ... + listN.size();
List<A> newList = new ArrayList<>(newListSize);
newList.addAll(list1);
newList.addAll(list2);
...
newList.addAll(listN);
Then, you could call stream() on this list and you would be done:
Stream<A> stream = newList.stream();
However, you would be creating an intermediate, pointless list, with the sole purpose of streaming the elements of the original list1, list2, ..., listN lists.
A much better approach is to use Stream.of:
Stream<A> stream = Stream.of(list1, list2, ..., listN)
.flatMap(Collection::stream);
This first creates a stream of lists by enumerating each one of them and then flat-maps this stream of lists into a stream of all lists' elements by means of the Stream.flatMap operation. Thus, Collection.stream is called when flat-mapping the original stream.

How to bind lists like an updating ForEach?

Here is a sample code:
public class Example3 {
class Point {
int x, y; // these can be properties if it matters
}
class PointRepresentation {
Point point; // this can be a property if it matters
public PointRepresentation(Point point) {
this.point = point;
}
}
Example3() {
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = FXCollections.observableArrayList();
points.forEach(point -> representations.add(new PointRepresentation(point)));
}
}
I have a data holder Point and a data representor PointRepresentation. I have a list of points and i would like that for each point in the list there would be an equivalent representation object in the second list. The code I gave works for the initialization but if there is any change later the above will not update.
What I am doing now is using a change listener to synchronize the lists (add and remove elements based on the change object) and it's OK but i am wondering if there's a simpler solution. I was looking for something like a "for each bind" that means: for each element in one list there is one in the other with the specified relation between them [in my case its that constructor]. In pseudocode:
representations.bindForEach(points, point -> new PointRepresentation(point));
Things I looked at: extractors for the list but that sends updates when a property in the objects they hold change and not when the list itself changes. So in my case if x in the point changes i can make an extractor that notifies it. Another thing I looked at is http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ListBinding.html, so maybe a custom binding does it but I don't know if it's simpler.
Also is there a similar solution for arrays instead of lists? i saw the http://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableArray.html as a possibility.
The third-party library ReactFX has functionality for this. You can do
ObservableList<Point> points = FXCollections.observableArrayList();
ObservableList<PointRepresentation> representations = LiveList.map(points, PointRepresentation::new);
This will update representations automatically on add/remove etc changes to points.

Java: Using .stream to transform ArrayList<MyObj> to TreeMap<String, ArrayList<MyObj>>

This is a Java 8 lower-intermediate question:
I have the following code in Java 6:
List <ViewWrapperContentElementTypeProperty> vwPropertyList = getFromDao();
TreeMap <Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties = new TreeMap<Long, ArrayList<ViewWrapperContentElementTypeProperty>> ();
for (ViewWrapperContentElementTypeProperty vwCetP:vwPropertyList)
{
if(null==mappedProperties.get(vwCetP.getContentElementTypeId()))
{
ArrayList<ViewWrapperContentElementTypeProperty> list = new ArrayList<ViewWrapperContentElementTypeProperty>());
list.add(vwCetP);
mappedProperties.put(vwCetP.getContentElementTypeId(), list);
}
else
{
mappedProperties.get(vwCetP.getContentElementTypeId()).add(vwCetP);
}
}
Can I use vwPropertyList.stream().map() to implement this more efficiently?
It seems like you are looking for a grouping by operation. Fortunately, the Collectors class provide a way to do this:
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toCollection;
...
TreeMap<Long, ArrayList<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId,
TreeMap::new,
toCollection(ArrayList::new)));
I used the overloaded version of groupingBy where you can provide a specific map implementation (if you really need a TreeMap).
Also the toList() collector returns a List (which is an ArrayList but it's an implementation details). Since you apparently need to specify a concrete implementation as you want ArrayLists as values, you can do it with toCollection(ArrayList::new).
With regard to using Streams and lambda expressions, of course... This should look like the following:
Map<Long, List<ViewWrapperContentElementTypeProperty>> mappedProperties =
vwPropertyList.stream()
.collect(Collectors.groupingBy(ViewWrapperContentElementTypeProperty::getContentElementTypeId));
Please note that using Stream API methods like above forces using interfaces (Map, List), which is a good practice anyway.
When it comes to performance, it should be roughly the same as using a traditional loop.

Flash Player: Get reference count for variable

I'm looking to build a library that needs to be very careful about memory management. Basically, I have to create a static factory to "disperse" instances of my tool to requesting objects. (I don't have a choice in this matter, I really do have to use a singleton) We'll call that class FooFactory. FooFactory defines a single method, getFoo(key:String):Foo.
getFoo looks in a private static flash.utils.Dictionary object for the appropriate Foo instance, and either lazy-instantiates it, or simply returns it. In any case, FooFactory MUST keep a reference to each Foo instance created, so all Foo instances can be updated by FooFactory using a method called updateFoos():void.
Here is some pseudo-code of what I'm talking about:
public class FooFactory {
private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc
public static function getFoo(key:String):Foo {
//search for the specified instance in the 'foos' dictionary
if (foos[key] != null && foos[key] != undefined) {
return foos[key];
} else {
//create foo if it doesn't exist.
var foo:Foo = new Foo(key);
foos[key] = foo;
return foo;
}
}
public static function updateFoos():void {
for (var key:String in foos) {
if (foos[key] != null && foos[key] != undefined) {
Foo(foos[key]).dispatchEvent(new Event("update"));
}
}
}
}
The actual function and identity of Foo isn't too important.
What IS important is garbage collection in this situation. I created something similar to the above example in the past and had incredible garbage collection issues. (I did use an array rather than a dictionary, which could be part of the problem.) What would happen is that, in my Flex application, modules would never unload, since instances had a reference to a Foo instance which was referenced by the FooFactory, like so: (again, pseudocode)
<?xml version="1.0"?>
<s:Group>
<fx:Script>
<![CDATA[
private static const foo:Foo = FooFactory.getFoo('myfoo');
]]>
</fx:Script>
</s:Group>
What I want to know are the two following things:
Is the pseudo-code above "garbage-collector safe?" IE: Will my modules unload properly and will instances of the Group subclass above get garbage collected?
Is there a way in Flash Player (even in the debug player if need be) that can assist me in counting references so I can test if things are getting garbage collected or not?
I'm aware of the flash.sampler API, but I am not sure as to how to use it to count references.
I don't think that the pattern you presented should give you problems GC-wise.
private static const foo:Foo = FooFactory.getFoo('myfoo');
Here, your module has a reference to a Foo instance. That means that this Foo instance won't be collectable as long as your module is not collectable. The module has a reference to foo, so here foo is reachable (if the module is reachable). That's not true the other way round. Even if foo lives forever, it doesn't have a reference to the module, so it won't pint it down.
Of course there could be other stuff going on to prevent your module from being collectable, but foo is not the culprit here, unless foo gets a reference to the module somehow. For instance, the module adds a listener to foo, which for this matter, is the same as writing:
foo.addReference(this); // where this is your module
The fact that you declare the instance as const shouldn't change things per se, either. It only means that the reference stored cannot be changed at a later point. However, if you want to null out foo at some later point, you can't because that would be reassigning the reference; and you can't reassigning a const reference (you should get a compiler error). Now, this does tie foo to module. As long as your module is alive it will have a reference to foo, so foo won't be collectable.
Regarding this line:
private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc
It looks like you're trying to build some kind of cache. I'm not sure you want to use weak refs here. (I could be wrong here because I'm making an assumption, and they say assumption is the mother of all... mistakes, but I digress)
In any case, the effect of this is that if a module gets a Foo and at some point the module is successfully unloaded (I mean, cleaned up from memory), that instance of foo could be collected, provided that no one else has a ref to it (that is, the only way to reach it is through the dictionary key, but since the keys are weak referenced, this ref will not count for the purposes of the GC).
Regarding your second question, I'd recommend the FlexBuilder/FlashBuilder profiler, if FB is available to you. It's not the most intuitive tool, granted, but with some practice it could be really useful to track memory problems. Basically, it will let you know how many instances of a given class were created, how many of those are still alive, what objects have references to these instances and where were all these objects allocated (an option not checked by default when you launch the profiler, buy very handy to track a leak).
PS
Regarding your comment:
Perhaps the real issue is the static
const reference bound by the Group
instance? If that's an issue, I could
simply abstract Foo to an interface,
then create something called
FooWeakReference which would use a
weak dictionary to reference the
actual Foo object. Thoughts?
Adding this extra layer of indirection only complicates things and makes your code less obvious for no gain here, I think. It's easier to consider the life-cycle of your module and define clear points of initialization and finalization. When it's finalized, make sure you remove any reference to the module added to the foo instance (i.e. if you have added listeners on foo, remove them, etc), so your module is collectable independently of the life-cycle of foo.
As a general rule, whenever a weak reference seems to solve a bug in your app, it's masking another one or covering up for a poor design; there are exceptions (and compromises that have to be made sometimes), but weak refs are abused gratuitously if you ask me; not everyone will agree, I know.
Also, weak-refs open a whole new kind of bugs: what happens if that instance you created lazily vanishes before you can use it or worse, while you are using it? Event listeners that stop working under not deterministically reproducible circumstances (e.g. you added a listener to an object that is gone), possible null references (e.g. you are trying to add a listener to an object that no longer exists), etc, etc. Don't drink the weak reference kool-aid ;).
Addedum
In conclusion, as one last question,
is it true for me to say that no AS3
solution exists for counting
references? I'm building a complete
unit-testing suite for this library
I'm building, and if I could do
something like Assert.assertEquals(0,
getReferenceCount(foo)), that would be
rad.
Well, yes. You can't get the reference count of a given object from Actionscript. Even if it were possible, I'm not sure that would help, because reference counting is only a part of how GC works. The other one is a mark and sweep algorithm. So, if an object has a zero ref-count is collectable, but it could have, say, 3 references and still be collectable. To really determine whether an object is collectable or not, you should really be able to hook into the GC routine, I guess, and that's not possible from AS.
Also, this code will never work.
Assert.assertEquals(0, getReferenceCount(foo)
Why? Here you are trying to query some API to know whether an object is collectable or not. Since you can't know that, let's assume this tells you whether an object has been collected or not. The problem is, foo at that point is either null or not null. If it's null, it's not a valid reference, so you can't get any useful information out of it, for obvious reasons. If it's not null, it's a valid reference to an object, then you can access it and it's alive; so you already know the answer to the question you're asking.
Now, I think I undestand your goal. You want to be able to tell, programatically, if you certain objects are being leaked. Up to some extent that's possible. It involves using the flash.sampler API, as you mentioned in your original question.
I suggest you check out the Flash Preload Profiler by jpauclair:
I haven't used it, but it looks like it could be just as good as the FB profiler for memory watching.
Since this is Actionscript code (and since it's open source), you could to use it for what you want. I just skimmed through the code, but I've been able to get a very simple-minded proof of concept by monkey-patching the SampleAnalyzer class:
There's a lot of other things going on in this tool, but I just modified the memory analizer to be able to return a list of the alive objects.
So, I wrote a simple class that would run this profiler. The idea is that when you create an object, you can ask this class to watch it. This objects' allocation id will be looked up in the allocated objects table maintained by the memory profiler and a handle to it will be stored locally (only the id). This id handle will also be returned for convenience. So you can store this id handle and at a later point, use it to check whether the object has been collected or not. Also, there's a method that returns a list of all the handles you added and another one that returns a list of the added handles that point to live objects. A handle will allow you to access the original object (if it hasn't been collected yet), its class and also the allocation stack trace. (I'm not storing the object itself or the NewObjectSample object to avoid accidentally pinning it down)
Now, this is important: this queries for alive objects. The fact that an object is alive doesn't mean it's not collectable. So, this alone doens't mean there's a leak. It could be alive at this point but still it doesn't mean there's a leak. So, you should combine this with forcing GC to get more relevant results. Also, this could be of use if you are watching objects that are owned by you and not shared with other code (or other modules).
So, here's the code to the ProfileRunner, with some comments.
import flash.sampler.Sample;
import flash.sampler.NewObjectSample;
import flash.utils.Dictionary;
class ProfilerRunner {
private var _watched:Array;
public function ProfilerRunner() {
_watched = [];
}
public function init():void {
// setup the analyzer. I just copied this almost verbatim
// from SamplerProfiler...
// https://code.google.com/p/flashpreloadprofiler/source/browse/trunk/src/SamplerProfiler.as
SampleAnalyzer.GetInstance().ResetStats();
SampleAnalyzer.GetInstance().ObjectStatsEnabled = true;
SampleAnalyzer.GetInstance().InternalEventStatsEnabled = false;
SampleAnalyzer.GetInstance().StartSampling();
}
public function destroy():void {
_watched = null;
}
private function updateSampling(hook:Function = null):void {
SampleAnalyzer.GetInstance().PauseSampling();
SampleAnalyzer.GetInstance().ProcessSampling();
if(hook is Function) {
var samples:Dictionary = SampleAnalyzer.GetInstance().GetRawSamplesDict();
hook(samples);
}
SampleAnalyzer.GetInstance().ClearSamples();
SampleAnalyzer.GetInstance().ResumeSampling();
}
public function addWatch(object:Object):WatchHandle {
var handle:WatchHandle;
updateSampling(function(samples:Dictionary):void {
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.object == object) {
handle = new WatchHandle(newSample);
_watched.push(handle);
}
}
}
});
return handle;
}
public function isActive(handle:WatchHandle):Boolean {
var ret:Boolean;
updateSampling(function(samples:Dictionary):void{
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.id == handle.id) {
ret = true;
break;
}
}
}
});
return ret;
}
public function getActiveWatchedObjects():Array {
var list:Array = [];
updateSampling(function(samples:Dictionary):void {
for each(var handle:WatchHandle in _watched) {
if(samples[handle.id]) {
list.push(handle);
}
}
});
return list;
}
public function getWatchedObjects():Array {
var list:Array = [];
for each(var handle:WatchHandle in _watched) {
list.push(handle);
}
return list;
}
}
class WatchHandle {
private var _id:int;
private var _objectProxy:Dictionary;
private var _type:Class;
private var _stack:Array;
public function get id():int {
return _id;
}
public function get object():Object {
for(var k:Object in _objectProxy) {
return k;
}
return null;
}
public function get stack():Array {
return _stack;
}
public function getFormattedStack():String {
return "\t" + _stack.join("\n\t");
}
public function WatchHandle(sample:NewObjectSample) {
_id = sample.id;
_objectProxy = new Dictionary(true);
_objectProxy[sample.object] = true;
_type = sample.type;
_stack = sample.stack;
}
public function toString():String {
return "[WatchHandle id: " + _id + ", type: " + _type + ", object: " + object + "]";
}
}
And here's a simple demo of how you'd use it.
It initializes the runner, allocates 2 Foo objects and then, after 2 seconds, it finalizes itself. Note that in the finalizer, I'm nulling out one of the Foo objects and finalizing the profiler. There I try to force GC, wait for some time (GC is not synchronous) and then check if these objects are alive. The first object should return false, and the second true. So, this is the place were you'd put your assert. Keep in mind that all of this will only work in a debug player.
So, without any further addo, here's the sample code:
package {
import flash.display.Sprite;
import flash.sampler.NewObjectSample;
import flash.sampler.Sample;
import flash.system.System;
import flash.utils.Dictionary;
import flash.utils.setTimeout;
public class test extends Sprite
{
private var x1:Foo;
private var x2:Foo;
private var _profiler:ProfilerRunner;
private var _watch_x1:WatchHandle;
private var _watch_x2:WatchHandle;
public function test()
{
init();
createObjects();
setTimeout(finalize,2000);
}
public function init():void {
initProfiler();
}
public function finalize():void {
x1 = null;
finalizeProfiler();
}
private function initProfiler():void {
_profiler = new ProfilerRunner();
_profiler.init();
}
private function finalizeProfiler():void {
// sometimes, calling System.gc() in one frame doesn't work
// you have to call it repeatedly. This is a kind of lame workaround
// this should probably be hidden in the profiler runner
var count:int = 0;
var id:int = setInterval(function():void {
System.gc();
count++;
if(count >= 3) {
clearInterval(id);
destroyProfiler();
}
},100);
}
private function destroyProfiler():void {
// boolean check through saved handles
trace(_profiler.isActive(_watch_x1));
trace(_profiler.isActive(_watch_x2));
// print all objects being watched
trace(_profiler.getWatchedObjects());
// get a list of the active objects and print them, plus the alloc stack trace
var activeObjs:Array = _profiler.getActiveWatchedObjects();
for each(var handle:WatchHandle in activeObjs) {
trace(handle);
trace(handle.getFormattedStack());
}
_profiler.destroy();
}
private function createObjects():void {
x1 = new Foo();
x2 = new Foo();
// add them for watch. Also, let's keep a "handle" to
// them so we can query the profiler to know if the object
// is alive or not at any given time
_watch_x1 = _profiler.addWatch(x1);
_watch_x2 = _profiler.addWatch(x2);
}
}
}
import flash.display.Sprite;
class Foo {
public var someProp:Sprite;
}
Alternatively, a more light-weight approach for tracking alive objects is storing them in a weak-referenced dictionary, forcing GC and then checking how many objects are stil alive. Check out this answer to see how this could be implemented. The main difference is that this gives you less control, but maybe it's good enough for your purposes. Anyway, I felt like giving the other idea a shot, so I wrote this object watcher and kind of like the idea.
Since you essentially want weak references, perhaps the best solution would involve one of the weak references available in AS3.
For example, have your method store Dictionaries rather than the actual objects. Something like this:
private var allFoos:Dictionary;
public function getFoo(key:String):Foo {
var f:Foo = _getFoo(key);
if (f == null) {
f = _createFoo(key);
}
return f;
}
private function _createFoo(key:String):Foo {
var f:Foo = new Foo();
var d:Dictionary = new Dictionary(/* use weak keys */ true);
d[f] = key;
allFoos[key] = d;
}
With some intense thinking over the weekend, I believe I figured out what the problem is.
Essentially, we have this scenario:
.--------------.
| APP-DOMAIN 1 |
| [FooFactory] |
'--------------'
|
| < [object Foo]
|
.--------------.
| APP-DOMAIN 2 |
| [MyModule] |
'--------------'
APP-DOMAIN 1 always stays in memory, since it's loaded in the highest app-domain possible: the original compiled code of a SWF. APP-DOMAIN 2 is loaded into and out of memory dynamically and must be able to completely sever itself from APP-DOMAIN 1. According to the genius answer above by Juan Pablo Califano, APP-DOMAIN 2 having a reference to [object Foo] doesn't necessarily tie APP-DOMAIN 2 into memory, though it could become tied into memory by [MyModule] adding an event listener to [object Foo], right?
Okay, so, with this in mind, an overkill solution would be to return a weak-reference-implementation of Foo from the getFoo method, since that's where things need to "break off" in case of "emergency." (Things need to be weak from this perspective so that APP-DOMAIN 1 can be garbage collected completely as it is unloaded.) Again, this is an overkill answer.
However, I do not need to keep a weak-ref to Foo in FooFactory, since FooFactory needs to have a surefire way of getting a hold of each created Foo object. In short, Juan Pablo Califano has the theory completely right, it just needs to be tested in the real world in order to prove everything definitively :)
All of this aside, I believe I have uncovered the real issue behind the scenes that caused a similar library I wrote in the past to never GC. The problem was not in the actual library I wrote, but it seems that it was in a reflection library I was using. The reflection library would "cache" every Class object I threw at it, since my original FooFactory.getFoo method took a Class parameter, rather than a String. Since the library seemed to be hard-referencing every Class object passed into memory, I'm pretty sure that was the memory leak.
In conclusion, as one last question, is it true for me to say that no AS3 solution exists for counting references? I'm building a complete unit-testing suite for this library I'm building, and if I could do something like Assert.assertEquals(0, getReferenceCount(foo)), that would be rad.

Resources