Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Method Signatures

Jesse Daniels
Jesse Daniels
810 Points

Memory Management regarding Calling a Method vs. a Simple Operation

In this video, Craig recommends that we call the load(int num) method inside of the load() method in order to avoid repeating ourselves.

What I was wondering is which choice, method call or simple operation, takes up more memory. I realize that worrying about memory management on this level of coding is a bit silly, as it's basically a non-issue. But I figure that calling another method takes up slightly more memory than just using a simple mathematical operation such as +=1, -=1, et cetera or a simple assignment operation (mPezCount=MAX_PEZ;). Because the program needs to find the code of the method, execute it, and then do assignments, rather than just simply assigning the provided value, or doing the simple math operations and then assigning the value. Am I right about this, or does either take up about the same amount of memory? It just seems to me that it necessarily takes up more memory due to additional lines of code.

Not repeating yourself normally leads to better memory management, but in this case, aren't we using more memory? In this simple program it doesn't really matter, but if this were a huge complex program where the code was called hundreds or thousands of times, wouldn't it make a difference?

Perhaps I'm simply ignorant of how Java interacts with memory exactly, so if anyone could provide information on how Java handles either situation memory-wise, that would be great.

1 Answer

Raymond Wach
Raymond Wach
7,961 Points

I can't get super technical in my answer because I am not that well-versed in the low level memory management models of the JVM, but I will point out two details that may change your opinion. First of all, the simple statements x += y and x -= y (etc.) are still doing assignments to x so that step is pretty much unavoidable no matter how much we optimize (ultimately we're talking about saving a value to use later so we have to do that somewhere). Secondly, there's another reason not to repeat yourself that doesn't have to do with an immediate impact to performance. By calling load(int num) inside load() with a preset value, we are keeping the logic of the load function in one place and keeping the "default" value of num in one place. This means that as specifications and requirements change, the chances of introducing errors are, in a sense, cut in half because there is only one location requiring modification. So not only is this technique DRY, it is also applying the Single Responsibility Principle.

That being said, you are right that there is a cost to every function call and that there is some threshold where performance takes a hit because of the passing of parameters, but in modern environments code readability and modifiability are generally much more dominant concerns because you are likely to have other people reading your code before you have people complaining about it's performance.

Jesse Daniels
Jesse Daniels
810 Points

Fair enough, that's a good point. Thanks for answering.