Does not using a return variable cause memory leak [closed]












-1















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);









share|improve this 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


















-1















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);









share|improve this 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
















-1












-1








-1








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);









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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





















  • 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














2 Answers
2






active

oldest

votes


















4














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.






share|improve this answer


























  • 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





    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



















-1














No, Java has the garbage collector to help manage the memory.
Hope this could help. What is the garbage collector in Java?






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    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.






    share|improve this answer


























    • 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





      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
















    4














    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.






    share|improve this answer


























    • 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





      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














    4












    4








    4







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





      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



















    • 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





      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

















    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













    -1














    No, Java has the garbage collector to help manage the memory.
    Hope this could help. What is the garbage collector in Java?






    share|improve this answer




























      -1














      No, Java has the garbage collector to help manage the memory.
      Hope this could help. What is the garbage collector in Java?






      share|improve this answer


























        -1












        -1








        -1







        No, Java has the garbage collector to help manage the memory.
        Hope this could help. What is the garbage collector in Java?






        share|improve this answer













        No, Java has the garbage collector to help manage the memory.
        Hope this could help. What is the garbage collector in Java?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 20:25









        Y. ArcherY. Archer

        1




        1















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter