Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I want to run a method at program shutdown which requires a temp variable to an object reference to be stored. I just so happen to have a class variable of the same type that I've been using this whole time to store the current value, and it's no longer needed.
I know it's probably not best practice, but if I want to save the extra step and write an object reference as a placeholder while my program does some housekeeping, would it be faster to write it to this variable which already exists than to declare a fresh temp from scratch? The static variable is in another class, if that makes a difference.
"So fast it doesn't matter," and either way an object gets written. I'm just trying to understand how these things work in memory. The glaring issue with a new variable seems to me that it's a declaration which has to allocate some space on the disk.
Reusing some unrelated class variable is the worst choice. It makes your code unreadable and unmaintainable.
Allocating an additional reference variable on the stack also doesn't take time - it doesn't matter whether your method has 5 or 50 local variables. Allocating local variables means just adding/subtracting some (constant) value from the stack pointer.
Forget about allocating space on the disk - the state of your running program is normally not written to the disk at all.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to avoid array zeroing/initialization in Java?
I need to quickly allocate fresh byte arrays of fixed length that will completely be filled with predefined data. Therefore, I don't need the JVM to automatically zero the array on instantiation and I certainly don't mind if the array contains junk. All I need is constant time array allocation, which unfortunately becomes O(n) due to the mentioned zeroing issue.
Would using unsafe help?
JVM always initializes arrays. But you can reuse the same array and it will be initialized once.
The class sun.misc.Unsafe is officially undocumented.
From http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
Avoid initialization
allocateInstance method can be useful when you need to skip object
initialization phase or bypass security checks in constructor or you
want instance of that class but don't have any public constructor.
Some resources indicate its removal from Java 9.
What kind of application do you have that array initialization became a performance bottleneck?
In response to "Sadly I can't because these arrays are held on to for an undefined period of time by an asynchronous networking library. Reusing them results in overwriting partially unsent messages.":
Then use a pool and reuse only arrays that are not currently in use. You will have to manage that, though. So, is array creation really that much of an issue?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is it considered bad practice to use a method that returns an object as a parameter? I have the following method:
public static boolean checkForBlanks(PageDescription pageDesc){
// do some stuff
}
And this method is called within another method:
checkForBlanks(aPage.getPageDescription());
Is it better practice to create a PageDescription object using the getPageDescription method first, then pass that as a parameter(The checkForBlanks methods will check a number of fields on this object) or is there any advantage to be had with the current implementation?
The PageDescription object is not used anywhere else in the calling method, so it is used explicitly in the checkForBlanks method only.
Since the instance returned by aPage.getPageDescription() is only used once within your method, there is no advantage in assigning aPage.getPageDescription() to a variable prior to passing it to checkForBlanks().
In such cases it is simply a matter of preference. I don't like the concept of tieing the hands behind the back in order to fit some misguided concept of code.
In general I prefer the idea of good looking code is good.
checkStuff(storage.getEntry()); is a easy line of good that is clear and everyone can easily follow it.
World.getSomething().doStuff(Storages.getStorage(key).getEntry()); is for example less clear to read.
Keep it clean and simple. That is the best way to go by it. If you look a week later at your code and have no clue what the heck you did, then you did something wrong.
Good code is in the end:
Have fun coding.
you should go with this (good);
checkForBlanks(aPage.getPageDescription());
instead of this (bad):
PageDescription pageDescription = aPage.getPageDescription();
checkForBlanks(pageDescription);
pageDescription in the second variant is an unnecessary local variable because there is no value read from afterwards. So you blow up your code (okay, just one line), and it will cost CPU time and Memory consumption for creating the object. Both is not needed.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Thinking about this question, I don't think it would be bad since object references only take up 4 bytes of memory (in a 32-bit JVM), but intuitively I feel like I'm doing something wrong when I have many (+100) references to the same object. This usually happens when I create a certain class +100 times and each need to hold the reference.
I know I can re-design my code in most cases to avoid this, but sometimes its much easier to just keep the reference within each object.
Anyway, is it bad to have many references to the same object?
Having many references to the same object is unavoidable, but has no dissadvantage IMHO.
Every Class has a reference to it from every instance of that class. Each class loader has a reference from every class. The empty String is often the most referenced object with tens of thousands of references being common.
I know I can re-design my code in most cases to avoid this, but sometimes its much easier to just keep the reference within each object.
I suggest you do what you believe is simplest and clearest and this will be easiest to maintain.
Thinking about this question, I don't think it would be bad since object references only take up 4 bytes of memory (in a 32-bit JVM), but intuitively I feel like I'm doing something wrong when I have many (+100) references to the same object.
From a performance/resource utilization standpoint, references are waaaaaaaaaaaaaaaay more efficient than creating and destroying objects. Lots of ittybitty objects can fragment the heap and tax the memory manager/garbage collector. This is why it's usually worth the effort to make immutable objects singletons. Construction of even small objects in Java is more expensive than using references.
Most programs won't notice any significant difference, but some will.
This usually happens when I create a certain class +100 times and each need to hold the reference.
If every instance of a class references that object, use a static rather than instance variable to store the reference. You can use a static initializer to allocate it, or create a factory method to instantiate objects of the class and have the factory method allocate the referenced object the first time it is invoked.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How is memory allocated for a variable declaration in Python vs. Java? Without a compilation step how does an interpreted language know how much memory is needed for a variable?
Before being usable, variables must be allocated a memory location and then initialized--whether in Java, Python, or even Logo.
Declare means that you make that variable come to life with a specific snippet of code, with (using Java as an example) something like
int i;
Person p;
These are declared, but not initialized. They are now assigned a location in memory--which, in some languages, may be ever-changing, both in location and size. But regardless, there is now some physical location in memory that the runtime environment can query, to retrieve the variable (either an indirect pointer to it, or the actual location itself).
Now that it has an empty "box" in which to go, it must be filled, which is to say it must be "initialized":
i = 3;
p = new Person();
Now there is something concrete in the box. It is ready for use. Attempting to use it before its initialized will result (in Java) in a NullPointerException.
Some languages require you to declare variables, in order to explicitly allocate memory for it (location and/or size). Some languages do this memory-allocation for you. As stated in the comments to both your question and this answer, there's a lot of variation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am programming something in Java and in my main-class, i have a static class variable called "file-system".
Now i want to give the variable file-system to a method from another class, which needs some information from file-system, and modifies it in some ways.
Now I am wondering: Do i have to return the new file-system from my method that is modifying it? Or is the modification taken over to my file-system in main-class due to the "static" attribute?
I don't know what else to write here, but i cannot post my question yet because editor tells me it does not serve the quality standards. Seriously, who had the idea to do a quality analyzing tool which is completely messed up and does not even let me post a single question?
If the static variable is also declared public then your other class can modify it "in place" - just reference the variable:
MyClass.fileSystem = ....
It depends on the type of your static variable. Intrinsic types (such as int, char, boolean) are copied, no matter how static it is, while for objects the reference (or a pointer if it's more clear to you) is kept.