├── LICENSE ├── mod.ts ├── .vscode └── settings.json ├── gpu.ts ├── lock.ts ├── resources.ts ├── egg.json ├── examples └── mem_usage.ts ├── drop_test.ts ├── README.md └── drop.ts /LICENSE: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | export * from "./lock.ts"; 2 | export * from "./drop.ts"; 3 | export * from "./resources.ts"; 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "deno.enable": true, 3 | "deno.unstable": false, 4 | "deno.lint": true 5 | } 6 | -------------------------------------------------------------------------------- /gpu.ts: -------------------------------------------------------------------------------- 1 | import { drop } from "./mod.ts"; 2 | 3 | const adapter = await navigator.gpu.requestAdapter(); 4 | const device = await adapter?.requestDevice(); 5 | 6 | console.log(Deno.resources()); 7 | 8 | drop(adapter); 9 | 10 | drop(device); 11 | 12 | console.log(Deno.resources()); 13 | -------------------------------------------------------------------------------- /lock.ts: -------------------------------------------------------------------------------- 1 | // Every release for `drop` is locked to a specific version 2 | // of Deno. Hence, this source is guranteed to work with 3 | // the below specified version. 4 | // 5 | // This is needed because tests and resource names are not part 6 | // of the Deno public API and can change/break in other version. 7 | export const DENO_VERSION = "1.12.0"; 8 | -------------------------------------------------------------------------------- /resources.ts: -------------------------------------------------------------------------------- 1 | export enum Category { 2 | Fetch, 3 | WebGPU, 4 | Encoding, 5 | } 6 | 7 | export interface Resource { 8 | name: string; 9 | category: Category; 10 | } 11 | 12 | export const FetchResource: Resource = { 13 | name: "fetchResponseBody", 14 | category: Category.Fetch, 15 | }; 16 | 17 | export const TextDecoderResource: Resource = { 18 | name: "textDecoder", 19 | category: Category.Encoding, 20 | }; 21 | 22 | export const GPUAdapterResource: Resource = { 23 | name: "webGPUAdapter", 24 | category: Category.WebGPU, 25 | }; 26 | 27 | export const Resources: Resource[] = [FetchResource]; 28 | -------------------------------------------------------------------------------- /egg.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://x.nest.land/eggs@0.3.8/src/schema.json", 3 | "name": "drop", 4 | "entry": "./mod.ts", 5 | "description": "`drop` unwanted Deno resources out of memory. yeet.", 6 | "homepage": "https://github.com/littledivy/drop", 7 | "version": "1.12.0", 8 | "releaseType": null, 9 | "unstable": false, 10 | "unlisted": false, 11 | "files": [ 12 | "*.ts", 13 | "README.md", 14 | "LICENSE", 15 | "examples/**/*" 16 | ], 17 | "ignore": [ 18 | ".vscode" 19 | ], 20 | "checkFormat": false, 21 | "checkTests": false, 22 | "checkInstallation": false, 23 | "check": true 24 | } 25 | -------------------------------------------------------------------------------- /examples/mem_usage.ts: -------------------------------------------------------------------------------- 1 | import { drop, FetchResource } from "../mod.ts"; 2 | 3 | async function addNewResource() { 4 | // XXX: Does not automatically spin up a server for this run. 5 | // You will have to do it yourself before executing this. 6 | // Ex: `python -m SimpleHTTPServer` 7 | return await fetch("http://localhost:8000/images.png"); 8 | } 9 | 10 | for (let i = 0; i < 1e4; i++) { 11 | await addNewResource(); 12 | // Uncomment the below line to see how much memory it normally takes. 13 | // (depends on size of file being fetched) 14 | // 15 | // With `drop` : 17.8mb 16 | // Without `drop` (default) : 200.1mb 17 | drop(FetchResource); 18 | } 19 | -------------------------------------------------------------------------------- /drop_test.ts: -------------------------------------------------------------------------------- 1 | import { assertEquals } from "https://deno.land/std@0.89.0/testing/asserts.ts"; 2 | 3 | import { drop } from "./drop.ts"; 4 | import { FetchResource, TextDecoderResource } from "./resources.ts"; 5 | 6 | const assertResources = (expected: Deno.ResourceMap) => { 7 | const resources = Deno.resources(); 8 | assertEquals(resources, expected); 9 | }; 10 | 11 | const defaultResources: Deno.ResourceMap = { 12 | "0": "stdin", 13 | "1": "stdout", 14 | "2": "stderr", 15 | }; 16 | 17 | Deno.test({ 18 | name: "dropTest", 19 | fn: async (): Promise => { 20 | assertResources(defaultResources); 21 | 22 | await fetch("https://google.com"); 23 | // rid: 8 for `fetchResponseBody` indicates that there 24 | // were multiple resources created by `fetch` internally. 25 | // This might change in future versions of Deno. 26 | assertResources({ ...defaultResources, "8": "fetchResponseBody" }); 27 | 28 | drop(FetchResource); 29 | assertResources(defaultResources); 30 | }, 31 | }); 32 | 33 | Deno.test({ 34 | name: "dropTestEncoding", 35 | fn: (): void => { 36 | assertResources(defaultResources); 37 | 38 | const decoder = new TextDecoder(); 39 | decoder.decode(new Uint8Array([1, 2, 3]), { stream: true }); 40 | 41 | assertResources({ ...defaultResources, "9": "textDecoder" }); 42 | 43 | drop(TextDecoderResource); 44 | assertResources(defaultResources); 45 | }, 46 | }); 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## `deno_drop` 2 | 3 | ![](https://i.imgur.com/AumwQ4y.png) 4 | 5 | Manage your programs resource memory. Check out its 6 | [impact on memory usage](#impact) 7 | 8 | 9 | > **Do not use in production** - A lot of the code depends on internal Deno APIs that can change anytime. Intead, consider consuming your resources (like `await response.arrayBuffer()` on `Response`) to free up memory. 10 | 11 | ### Impact 12 | 13 | Here we have a long running loop pinging a local server. The request is stored 14 | as a resource in Deno's internal resource table. 15 | 16 | ```typescript 17 | import { drop, FetchResource } from "https://deno.land/x/drop@1.12.0/mod.ts"; 18 | 19 | for (let i = 0; i < 1e4; i++) { 20 | await fetch("http://localhost:8000/images.png"); 21 | // With `drop` : 17.8mb 22 | // Without `drop` (default) : 200.1mb 23 | drop(FetchResource); 24 | } 25 | ``` 26 | 27 | Doing this will create `10000` resources for every request made. Which can take 28 | up a LOT of memory. 29 | 30 | By adding the `drop(FetchResource)`, you will notice the memory consumption 31 | decrease drastically. 32 | 33 | ### Examples 34 | 35 | ```typescript 36 | import { 37 | drop, 38 | TextDecoderResource, 39 | } from "https://deno.land/x/drop@1.12.0/mod.ts"; 40 | 41 | const decoder = new TextDecoder(); 42 | decoder.decode(new Uint8Array([1, 2, 3]), { stream: true }); 43 | 44 | drop(TextDecoderResource); 45 | ``` 46 | 47 | ```typescript 48 | import { drop } from "https://deno.land/x/drop@1.12.0/mod.ts"; 49 | 50 | const adapter = await navigator.gpu.requestAdapter(); 51 | const device = await adapter?.requestDevice(); 52 | 53 | drop(device); 54 | drop(adapter); 55 | ``` 56 | 57 | ### License 58 | 59 | MIT 60 | -------------------------------------------------------------------------------- /drop.ts: -------------------------------------------------------------------------------- 1 | import { GPUAdapterResource, Resource } from "./resources.ts"; 2 | 3 | type Rid = number; 4 | 5 | // deno-lint-ignore no-explicit-any 6 | function _isResource(maybeResource: any) { 7 | // XXX: Better way to test if parameter is Resource object? 8 | return typeof maybeResource == "object" && !!maybeResource.name; 9 | } 10 | 11 | // deno-lint-ignore no-explicit-any 12 | export function drop(resource: Resource | Rid | any): boolean { 13 | if (typeof resource == "number") { 14 | try { 15 | Deno.close(resource); 16 | return true; 17 | } catch (_) { 18 | return false; 19 | } 20 | } // *Magically* determine resource ID from instance :eyes: 21 | else if ( 22 | // https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/op_crates/fetch/26_fetch.js#L870 23 | // @ts-ignore Ignore `--unstable` not detected diagnostic. 24 | (Deno.HttpClient && resource instanceof Deno.HttpClient) || 25 | // https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/op_crates/websocket/01_websocket.js#L258. 26 | (WebSocket && resource instanceof WebSocket) || 27 | // https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/runtime/js/30_files.js#L106 28 | (Deno.File && resource instanceof Deno.File) 29 | ) { 30 | resource.close(); 31 | return true; 32 | } else if ( 33 | // https://gpuweb.github.io/gpuweb/#dom-gpudevice-destroy 34 | (GPUDevice && resource instanceof GPUDevice) || 35 | // https://gpuweb.github.io/gpuweb/#dom-gputexture-destroy 36 | (GPUTexture && resource instanceof GPUTexture) || 37 | // https://gpuweb.github.io/gpuweb/#dom-gpubuffer-destroy 38 | (GPUBuffer && resource instanceof GPUBuffer) || 39 | // https://gpuweb.github.io/gpuweb/#dom-gpuqueryset-destroy 40 | (GPUQuerySet && resource instanceof GPUQuerySet) 41 | ) { 42 | resource.destroy(); 43 | return true; 44 | } else if (Worker && resource instanceof Worker) { 45 | resource.terminate(); 46 | return true; 47 | // @ts-ignore Ignore `--unstable` diagnostics 48 | } else if (Deno.SignalStream && resource instanceof Deno.SignalStream) { 49 | resource.dispose(); 50 | return true; 51 | } else if (GPUAdapter && resource instanceof GPUAdapter) { 52 | return drop(GPUAdapterResource); 53 | } else if (_isResource(resource)) { 54 | const rt: Deno.ResourceMap = Deno.resources(); 55 | for (const rid in rt) { 56 | if (rt[rid] == resource.name) { 57 | Deno.close(Number(rid)); 58 | return true; 59 | } 60 | } 61 | return false; 62 | } 63 | // https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/runtime/js/40_fs_events.js#L16 64 | // XXX add links for: https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/runtime/js/39_net.js 65 | if ( 66 | // https://github.com/denoland/deno/blob/10b99e8eb0e04e8340187b8aafe860405114d0d7/runtime/js/40_process.js#L38 67 | (Deno.Process && resource instanceof Deno.Process) 68 | // XXX: Deno does not include the below classes in its namespace. 69 | // (Deno.Conn && resource instanceof Deno.Conn) || 70 | // (Deno.Listener && resource instanceof Deno.Listener) 71 | // (Deno.Datagram && resource instanceof Deno.Datagram) 72 | ) { 73 | if (Object.getOwnPropertyDescriptor(resource, "rid")!["get"]) { 74 | try { 75 | Deno.close(resource.rid); 76 | return true; 77 | } catch (_) { 78 | return false; 79 | } 80 | } 81 | } 82 | return false; 83 | } 84 | --------------------------------------------------------------------------------