Does not using a return variable cause memory leak [closed]
Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
java memory-leaks
closed as unclear what you're asking by Nikolas, Sotirios Delimanolis, M-M, jww, Machavity Jan 4 at 14:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
java memory-leaks
closed as unclear what you're asking by Nikolas, Sotirios Delimanolis, M-M, jww, Machavity Jan 4 at 14:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08
add a comment |
Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
java memory-leaks
Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
java memory-leaks
java memory-leaks
asked Jan 2 at 20:04
Nelson MatiasNelson Matias
1261111
1261111
closed as unclear what you're asking by Nikolas, Sotirios Delimanolis, M-M, jww, Machavity Jan 4 at 14:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Nikolas, Sotirios Delimanolis, M-M, jww, Machavity Jan 4 at 14:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08
add a comment |
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08
add a comment |
2 Answers
2
active
oldest
votes
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
Ifres
is never used, then it will become subject to garbage collection when it goes out of scope.
– Joe C
Jan 2 at 20:30
add a comment |
No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
Ifres
is never used, then it will become subject to garbage collection when it goes out of scope.
– Joe C
Jan 2 at 20:30
add a comment |
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
Ifres
is never used, then it will become subject to garbage collection when it goes out of scope.
– Joe C
Jan 2 at 20:30
add a comment |
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
edited Jan 2 at 20:29
answered Jan 2 at 20:10
Joe CJoe C
11.7k62543
11.7k62543
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
Ifres
is never used, then it will become subject to garbage collection when it goes out of scope.
– Joe C
Jan 2 at 20:30
add a comment |
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
Ifres
is never used, then it will become subject to garbage collection when it goes out of scope.
– Joe C
Jan 2 at 20:30
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
Thank you for the answer. Going by your second part, if I were to return an Int Object, as in "return new Integer(a+b);" and store the result as "Integer res = sum(2,3);" and never use "res". Would this be subject to memory leaks?
– Nelson Matias
Jan 2 at 20:28
1
1
If
res
is never used, then it will become subject to garbage collection when it goes out of scope.– Joe C
Jan 2 at 20:30
If
res
is never used, then it will become subject to garbage collection when it goes out of scope.– Joe C
Jan 2 at 20:30
add a comment |
No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?
add a comment |
No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?
add a comment |
No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?
No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?
answered Jan 2 at 20:25
Y. ArcherY. Archer
1
1
add a comment |
add a comment |
No. The return value will not be stored in any variable. Hence, no memory is allocated for it.
– Aziz
Jan 2 at 20:05
You could clarify a bit, why are you asking this? Java has garbage collection, so only way to leak memory is to leave static (or otherwise long-lived) variables (often in containers) pointing to allocated objects, so they can't be freed even though they are not used ever again.
– hyde
Jan 2 at 20:07
Further, this function returns primitive type, which won't have any allocation which needs to be freed. What kind of memory leak are you worried about?
– hyde
Jan 2 at 20:08