Skip to content

Commit 1da95fe

Browse files
muhammadhafifalbusyromazipan
authored andcommitted
Update [Cookbook] - Avoiding-memory-leaks (vuejs#174)
Closes vuejs#140
1 parent 9d721c0 commit 1da95fe

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/v2/cookbook/avoiding-memory-leaks.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
2-
title: Avoiding Memory Leaks
3-
type: cookbook
4-
order: 10
2+
judul: Menghindari Kebocoran Memori
3+
tipe: cookbook
4+
urutan: 10
55
---
66

7-
## Introduction
7+
## Pengantar
88

9-
If you are developing applications with Vue, then you need to watch out for memory leaks. This issue is especially important in Single Page Applications (SPAs) because by design, users should not have to refresh their browser when using an SPA, so it is up to the JavaScript application to clean up components and make sure that garbage collection takes place as expected.
9+
Jika Anda mengembangkan aplikasi dengan Vue, maka Anda harus berhati-hati terhadap kebocoran memori. Masalah ini sangat penting dalam Aplikasi Halaman Tunggal (SPA) karena secara desain, pengguna tidak perlu me-refresh browser mereka ketika menggunakan SPA, jadi itu tergantung pada aplikasi JavaScript untuk membersihkan komponen dan memastikan bahwa pengumpulan sampah berlangsung sebagai diharapkan.
1010

11-
Memory leaks in Vue applications do not typically come from Vue itself, rather they can happen when incorporating other libraries into an application.
11+
Kebocoran memori dalam aplikasi Vue biasanya tidak berasal dari Vue itu sendiri, melainkan dapat terjadi ketika menggabungkan pustaka lain ke dalam suatu aplikasi.
1212

13-
## Simple Example
13+
## Contoh Sederhana
1414

15-
The following example shows a memory leak caused by using the [Choices.js](https://github.com/jshjohnson/Choices) library in a Vue component and not properly cleaning it up. Later, we will show how to remove the Choices.js footprint and avoid the memory leak.
15+
Contoh berikut memperlihatkan kebocoran kehabisan memori yang disebabkan oleh penggunaan pustaka [Choices.js](https://github.com/jshjohnson/Choices) di komponen Vue dan tidak membersihkannya dengan benar. Nanti, kami akan menunjukkan cara menghapus jejak Choices.js dan menghindari kebocoran memori.
1616

17-
In the example below, we load up a select with a lot of options and then we use a show/hide button with a [v-if](/v2/guide/conditional.html) directive to add it and remove it from the virtual DOM. The problem with this example is that the `v-if` directive removes the parent element from the DOM, but we did not clean up the additional DOM pieces created by Choices.js, causing a memory leak.
17+
Pada contoh di bawah ini, kami memuatkan pilihan dengan banyak opsi dan kemudian kami menggunakan tombol show / hide dengan arahan [v-if](/v2/guide/conditional.html) untuk menambahkan dan menghapusnya dari DOM virtual. Masalah dengan contoh ini adalah bahwa arahan `v-if` menghapus elemen induk dari DOM, tetapi kami tidak membersihkan potongan DOM tambahan yang dibuat oleh Choices.js, yang menyebabkan kebocoran memori.
1818

1919
```html
2020
<link rel='stylesheet prefetch' href='https://joshuajohnson.co.uk/Choices/assets/styles/css/choices.min.css?version=3.0.3'>
@@ -75,16 +75,15 @@ new Vue({
7575
}
7676
})
7777
```
78-
79-
To see this memory leak in action, open this [CodePen example](https://codepen.io/freeman-g/pen/qobpxo) using Chrome and then open the Chrome Task Manager. To open the Chrome Task Manager on Mac, choose Chrome Top Navigation > Window > Task Manager or on Windows, use the Shift+Esc shortcut. Now, click the show/hide button 50 or so times. You should see the memory usage in the Chrome Task Manager increase and never be reclaimed.
78+
Untuk melihat kebocoran memori ini dalam aksi, buka [contoh CodePen](https://codepen.io/freeman-g/pen/qobpxo) menggunakan Chrome dan kemudian buka Chrome Task Manager. Untuk membuka Chrome Task Manager di Mac, pilih Chrome Top Navigation> Window> Task Manager atau di Windows, gunakan pintasan Shift + Esc. Sekarang, klik tombol show / hide 50 atau lebih kali. Anda akan melihat penggunaan memori dalam Chrome Task Manager meningkat dan tidak pernah direklamasi.
8079

8180
![Memory Leak Example](/images/memory-leak-example.png)
8281

83-
## Resolving the Memory Leak
82+
## Mengatasi Kebocoran Memori
8483

85-
In the above example, we can use our `hide()` method to do some clean up and solve the memory leak prior to removing the select from the DOM. To accomplish this, we will keep a property in our Vue instance’s data object and we will use the [Choices API’s](https://github.com/jshjohnson/Choices) `destroy()` method to perform the clean up.
84+
Pada contoh di atas, kita dapat menggunakan metode `hide ()` untuk melakukan pembersihan dan menyelesaikan kebocoran memori sebelum menghapus pemilihan dari DOM. Untuk mencapai hal ini, kami akan menyimpan properti dalam objek data instance Vue kami dan kami akan menggunakan metode [Choices API](https://github.com/jshjohnson/Choices) `destroy ()` untuk melakukan pembersihan.
8685

87-
Check the memory usage again with this [updated CodePen example](https://codepen.io/freeman-g/pen/mxWMor).
86+
Periksa penggunaan memori lagi dengan [contoh CodePen yang diperbarui](https://codepen.io/freeman-g/pen/mxWMor).
8887

8988
```js
9089
new Vue({
@@ -130,31 +129,31 @@ new Vue({
130129
})
131130
```
132131

133-
## Details about the Value
132+
## Rincian tentang Nilai
134133

135-
Memory management and performance testing can easily be neglected in the excitement of shipping quickly, however, keeping a small memory footprint is still important to your overall user experience.
134+
Manajemen memori dan pengujian kinerja dapat dengan mudah diabaikan dalam kegembiraan pengiriman dengan cepat, namun, menjaga jejak memori yang kecil masih penting untuk keseluruhan pengalaman pengguna Anda.
136135

137-
Consider the types of devices your users may be using and what their normal flow will be. Could they use memory constrained laptops or mobile devices? Do your users typically do lots of in-application navigation? If either of these are true, then good memory management practices can help you avoid the worst case scenario of crashing a user’s browser. Even if neither of these are true, you can still potentially have degradation of performance over extended usage of your app if you are not careful.
136+
Pertimbangkan jenis perangkat yang mungkin digunakan pengguna Anda dan seperti apa aliran normalnya. Bisakah mereka menggunakan laptop atau perangkat seluler yang dibatasi memori? Apakah pengguna Anda biasanya melakukan banyak navigasi dalam aplikasi? Jika salah satu dari ini benar, maka praktik manajemen memori yang baik dapat membantu Anda menghindari skenario terburuk yang menyebabkan browser pengguna rusak. Bahkan jika tidak satu pun dari ini benar, Anda masih dapat berpotensi mengalami penurunan kinerja karena penggunaan aplikasi yang diperpanjang jika Anda tidak berhati-hati.
138137

139-
## Real-World Example
138+
## Contoh Dunia Nyata
140139

141-
In the above example, we used a `v-if` directive to illustrate the memory leak, but a more common real-world scenario happens when using [vue-router](https://router.vuejs.org/en/) to route to components in a Single Page Application.
140+
Dalam contoh di atas, kami menggunakan arahan `v-if` untuk menggambarkan kebocoran memori, tetapi skenario dunia nyata yang lebih umum terjadi ketika menggunakan [vue-router](https://router.vuejs.org/en/) untuk merutekan ke komponen dalam Aplikasi Halaman Tunggal.
142141

143-
Just like the `v-if` directive, `vue-router` removes elements from the virtual DOM and replaces those with new elements when a user navigates around your application. The Vue `beforeDestroy()` [lifecycle hook](/v2/guide/instance.html#Lifecycle-Diagram) is a good place to solve the same sort of issue in a `vue-router` based application.
142+
Seperti halnya arahan `v-if`,` vue-router` menghapus elemen dari DOM virtual dan menggantikannya dengan elemen baru saat pengguna menavigasi aplikasi Anda. Vue `beforeDestroy ()` [hook siklus hidup](/v2/guide/instance.html # Lifecycle-Diagram) adalah tempat yang baik untuk memecahkan masalah yang sama dalam aplikasi berbasis `vue-router`.
144143

145-
We could move our clean up into the `beforeDestroy()` hook like this:
144+
Kita dapat memindahkan pembersihan kita ke kait `beforeDestroy ()` seperti ini:
146145

147146
```js
148147
beforeDestroy: function () {
149148
this.choicesSelect.destroy()
150149
}
151150
```
152151

153-
## Alternative Patterns
152+
## Pola Alternatif
154153

155-
We have discussed managing memory when removing elements, but what if you intentionally want to preserve state and keep elements in memory? In this case, you can use the built-in component [keep-alive](/v2/api/#keep-alive).
154+
Kami telah membahas mengelola memori saat melepas elemen, tetapi bagaimana jika Anda dengan sengaja ingin mempertahankan status dan menyimpan elemen dalam memori? Dalam hal ini, Anda dapat menggunakan komponen bawaan [keep-hidup](/v2/api/#keep-hidup).
156155

157-
When you wrap a component with `keep-alive`, its state will be preserved and therefore kept in memory.
156+
Ketika Anda membungkus komponen dengan `keep-hidup`, statusnya akan dipertahankan dan karenanya disimpan dalam memori.
158157

159158
```html
160159
<button @click="show = false">Hide</button>
@@ -163,16 +162,16 @@ When you wrap a component with `keep-alive`, its state will be preserved and the
163162
<my-component v-if="show"></my-component>
164163
</keep-alive>
165164
```
166-
This technique can be useful to improve user experience. For example, imagine a user starts entering comments into a text input and then decides to navigate away. If the user then navigated back, their comments would still be preserved.
165+
Teknik ini dapat bermanfaat untuk meningkatkan pengalaman pengguna. Misalnya, bayangkan seorang pengguna mulai memasukkan komentar ke dalam input teks dan kemudian memutuskan untuk bernavigasi. Jika pengguna kemudian menavigasi kembali, komentar mereka akan tetap dipertahankan.
167166

168-
Once you use keep-alive, then you have access to two more lifecycle hooks: `activated` and `deactivated`. If you do want to clean up or change data when a keep-alive component is removed, you can do so in the `deactivated` hook.
167+
Setelah Anda menggunakan keep-hidup, maka Anda memiliki akses ke dua kait siklus hidup lagi: `diaktifkan` dan` dinonaktifkan`. Jika Anda ingin membersihkan atau mengubah data saat komponen yang tetap hidup dihapus, Anda dapat melakukannya di kait `dinonaktifkan`.
169168

170169
```js
171170
deactivated: function () {
172171
// remove any data you do not want to keep alive
173172
}
174173
```
175174

176-
## Wrapping Up
175+
## Kesimpulan
177176

178-
Vue makes it very easy to develop amazing, reactive JavaScript applications, but you still need to be careful about memory leaks. These leaks will often occur when using additional 3rd Party libraries that manipulate the DOM outside of Vue. Make sure to test your application for memory leaks and take appropriate steps to clean up components where necessary.
177+
Vue emmudahkan untuk mengembangkan aplikasi JavaScript reaktif yang luar biasa, tetapi Anda masih harus berhati-hati dengan kebocoran memori. Kebocoran ini akan sering terjadi ketika menggunakan perpustakaan Pihak ke-3 tambahan yang memanipulasi DOM di luar Vue. Pastikan untuk menguji aplikasi Anda untuk kebocoran memori dan mengambil langkah-langkah yang tepat untuk membersihkan komponen jika perlu.

0 commit comments

Comments
 (0)