Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions pkg/cmd/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func List(ctx context.Context, client *containerd.Client, options types.Containe
if err != nil {
return nil, err
}
return prepareContainers(ctx, client, containers, cMap, options)
return prepareContainers(ctx, client, &containers, cMap, options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

}

// filterContainers returns containers matching the filters.
Expand Down Expand Up @@ -134,50 +134,47 @@ func (x *ListItem) Label(s string) string {
return x.LabelsMap[s]
}

func prepareContainers(ctx context.Context, client *containerd.Client, containers []containerd.Container, statusPerContainer map[string]string, options types.ContainerListOptions) ([]ListItem, error) {
listItems := make([]ListItem, len(containers))
func prepareContainers(ctx context.Context, client *containerd.Client, containers *[]containerd.Container, statusPerContainer map[string]string, options types.ContainerListOptions) ([]ListItem, error) {
listItems := make([]ListItem, len(*containers))
snapshottersCache := map[string]snapshots.Snapshotter{}
for i, c := range containers {
info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
for i := range *containers {
info, err := (*containers)[i].Info(ctx, containerd.WithoutRefreshedMetadata)
if err != nil {
if errdefs.IsNotFound(err) {
log.G(ctx).Warn(err)
continue
}
return nil, err
}
spec, err := c.Spec(ctx)
if err != nil {
if errdefs.IsNotFound(err) {
log.G(ctx).Warn(err)
continue
}
return nil, err
}
id := c.ID()
id := (*containers)[i].ID()
if options.Truncate && len(id) > 12 {
id = id[:12]
}
var status string
if s, ok := statusPerContainer[c.ID()]; ok {
if s, ok := statusPerContainer[(*containers)[i].ID()]; ok {
status = s
} else {
return nil, fmt.Errorf("can't get container %s status", c.ID())
return nil, fmt.Errorf("can't get container %s status", (*containers)[i].ID())
}
dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address)
if err != nil {
return nil, err
}
containerLabels, err := c.Labels(ctx)
containerLabels, err := (*containers)[i].Labels(ctx)
if err != nil {
return nil, err
}
ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, c.ID(), containerLabels)
ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, (*containers)[i].ID(), containerLabels)
if err != nil {
return nil, err
}

cmd, err := formatter.GetCommandFromSpec(info.Spec, options.Truncate, true)
if err != nil {
continue
}
li := ListItem{
Command: formatter.InspectContainerCommand(spec, options.Truncate, true),
Command: cmd,
CreatedAt: info.CreatedAt,
ID: id,
Image: info.Image,
Expand All @@ -201,6 +198,7 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
}
li.Size = containerSize
}
(*containers)[i] = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

@ningmingxiao ningmingxiao Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I want to let gc recycle the memory quickly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That behavior has to be explained in the code comments in the function declaration

listItems[i] = li
}
return listItems, nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/errdefs"
"github.com/containerd/go-cni"
"github.com/containerd/typeurl/v2"
)

func ContainerStatus(ctx context.Context, c containerd.Container) string {
Expand Down Expand Up @@ -73,6 +74,14 @@ func ContainerStatus(ctx context.Context, c containerd.Container) string {
}
}

func GetCommandFromSpec(spec typeurl.Any, trunc, quote bool) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code does not need to be a separate function, as it is called only once

Copy link
Contributor Author

@ningmingxiao ningmingxiao Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since spec have much data, If I use it into a function, it may cause quickly gc (gc may happen after the function return)
if I use it into a for loop (gc may happen after every loop). @AkihiroSuda

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just create a scope with {}

Also please add the code comments about GC

var s oci.Spec
if err := json.Unmarshal(spec.GetValue(), &s); err != nil {
return "", err
}
return InspectContainerCommand(&s, trunc, quote), nil
}

func InspectContainerCommand(spec *oci.Spec, trunc, quote bool) string {
if spec == nil || spec.Process == nil {
return ""
Expand Down
Loading