When you're working with your own objects, you might need to create copies.
- [Instructor] So, that's okay for collections,…but what should we do about other types of objects?…Let's suppose that we want to add a method…into our customer records that allows us…to retrieve a single object from the collection,…so we'll do that down here.…We'll call it getCustomerByName,…and it'll return a customer object,…(typing)…and so we'll pass in a string,…which is the customer's name that we're looking for.…
Now, when we created the map, the keys…are actually set to be the customers' names.…So, all I need to do to implement this method…is to return (typing) this.records.get,…and the value that is associated with…the key of the customer's name.…So, once again, what we've created here is some risky code.…We've provided an escaping reference to the customer object.…A client that calls getCustomerByName…is going to get a reference to the customer,…and can now call any of the methods…on this customer object that change its values.…
For example, in customer there's a set name method,…and it could call that and change the customer's name.…
Author
Released
12/19/2018- How memory works in Java
- Passing variables by value
- How objects are passed
- What are escaping references?
- How to avoid escaping references with collections and custom objects
- Garbage collection and generation sizes
- Detecting soft leaks
- Choosing a garbage collector
- Tuning a virtual machine
- Fixing a memory leak
Skill Level Intermediate
Duration
Views
Related Courses
-
Windows Performance Toolkit: Detecting Memory Leaks
with Thomas Pantels53m 32s Intermediate -
Learning Java Threads
with Peggy Fisher1h 9m Advanced
-
Introduction
-
Introduction1m 45s
-
-
1. How Memory Works in Java
-
Why memory is important1m 3s
-
The role of the stack5m 11s
-
The role of the Heap9m 36s
-
-
2. Values and References
-
Passing variables by value5m 26s
-
How objects are passed3m 30s
-
The final keyword6m 14s
-
Memory exercise1m 15s
-
Exercise walkthrough7m 18s
-
-
3. Escaping References
-
Exercise walkthrough10m 54s
-
4. Introduction to Garbage Collection
-
String pools9m 14s
-
Garbage eligibility5m 43s
-
-
5. Generational Garbage Collection
-
Mark and sweep2m 37s
-
-
6. Tuning the Virtual Machine
-
Heap size5m 16s
-
PermGen size1m 12s
-
Choosing a garbage collector4m 51s
-
-
7. Hunting for a Memory Leak
-
Monitoring the application8m 47s
-
Fixing a memory leak10m 39s
-
Conclusion
-
Course summary22s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Avoiding escaping references with custom objects, part 1