In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

Ben

For example lets say we have:

    public void doThis() {
        final Foo foo = Foo.getInstance();
        ... initialize foo somehow...
        baz(Bar.getInstance(foo)); // adds Bar instance to something
        ... bar will be popped from some list, used, and disposed of...
    }

Can a memory leak occur for this scenario?

I'm just not understanding what a final local variable really means. Does it just mean that the local variable cannot be reassigned, and that is it? Does declaring it final put it somewhere in the java heap/memory such that it's like an instance variable but with a different/unique? Especially since an inner/nested class can use a final local variable, but not a non-final local variable?

user253751

No. If there wasn't a memory leak without final, then there won't be one with final.

The only thing that final does to a local variable (in Java 8)1 is prevent you from assigning to the variable more than once.

1 In Java 7 and earlier, there was another effect which also had nothing to do with memory leaks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Local class can access non-final variable in java 8

From Dev

why does not implementing equals method in Java cause memory leak

From Dev

semantic of local final variable in the Java Memory Model?

From Dev

Memory leak - probably 'cause of to many eventhandlers

From Dev

Will this method cause a memory leak when it throws an exception?

From Dev

Will this method cause a memory leak when it throws an exception?

From Dev

Count how many times a method can be called until it returns nil

From Dev

Java 8 Method references called on a local variable

From Dev

Use non-final local variable in local scope in Java

From Dev

Can the value of a ConditionalWeakTable cause a memory leak?

From Dev

How can memory leaks cause information leak?

From Dev

Assigning an allocated object to some non allocated object can cause the memory leak?

From Dev

java inject local variable in static method

From Dev

Does 'new' cause a memory leak in Java?

From Dev

making an instance before calling non static method in java

From Dev

Why callback method for asynchronous network call can't cause memory leak when activity finished?

From Dev

Android memory leak on static Resource member variable?

From Dev

Java - How is memory leak in Java harmful? How can it possibly get used for a bad cause?

From Dev

Can static method access non-static instance variable?

From Dev

Can static method access non-static instance variable?

From Dev

Static syncronized method and non-static synchronized method can both work on static member variables and this can cause unexpected behaviour

From Dev

Can rebooting many times cause problems

From Dev

Using a non static variable on a static method through an object? Java

From Dev

Calling non-static method in static method in Java (Non-Static Variable Error)

From Dev

Java memory usage - is this syntax correct or it will cause a memory leak?

From Dev

How can a static method change a variable? (Java)

From Dev

ContentObserver onChange() method gets called many times

From Dev

Why can't I create static final variable in psvm Java?

From Dev

Will using form.hide many times cause problems with memory, etc

Related Related

  1. 1

    Local class can access non-final variable in java 8

  2. 2

    why does not implementing equals method in Java cause memory leak

  3. 3

    semantic of local final variable in the Java Memory Model?

  4. 4

    Memory leak - probably 'cause of to many eventhandlers

  5. 5

    Will this method cause a memory leak when it throws an exception?

  6. 6

    Will this method cause a memory leak when it throws an exception?

  7. 7

    Count how many times a method can be called until it returns nil

  8. 8

    Java 8 Method references called on a local variable

  9. 9

    Use non-final local variable in local scope in Java

  10. 10

    Can the value of a ConditionalWeakTable cause a memory leak?

  11. 11

    How can memory leaks cause information leak?

  12. 12

    Assigning an allocated object to some non allocated object can cause the memory leak?

  13. 13

    java inject local variable in static method

  14. 14

    Does 'new' cause a memory leak in Java?

  15. 15

    making an instance before calling non static method in java

  16. 16

    Why callback method for asynchronous network call can't cause memory leak when activity finished?

  17. 17

    Android memory leak on static Resource member variable?

  18. 18

    Java - How is memory leak in Java harmful? How can it possibly get used for a bad cause?

  19. 19

    Can static method access non-static instance variable?

  20. 20

    Can static method access non-static instance variable?

  21. 21

    Static syncronized method and non-static synchronized method can both work on static member variables and this can cause unexpected behaviour

  22. 22

    Can rebooting many times cause problems

  23. 23

    Using a non static variable on a static method through an object? Java

  24. 24

    Calling non-static method in static method in Java (Non-Static Variable Error)

  25. 25

    Java memory usage - is this syntax correct or it will cause a memory leak?

  26. 26

    How can a static method change a variable? (Java)

  27. 27

    ContentObserver onChange() method gets called many times

  28. 28

    Why can't I create static final variable in psvm Java?

  29. 29

    Will using form.hide many times cause problems with memory, etc

HotTag

Archive