0

I'm sure this question (or something similar) has been asked elsewhere, but I couldn't find it, so this is the (slightly simplified) flow for my android app:

Actvity A: Parses a json file and stores resulting objects in a List (let's call it "raw"). Once done, runs Activty B.

Activity B: A search screen. Button click runs Activity C

Activity C: Filters the list made in Activity A according to search parameters from Activity B. Adds the filtered objects to another list (for use in Activity D - let's call that "filtered") and creates another object based on their values (for use in Activity E - let's call that bigObj). Just in case how bigObj is created is relevant, say I start with objects like this:

filtered = [{"name":"Dave", "age":"32", "job":"boss"},
{"name":"Dave", "age":"32", "job":"worker"},
{"name":"Dave", "age":"24", "job":"boss"},
{"name":"James", "age":"26", "job":"boss"},
{"name":"James", "age":"26", "job":"boss"},
{"name":"James", "age":"65", "job":"boss"}]

then I end up with an object like this:

   bigObj = {"Dave":{"32":[{"name":"Dave", "age":"32", "job":"boss"},
                {"name":"Dave", "age":"32", "job":"worker"}],
        "24":[{"name":"Dave", "age":"24", "job":"boss"}]
            },
        "James":{"26":[{"name":"James", "age":"26", "job":"boss"},
                {"name":"James", "age":"26", "job":"boss"}],
        "65":[{"name":"James", "age":"65", "job":"boss"}]
            }
}

And all three of these get saved via setters on the application activity.

Later, depending on user interaction in either Activity D or E, the contents of the smaller object - say for example {"name":"Dave", "age":"32", "job":"boss"} - get displayed to the user in Activity F.

Which all works fine. But there's obvious repetition. If an object makes it through the filter, it appears in all 3 places. And being that the objects contain alot more information than is actually needed in Activities D or E, there seems to be alot of data getting passed around needlessly. Which brings me to my question:

Say that "raw" has 100 objects in it, and 50 pass the filter. Are there now 150 objects in memory, or are the 50 that get put into "filtered" just references to the originals?

Because the other way to do it (it would seem) would be simply to use "filtered" to store the indices that the objects appear at in "raw" (and retrieve the actual objects on the fly from "raw" in Activity D) and then, when making "bigObj" to make new "mini objects" that contain just the information that Activity E needs - from the above example the name and age values, plus the object's position in "raw", which can then be used to retrieve the object in Activity F.

But then if the objects are just stored by reference in "filtered" and "bigObj", making a whole set of "mini objects" would actually be counter productive, and end up chewing into memory rather than saving it.

Thanks for any thoughts. I suspect that this question could have been asked alot more simply, but I don't know enough to know what's relevant and what's not...

4
  • Implementation specific. But if you use the references from the original list, then only 100 will exist. If you create new copies via the new keyword, then you'll have 150. Commented Feb 16, 2014 at 15:37
  • Remember that, no matter how you do it, everything in Java is passed by value. Commented Feb 16, 2014 at 15:50
  • What I mean by that is that if you pass an object, you pass a copy of it's reference (strictly, a copy of a copy). objectA created in class A and it's reference passed to class B, will be the same object reference. == would evaluate to true. Commented Feb 16, 2014 at 15:59
  • thanks, Gabe and Simon.This is what I was hoping. Commented Feb 16, 2014 at 17:09

1 Answer 1

1

---Android part Begin---
All activities within single application share same virtual memory. Which means any data from any activity would be available to any other activity without any restrictions.
---Android part End---

Java doesn't keeps objects in variables. Object variable in Java similar to object variable in C++ - it is a pointer to memory that contains object data.

In Java arguments of methods passed by value only. That means any primitive variable would be copied and copy will be passed to method. But object variable is not a primitive type variable, so method will got a copy of reference to object, which means actual object data never be copied, until you copy it yourself to newly created object.

Variables (The Java Tutorials > Learning the Java Language > Language Basics)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, Valentin. I later found a nice explanation here, too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.